简体   繁体   English

Java排列

[英]Java permutations

I am trying to write a program which can keep track of the number of chips each player has in poker. 我正在尝试编写一个程序,该程序可以跟踪每个玩家在扑克中拥有的筹码数量。 This is not a poker game, only a program which can keep the number of chips each player has. 这不是扑克游戏,而是一个可以保留每个玩家拥有的筹码数量的程序。 However, I want to be able to "bet" "call" "fold" etc and be able to automatically keep track. 但是,我希望能够“下注”“呼叫”“弃牌”等,并能够自动跟踪。

My question is, is there a way for adjusting the number of players without writing each permutations? 我的问题是,有没有一种方法可以在不编写每个排列的情况下调整玩家数量? Currently, it can only keep track of 2 people. 目前,它只能跟踪2个人。 How would I make it so it can theoretically keep track of an infinite amount of people? 我将如何做到这一点,以便在理论上可以跟踪无数人? I've written every permutation possible and I know it's not very efficient so is there a more efficient way of coding this? 我已经写了所有可能的排列,但我知道它不是很有效,所以有没有更有效的编码方法?

I am a java noob so any help would be appreciated. 我是一个Java菜鸟,所以任何帮助将不胜感激。

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

} }

Yes but you will need to re-think the way your program is designed. 是的,但是您将需要重新考虑程序的设计方式。

The easiest way to do this would be to create a Player class, and implement every action a player performs as a method within this class. 最简单的方法是创建一个Player类,并将玩家执行的每个动作作为该类中的方法来实现。 In order to manage the Player's chips you would have a variable Player.chips that is either globally accessible or managed with methods such as Player.getChips() and Player.setChips() . 为了管理玩家的筹码,您需要具有一个变量Player.chips ,该变量可以全局访问,也可以使用Player.getChips()Player.setChips()方法进行管理。

For multiple players you would have an array of Player 's ie. 对于多个玩家,您将拥有一个Player数组,即。

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

The following post may help you more (similar problem but using Blackjack instead of Poker ) - How to initialize an array of objects in Java . 以下文章可能会为您提供更多帮助(类似的问题,但使用Blackjack代替Poker)- 如何在Java中初始化对象数组

I think the main thing you're missing is that methods can take parameters . 我认为您最缺少的是方法可以带有参数

So rather than having things in the style of; 因此,与其拥有风格相似的事物;

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

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

You would have; 你将会拥有;

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

Java naming conventions dictate that method and field names must start with a lower-case letter, class names with an upper case. Java命名约定规定方法和字段名称必须以小写字母开头,类名称必须以大写字母开头。 Also don't forget that methods can return stuff, useful stuff . 同样不要忘记方法可以返回有用的东西 All your methods are void , try thinking about how they could return some kind of status to the calling function. 您所有的方法都是void ,请尝试考虑如何将某些状态返回给调用函数。

For example you could have a method int getBet(int playerID) which requests the user to type their bet amount, checks it against their chip stack (using the playerID parameter), and returns their bet amount when it is valid. 例如,您可能有一个方法int getBet(int playerID) ,该方法要求用户键入他们的下注金额,对照他们的筹码量进行检查(使用playerID参数),并在有效时返回其下注金额。 Much better than rewriting everything over and over right? 比一遍又一遍地重写所有内容要好得多?

Final tip: if you find yourself copying a single line of code, you're doing something wrong. 最后提示:如果您发现自己复制的一行代码,你做错了什么。

Edit: Nathan Dunn's answer is also very relevant. 编辑:内森·邓恩的答案也很相关。 Java is an object oriented language, learn to leverage that fact. Java是一种面向对象的语言,请学习利用这一事实。 You certainly need a Player class. 您当然需要一个Player类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM