2

我正在尝试编写一个程序来跟踪每个玩家在扑克中的筹码数量。这不是扑克游戏,只是一个可以保持每个玩家拥有的筹码数量的程序。但是,我希望能够“下注”“跟注”“弃牌”等并能够自动跟踪。

我的问题是,有没有办法在不写每个排列的情况下调整玩家数量?目前,它只能跟踪 2 个人。我将如何使它理论上可以跟踪无限数量的人?我已经写了所有可能的排列,我知道它不是很有效,所以有没有更有效的编码方式?

我是一个java noob,所以任何帮助将不胜感激。

import java.util.Scanner;
import static java.lang.System.out;

class Chips {
static String move; //first move
static int betVal1, betVal2;
static int pot;
static int P1, P2;
static int roundcount;
static String player;
static Scanner myScanner = new Scanner(System.in);
static int turncount;
static String outcome;

public static void firstP1() { //first move when P1 start
    out.print("P1 - will you 'Bet', 'Check' or 'Fold'? ");
    move = myScanner.next();

    while (!move.equals("Bet") && !move.equals("Check") && !move.equals("Fold")){
    out.print("Please type 'Bet', 'Check' or 'Fold'");
    move = myScanner.next();
    }

    switch(move) {
    case "Bet":
        out.print("How much will you bet? If you've changed your mind: Type '0' to check and '-1' to fold. ");
        betVal1 = myScanner.nextInt();

        while (betVal1 > P1) {
            out.print("You cannot bet more than your chip count. How much will you bet? ");
            betVal1 = myScanner.nextInt();
        }
        if (betVal1 == 0) { //check
            P2aftercheck();
        }
        if (betVal1 == -1) { //fold
            P2 += pot;
            pot = 0;
            roundcount = 0;
            turncount++;

            out.print("P1: ");
            out.println(P1);
            out.print("P2: ");
            out.println(P2);
            out.println("Next turn");

            if (turncount  % 2 == 0) { //check to see who begins next turn
                player = "One";
            } else {
                player = "Two";         
            }

            if( player.equals("One")) {
                firstP1();
            } else {
                firstP2();
            }

        }


        pot += betVal1;
        P1 -= betVal1;
        //out.println(betVal);
        //out.print(pot);
        P2afterbet();
        break;

    case "Check":
        P2aftercheck();
        break;

    case "Fold":
        P2 += pot;
        pot = 0;
        roundcount = 0;
        turncount++;

        if (turncount  % 2 == 0) {
            player = "One";
        } else {
            player = "Two";         
        }

        if( player.equals("One")) {
            firstP1();
        } else {
            firstP2();
        }
        break;

    }
}

public static void firstP2() { //first move when P2 start
    out.print("P2 - will you 'Bet', 'Check' or 'Fold'? ");
    move = myScanner.next();

    while (!move.equals("Bet") && !move.equals("Check") && !move.equals("Fold")){
    out.print("Please type 'Bet', 'Check' or 'Fold' ");
    move = myScanner.next();
    }

    switch(move) {
    case "Bet":
        out.print("How much will you bet? ");
        betVal2 = myScanner.nextInt();
        while (betVal2 > P2) {
            out.print("You can not bet more than your chip count. How much will you bet? ");
            betVal2 = myScanner.nextInt();
        }
        pot += betVal2;
        P2 -= betVal2;
        //out.println(betVal);
        //out.print(pot);
        P1afterbet();
        break;

    case "Check":
        P1aftercheck();
        break;

    case "Fold":
        P1 += pot;
        pot = 0;
        roundcount = 0;
        turncount++;

        out.print("P1: ");
        out.println(P1);
        out.print("P2: ");
        out.println(P2);
        out.println("Next turn");

        if (turncount  % 2 == 0) {
            player = "One";
        } else {
            player = "Two";         
        }

        if( player.equals("One")) {
            firstP1();
        } else {
            firstP2();
        }
        break;

    }
}

public static void P1afterbet() { //P1 move after P2 bet
    out.print("P1 - will you 'Bet', 'Call' or 'Fold'? ");
    move = myScanner.next();

    while (!move.equals("Bet") && !move.equals("Call") && !move.equals("Fold")){
        out.print("Please type 'Bet', 'Check' or 'Fold' ");
        move = myScanner.next();
    }

    switch(move) {
    case "Bet":
        out.print("How much will you bet? ");
        betVal1 = myScanner.nextInt();
        while (betVal1 < betVal2){
            out.print("Please bet at least ");
            out.print(betVal2);
            out.println(" chips.");
            out.print("How much will you bet? ");
            betVal1 = myScanner.nextInt();
        }

        pot += betVal1;
        P1 -= betVal1;
        P2afterbet();
        break;

    case "Call":
        pot += betVal1;
        P1 -= betVal1;
        roundcount++;
        if (roundcount == 4){
            roundend();
        }
        if( player.equals("P1")) {
            firstP1();
        } else {
            firstP2();
        }
        break;

    case "Fold":
        P2 += pot;
        pot = 0;
        roundcount = 0;
        turncount++;

        out.print("P1: ");
        out.println(P1);
        out.print("P2: ");
        out.println(P2);
        out.println("Next turn");

        if (turncount  % 2 == 0) {
            player = "One";
        } else {
            player = "Two";         
        }

        if( player.equals("One")) {
            firstP1();
        } else {
            firstP2();
        }
        break;
    }
}

public static void P1aftercheck() { //P1 move after P2 check
    out.print("P1 - will you 'Bet', 'Check' or 'Fold'? ");
    move = myScanner.next();

    while (!move.equals("Bet") && !move.equals("Check") && !move.equals("Fold")){
    out.print("Please type 'Bet', 'Check' or 'Fold'");
    move = myScanner.next();
    }

    switch(move) {
    case "Bet":
        out.print("How much will you bet? ");
        betVal1 = myScanner.nextInt();
        while (betVal1 > P1) {
            out.print("You can not bet more than your chip count. How much will you bet? ");
            betVal1 = myScanner.nextInt();
        }
        pot += betVal1;
        P1 -= betVal1;
        //out.println(betVal);
        //out.print(pot);
        P2afterbet();
        break;

    case "Check":
        roundcount++;
        if (roundcount == 4) {
            roundend();
        }
        if( player.equals("One")) {
            firstP1();
        } else {
            firstP2();
        }
        break;

    case "Fold":
        P2 += pot;
        pot = 0;
        roundcount=0;
        turncount++; 

        out.print("P1: ");
        out.println(P1);
        out.print("P2: ");
        out.println(P2);
        out.println("Next turn");

        if (turncount  % 2 == 0) {
            player = "One";
        } else {
            player = "Two";         
        }

        if( player.equals("One")) {
            firstP1();
        } else {
            firstP2();
        }
        break;

    }
}

public static void P2afterbet() { //P2 move after P1 bet
    out.print("P2 - will you 'Bet', 'Call' or 'Fold'? ");
    move = myScanner.next();

    while (!move.equals("Bet") && !move.equals("Call") && !move.equals("Fold")){
    out.print("Please type 'Bet', 'Check' or 'Fold'");
    move = myScanner.next();
    }

    switch(move) {
    case "Bet":
        out.print("How much will you bet? ");
        betVal2 = myScanner.nextInt();
        while (betVal2 > P2) {
            out.print("You can not bet more than your chip count. How much will you bet? ");
            betVal2 = myScanner.nextInt();
        }
        while (betVal2 < betVal1) {
            out.print("You must bet at least ");
            out.print(betVal1);
            betVal2 = myScanner.nextInt();
        }
        pot += betVal2;
        P2 -= betVal2;
        //out.println(betVal);
        //out.print(pot);
        P1afterbet();
        break;

    case "Call":
        P2 -= betVal1;
        pot += betVal1;
        roundcount++;
        if (roundcount == 4){
            roundend();
        }
        if( player.equals("P1")) {
            firstP1();
        } else {
            firstP2();
        }
        break;

    case "Fold":
        P1 += pot;
        pot = 0;
        roundcount = 0;
        turncount++;

        out.print("P1: ");
        out.println(P1);
        out.print("P2: ");
        out.println(P2);
        out.println("Next turn");

        if (turncount  % 2 == 0) {
            player = "One";
        } else {
            player = "Two";         
        }

        if( player.equals("One")) {
            firstP1();
        } else {
            firstP2();
        }
        break;

    }
}

public static void P2aftercheck() { //P2 move after P1 check
    out.print("P2 - will you 'Bet', 'Check' or 'Fold'? ");
    move = myScanner.next();

    while (!move.equals("Bet") && !move.equals("Check") && !move.equals("Fold")){
    out.print("Please type 'Bet', 'Check' or 'Fold'");
    move = myScanner.next();
    }

    switch(move) {
    case "Bet":
        out.print("How much will you bet? ");
        betVal2 = myScanner.nextInt();
        while (betVal2 > P1) {
            out.print("You can not bet more than your chip count. How much will you bet? ");
            betVal2 = myScanner.nextInt();
        }
        pot += betVal2;
        P2 -= betVal2;
        //out.println(betVal);
        //out.print(pot);
        P1afterbet();
        break;

    case "Check":
        roundcount++;
        if (roundcount == 4){
            roundend();
        }
        if( player.equals("One")) {
            firstP1();
        } else {
            firstP2();
        }

        break;

    case "Fold":
        P1 += pot;
        pot = 0;
        roundcount=0;
        turncount++;

        out.print("P1: ");
        out.println(P1);
        out.print("P2: ");
        out.println(P2);
        out.println("Next turn");

        if (turncount  % 2 == 0) {
            player = "One";
        } else {
            player = "Two";         
        }

        if( player.equals("One")) {
            firstP1();
        } else {
            firstP2();
        }

        break;

    }
}

public static void roundend() {
    out.print("Who won the round? 'P1' or 'P2'? ");
    outcome = myScanner.next();
    turncount++;

    if (turncount  % 2 == 0) {
        player = "One";
    } else {
        player = "Two";         
    }

    while (!outcome.equals("P1") && !outcome.equals("P2")){
        out.print("Please type 'P1' or 'P2'");
        outcome = myScanner.next();
    }

    if (outcome.equals("P1")){
        P1 += pot;
        pot = 0;
        roundcount = 0;

        out.print("P1: ");
        out.println(P1);
        out.print("P2: ");
        out.println(P2);
        out.println("Next turn");

        if (P1 != 0 && P2 != 0){
            if( player.equals("One")) {
                firstP1();
            } else {
                firstP2();
            }
        } else if (P1 == 0) {
            out.print("P1 is out of chips. P2 Wins!");
        } else {
            out.print("P2 is out of chips. P2 Wins!");
        }
    } else {
        P2 += pot;
        pot = 0;
        roundcount = 0;

        out.print("P1: ");
        out.println(P1);
        out.print("P2: ");
        out.println(P2);
        out.println("Next turn");

        if (P1 != 0 && P2 != 0) {
            if( player.equals("P1")) {
                firstP1();
            } else {
                firstP2();
            }
        } else if (P1 == 0) {
            out.print("P1 is out of chips. P2 Wins!");
        } else {
            out.print("P2 is out of chips. P2 Wins!");
        }
    } 

    System.exit(0);
}

public static void main(String args[]){

    pot = 0;
    roundcount = 0; //status within turn i.e. Flop, Turn, River
    turncount = 2; //use for who starts

    out.print("Please enter starting chip count ");
    P1 = myScanner.nextInt();
    P2 = P1;

    firstP1();
}

}

