3
\$\begingroup\$

For playing a Poker tournament, I wanted to create an application which should calculate the pot and the money of all players at every time.

PokerBank class:

public class PokerBank {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    System.out.println("---------------Spiel Beginnen----------------");

    try {
        request();
    } catch (IOException e) {
        System.err.println("Fehler bei der Eingabe");
    }
    String[] playernames = new String[numberOfPlayers];
    try {
        playernames = requestNames();
    } catch (IOException e) {
        System.err.println("Fehler bei der Names Eingabe");
    }

    Player[] players = new Player[playernames.length];
    players = requestPlayers(playernames);

    // Initialisation of the Players
    double bigBlind = initialBigblind;




    // first call of the play with the Pot of zero and the dealer Offset Zero
    boolean round = true;
    // The firts round is set as true becouse it now it is the first round
    playRound(bigBlind, players, round, 0,0);

}
public static int numberOfPlayers;
public static double initialBigblind;
public static double initialCapital;

/**
 * Method to request the main Settings
 * @throws IOException 
 */
public static void request() throws IOException {
    System.out.println("Wie viele Spieler machen mit?");
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    numberOfPlayers = Integer.parseInt(reader.readLine());
    System.out.println("Wie hoch ist der Anfangsbetrag jedes Spielers?");
    initialCapital = Double.parseDouble(reader.readLine());
    System.out.println("Wie hoch ist der Big Blind?");
    initialBigblind = Double.parseDouble(reader.readLine());

}

/**
 * 
 * Method to play the round
 * @param BigBlind the Big blind of the game
 * @param players The Players in form of an Array
 * @param NewRound boolean if it is a new round
 * @param Pot Potsize 
 * @param DealerOffSet to store the position of the dealer
 */
public static void playRound(double BigBlind, Player[] players, boolean NewRound, double Pot, int DealerOffSet) {
    double currentValueOfBet;
    double pot = Pot;
    int dealerOff;
    // if the DealerOffset would be out of the player array bound it will be setted as 0
    // this provides that the "Dealer Button" goes arround
    if(DealerOffSet<players.length-1){
        dealerOff =DealerOffSet;
    }else{dealerOff=0;}

    // if it is a new round e.g.(before the Flop), the big and Small blind will be setted 
    if (NewRound) {
        currentValueOfBet = BigBlind;
        System.out.println("Dealer in dieser runde ist "+players[dealerOff].getName());
    System.out.println("Small Blind von "+BigBlind/2+" zahlt "+players[dealerOff+1].getName());
    players[dealerOff+1].makeStake(BigBlind/2);
    pot+=BigBlind/2;
    players[dealerOff+1].setBlind();
    // to prevent an array out of bound Exception becouse there are just two pepole left in the game
    // there is a if Statement to set another player as the dealer and the BigBlind
    if (players.length<2){
        System.out.println("Den Big Blind von "+BigBlind+" zahlt "+players[dealerOff].getName());
    players[dealerOff].makeStake(BigBlind);
    pot+=BigBlind;
    players[dealerOff].setBlind();
    }else{
    System.out.println("Den Big Blind von "+BigBlind+" zahlt "+players[dealerOff+2].getName());
    players[dealerOff+2].makeStake(BigBlind);
    pot+=BigBlind;
    players[dealerOff+2].setBlind();
    }

    // if it is not the first round(after the flop or someone has risen then the Value of a bet is intitialized as zero
    } else {
        currentValueOfBet = 0;
    }

    // the loop over all players to ask anyone of them what for an action he wants to do
    for (Player player : players) {
        if (!player.getIN()) {
            System.out.println("Spieler " + player.getName() + " ist bereits raus");
        } else {

            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            char choice = 0;
            System.out.println(player.getName() + " Ihre Wahl?");
            try {
                choice = reader.readLine().toLowerCase().charAt(0);
            } catch (IOException e) {
                System.err.println("Keine Gültige Wahl");
            }
            switch (choice) {
                // Choice for call or check
                case 'c':
                    if (player.getStake() <= currentValueOfBet) {
                        pot+=currentValueOfBet-player.getStake();
                        player.makeStake(currentValueOfBet - player.getStake());


                    } else {
                        System.out.println("Sie können nicht callen, da ihr bisheriger einsatz schon höher ist.");
                    }
                    break;
                    // choice for raise
                case 'r':
                    System.out.println("wie viel wollen Sie Setzen?");
                    double raise = 0;
                    try {
                        raise = Double.parseDouble(reader.readLine());
                    } catch (IOException e) {
                        System.err.println("Dies war keine gültige Zahl");
                    }
                    player.makeStake(raise);
                    currentValueOfBet = raise;
                    pot = pot + raise;
                    break;
                    // chase for fold
                case 'f':
                    player.resetStake();
                    player.setOUT();
            }
        }
    }
    for (Player player : players) {

        player.Ausgabe();
        player.resetStake();
        player.resetblind();
    }
    System.out.println("Der Pot steht bei " + pot);

    int numOfIN = 0;
    int index = 0;
    int numOfLost = 0;
    for (int test = 0; test < players.length; test++) {
        if (players[test].getIN()) {
            numOfIN++;
            index = test;
        }

    }
    for (Player player : players) {
        if (player.getMoney() <= 0) {
            numOfLost += 1;
        }
    }
    // if only one is left then he is the winner of the  pot
    if (numOfIN == 1) {
        players[index].earnPot(pot);
        for (Player player : players) {
            player.setIN();
            player.Ausgabe();
        }
        playRound(BigBlind, players, true, 0,dealerOff+1);

    }
    // Next Round without the ones who folded
    if (numOfLost >= players.length - 1) {
        System.out.println("Runde ist Beendet");

        // the big Blind stays the same 
        playRound(BigBlind, players, false, pot,dealerOff+1);

    } else {
        // If the 
        playRound(BigBlind, players, false, pot,dealerOff);
    }

}

