0

I've a little problem.

Myself and a few friends were playing poker yesterday but we didn't have chips so I decided to start writing a program for that [Without Cards, just Chips].

In my code I have two main variables in the Game Object.

private int id;
private long bank;

I have a different file called Aside in which I can do different calculations.

In the code below I am trying to compare all instance bank variables to see if all the banks matched [In this case this will mean a new card can be drawn, otherwise users will have to keep to either raise or fold].

Is there a way of writing this in an easier term:

package poker;
import java.util.ArrayList;

public class Aside
{

    public boolean compareBanks(ArrayList<Game> x)
    {
        ArrayList<Game> players = new ArrayList(x);

        if(players.get(0).getBank() == players.get(1).getBank() && players.get(0).getBank() == players.get(2).getBank() 
            && players.get(1).getBank() == players.get(2).getBank())
        {
            return true;
        }
        return false;
    }
}

Later I use this here:

while(aside.compareBanks(players))

But the loop keeps going.

I'm fairly intermediate in programming so go easy on me with mistakes.

Thank you for your time.

P.S: This is NOT a code dump.

while(aside.compareBanks(players))
        {

            for(Game x : players)
            {
                if(x.hasPayedBid() == true)
                    {

                    System.out.println("Player : " + x.getName() +  " [Call, Raise, Fold]:");
                    action = in.nextLine();
                    if(action.equalsIgnoreCase("call"))
                    {
                      break;
                    }else if(action.equalsIgnoreCase("raise"))
                    {
                        System.out.println("How much are you raising? $");
                        int raise = in.nextInt();

                        table += raise;

                        x.raise(raise);
                    }else
                    {
                        x.fold();
                    }
                }
            }

            in.nextLine();

            for(Game x : players)
            {
                System.out.println(x.toString() + "\n");
            }

        }//End While

2 Answers2

1

You can do it using java-8 Stream API something like this

return players.stream().allMatch(p -> p.getBlank().equals( players.get(0).getBalnk()))

However if you will use while(aside.compareBanks(players)) and all elements of the list have equal blank value, your while loop will never stop. It is the same as while(true). So in this case you probably need to use if(aside.compareBanks(players)) or in case of equal blank values change them.

Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53
0

Try this

long bankValue=0;
For(Game player: players){

bankValue+=player.getBank(); }

if(bankValue==(players.get(0).getBank()*players.size)){

return true;}

else return false;
snofty
  • 70
  • 7
  • Could you please explain what does the if statement do. The logic for me looks a bit tangled. – ThatHashCodeGuy Nov 24 '16 at 12:24
  • We are validating the sum of all players bank is equal to the number players multiply first player bank value. – snofty Nov 24 '16 at 12:28
  • Oh. Okay that sounds interesting. Does the code handle with the case of: Player 1 Bank: $47 Player 1 Bank: $53 Player 1 Bank: $50 That sum still ends up being: $150 – ThatHashCodeGuy Nov 24 '16 at 12:30