我正在编写一个德州扑克游戏,并创建了此方法对用户手牌进行排名,我想将其变成通用方法,以便我可以通过庄家手牌,然后比较两者,看看哪手牌会更好。如果您能帮助我将其转换为通用方法,我将不胜感激。
public int rankCards(ArrayList<Integer> rankHandList){
//creates a list to put the card ranks into
ArrayList <Integer> newList;
newList = new ArrayList<>();
//Checks the rankHandList i created above
for(int x = 0; x<=rankHandList.size()-1; x++){
int rank = rankHandList.get(x);
//takes all the cards and gets them between 0 and 12
while(rank >= 13){
rank = rank -13;
}
//if a card = 12 it is an ace
if(rank == 12)
newList.add(rank);
//if a card = 11 it is a king
if(rank == 11)
newList.add(rank);
//if a card = 10 it is a queen
if(rank == 10)
newList.add(rank);
//if a card = 9 it is a jack
if(rank == 9)
newList.add(rank);
//if a card = 8 it is a 10
if(rank == 8)
newList.add(rank);
//if a card = 7 it is a 9
if(rank ==7)
newList.add(rank);
//if a card = 6 it is a 8
if(rank ==6)
newList.add(rank);
//if a card = 5 it is a 7
if(rank ==5)
newList.add(rank);
//if a card = 4 it is a 6
if(rank ==4)
newList.add(rank);
//if a card = 3 it is a 5
if(rank ==3)
newList.add(rank);
//if a card = 2 it is a 4
if(rank == 2)
newList.add(rank);
//if a card =1 it is a 3
if(rank ==1)
newList.add(rank);
//if a card = 0 it is a 2
if(rank ==0)
newList.add(rank);
}
//SENDS THE CARDS I JUST RANKED FROM 0-12 TO MAKE THE BEST HAND
makeBestHand(newList);
return 0;
}
我不完全理解你想要什么,因为我理解你想重构你的代码。
public int rankCards(ArrayList<Integer> rankHandList) {
// Creates a list to put the card ranks into
ArrayList <Integer> newList = new ArrayList<Integer>();
// Checks the rankHandList i created above
for (int rank : rankHandList) {
// Add card rank into the list, after module 13 to normalize rank.
newList.add(rank%13);
}
//SENDS THE CARDS I JUST RANKED FROM 0-12 TO MAKE THE BEST HAND
makeBestHand(newList);
return 0;
}