Domanda

I'm designing a multiplayer game of poker. I have Human and Computer objects, both which implement a Player interface which contains methods necessary to a poker player. I have an ArrayList of the players in the game, and I need to go to each player and check whether they want to fold their hand or stand. If everyone has folded, then the last person to check their play automatically wins. For each hand, the starting player needs to rotate. For the first hand, the person in index 0 of the ArrayList will go first. The second hand, the person in index 1 will go first. Just looking to bounce ideas and hear peoples views on how they'd implement these features.

Initially I had the idea of doing something like this;

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;
           }
        }
     }
 }

}

È stato utile?

Soluzione

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.

Altri suggerimenti

    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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top