4

2 回答 2

1

是的,但您需要重新考虑程序的设计方式。

最简单的方法是创建一个Player类,并将玩家执行的每个操作作为该类中的方法实现。为了管理玩家的筹码,您将拥有一个Player.chips可全局访问或使用诸如Player.getChips()和之类的方法管理的变量Player.setChips()

Player对于多个玩家,您将拥有一个's数组。

Player[] Players; 
Players[0] = new Player();
Players[1] = new Player();

以下帖子可能对您有更多帮助(类似的问题,但使用 Blackjack 而不是 Poker) - How to initialize a array of objects in Java

于 2012-10-30T11:48:59.240 回答
0

我认为您缺少的主要内容是方法可以采用参数

因此,与其拥有风格的东西;

private static void player1Move() {
    //player 1 takes his move
}

private static void player2Move() {
    //player 2 takes her move
}

你将会拥有;

private static void move(int playerID) {
    //player playerID takes their move
}

Java 命名约定规定方法名和字段名必须以小写字母开头,类名必须以大写字母开头。也不要忘记方法可以返回东西,有用的东西。您的所有方法都是void,尝试考虑它们如何将某种状态返回给调用函数。

例如,您可以有一个方法int getBet(int playerID)要求用户输入他们的下注金额,对照他们的筹码堆栈检查(使用 playerID 参数),并在有效时返回他们的下注金额。比一遍又一遍地重写所有内容要好得多,对吧?

最后提示:如果您发现自己复制了一行代码,那么您做错了什么。

编辑:内森邓恩的回答也很相关。Java 是一种面向对象的语言,学习利用这一事实。你当然需要一Player堂课。

于 2012-10-30T11:48:11.870 回答