-1

我正在设计一款多人扑克游戏。我有 Human 和 Computer 对象,它们都实现了 Player 接口,其中包含扑克玩家所需的方法。我有一个游戏中玩家的 ArrayList,我需要去找每个玩家并检查他们是否想要弃牌或站立。如果每个人都弃牌,那么最后一个过牌的人自动获胜。对于每一手,起始玩家需要轮换。对于第一手,ArrayList 索引 0 中的人将先走。第二手,索引 1 中的人会先走。只是想反弹想法并听取人们对如何实现这些功能的看法。

最初我有做这样的事情的想法;

public void poker(ArrayList<Player> players){
   int foldCounter = 0;
   int starter = 0; 

   while (weWantToPlay){
      for (int j = starter; j < players.size(); j++){
         //get the players game plan
         players.get(j).getStand

         //the player is folding
         if (!player.stand()){
            foldCounter++;
            //doStuff
         else{
            //doStuff
         }
     }

     //do more stuff and play poker

     //increment starter so the next hand, the second person starts
     // this obviously will not work, cause we need go to the end of the list, then wrap around
     starter++;

     //check who's folded to see if we automatically have a winner
     if (foldCounter == players.size()-1){
        for (Player element:players){
           if (element.stand()){
              winner = element; 
              break;
           }
        }
     }
 }

}

4

2 回答 2

1

I don't see any question so I'm not sure what problem you're trying to solve.

If it's the 'starter issue' : maybe, instead of using a starter that you increment, you can just iterate over your list of players from 0 to size-1, once you're done you remove the player at index 0 and add it back at the end of the list, and iterate again and etc.

于 2013-04-10T04:40:53.893 回答
0
    I am not much aware of the game poker but as  per my understanding i understood that after each hand the starting player will be changed (i.e if A,B,C are three players then at first time A will be playing first then second time C will and third time B will be the first to play.

    If this is the requirement then you can keep on rotating the players after each round and put your logic to find the winner.

    The following code can be used for rotation


    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;

    public class Main {
      public static void main(String[] args) {
        List numbers = new ArrayList();

        for (int i = 0; i < 5; i++) {
          numbers.add(i);
        }

        System.out.println("Original Array -" + Arrays.toString(numbers.toArray()));

        for(int i=0;i<numbers.size();i++){
            Collections.rotate(numbers, 1);

            System.out.println("After "+ i +" Rotation  -"+ Arrays.toString(numbers.toArray()));

            System.out.println("Element at first position - " +numbers.get(0));
        }
      }
    }


    Output :-

    Original Array -[0, 1, 2, 3, 4]
    After 0 Rotation  -[4, 0, 1, 2, 3]
    Element at first position - 4
    After 1 Rotation  -[3, 4, 0, 1, 2]
    Element at first position - 3
    After 2 Rotation  -[2, 3, 4, 0, 1]
    Element at first position - 2
    After 3 Rotation  -[1, 2, 3, 4, 0]
    Element at first position - 1
    After 4 Rotation  -[0, 1, 2, 3, 4]
    Element at first position - 0



NOTE:If you want to make rotation is some other fashion just change the digit in the following line
Collections.rotate(numbers, 1);
于 2013-04-10T06:26:16.940 回答