/**
 * Method to request the names of the players and stores it in an array
 * @return array with Playernames
 * @throws IOException 
 */
private static String[] requestNames() throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String[] playernames = new String[numberOfPlayers];
    for (int i = 0; i < playernames.length; i++) {
        System.out.println("Name von Spieler " + (i + 1));

        String name;
        name = (reader.readLine());
        playernames[i] = name;
    }
    return playernames;
}

/**
 * Method to initialize all Player objects
 * @param playernames
 * @return Players as an array of Player[]
 */
private static Player[] requestPlayers(String[] playernames) {

    Player[] players = new Player[playernames.length];
    for (int i = 0; i < playernames.length; i++) {

        players[i] = new Player(initialCapital, playernames[i]);
    }
    return players;

}




}

Player class:

public class Player {
double money;
String name;
double currentstake=0;
boolean in=true;
boolean Blind=false;


public Player(double initialCapital, String name){
   this.money = initialCapital;
    this.name=name;

}
/**
 * sets the Stake to zero
 */
public void resetStake(){
    currentstake=0;
}
/**
 * set the Blind to true
 */
public void setBlind(){
    Blind = true;
}
/**
 * set the blind to false
 */
public void resetblind(){
    Blind=false;
}
/**
 * request what for a Blind status the player has
 * @return Blind status
 */
public boolean isBlind(){
    return Blind;
}
/**
 * request how much money the player has
 * @return ammount of money
 */
public double getMoney(){
    return money;
}
/**
 * gives out if the player is still in the game
 * @return Status if the player is in or out of the game
 */
public boolean getIN(){
    return in;
}
/**
 * set the player out of the game
 */
public void setOUT(){
    in=false;
}
/**
 * set the player in the game
 */
public void setIN(){
    in=true;
}
/**
 * sets a new Stake to raise or to call
 * @param stake the stake which the player make
 */
public void makeStake(double stake){
    currentstake=currentstake +stake;
    money=money-stake;
}
/**
 * shows the current Stake
 * @return the current Stake which the player made
 */
public double getStake(){
    return currentstake;
}

/**
 * method to buy in
 * @param ammount of money to buy in
 */
public void earnPot(double ammount){
    money +=ammount;
}
/**
 * returns the Name of the Player
 * @return 
 */
public String getName(){
    return name;
}

/**
 * Gives out the Status of the player
 */
public void Ausgabe(){


   System.out.println(name+"\n"+money+"\n"+"-------------------");

}



}

As you can see, my problem is that the code is very long. I would like to know if there is a way to write the same but in a shorter way.

\$\endgroup\$
3
\$\begingroup\$

Language

The I/O of your programm is german. That's okay, but not so good if you post this in an mainly English Q&A forum. I'm german too ;)

Main method

Player[] players = new Player[playernames.length];
players=requestPlayers(playernames);
double bigBlind = initialBigBlind;
boolean round = true;
playRound(bigBlind,requestPlayers(playernames),round,0,0);

Why don't shorten to:

 playRound(initialBigBlind,new Player[playernames.length],0,0);

request function

Use java.util.Scanner. Also what is if I just input

Wie viele Spieler machen mit?
> foo

Probably not what you want. And you need that code repeatedly. So make an extra function:

static Scanner s = new Scanner(System.in);
public int readInt(String message){
  while(!s.hasNextInt()){
    System.out.println(message);
  }
  return s.nextInt();
}
public double readDouble(String message){
  while(!s.hasNextDouble()){
    System.out.println(message);
  }
  return s.nextDouble();
}

Now the foo as input doesn't works any more to crash your programm.

Wie viele Spieler machen mit?
> foo
Wie viele Spieler machen mit?
> 5
...

playRound function

Messy. Just messy and totally overloaded. And recursive. Clean up a little bit and think about a loop. And renaming the whole thing. Sorry, but I can't give you any specific advise, I don't know Poker good enough to see any logic. That may isn't connected too your code-style. :(

OOP

Java is for object-oriented-programming. If you want to use only functions then go and use Python. No offense to Python but it's definitely not normal Java to put everything (except the Player) into static context. Maybe At least create an class.

public class PokerGame{
  //...
}

Player class

public void setIN(){//...
public void setOUT(){//...

Why don't just use one method

public boolean setIn(boolean in){
  boolean old = this.in;
  this.in = in;
  return old;
}

Ausgabe?!?!?!

public void Ausgabe(){/...

Not pretty English. Also there is a toString() method.

@Override
public void toString(){
  return name+"\n"+money+"\n"+"-------------------"); 
}

And then use System.out.println(player); instead player.Ausgabe();

Sorry If I sound rude sometimes. I don't mean so I'm just not so good in English and this sounds fairly aggressive to me. I don't want to be rude.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to Code Review! For the record, as long as the question is adequately explained in English, it's fine. I much prefer to review real code in its original language than an English version that has been poorly translated just for the sake of posting a question in English. (And I'm not German.) \$\endgroup\$ Oct 23 '16 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.