Java 设置纸牌游戏进入无限循环,有2名玩家

Java 设置纸牌游戏进入无限循环,有2名玩家,java,arrays,oop,Java,Arrays,Oop,这是一个使用Java的纸牌游戏。游戏规则张贴在下面 ● There are 50 cards ● Each card is a colour out of red, green, blue, yellow, and purple ● Each card have a value between 1 to 10 To play Cody’s Card game, you have 3 players. Each player plays a card from the top of their

这是一个使用Java的纸牌游戏。游戏规则张贴在下面

● There are 50 cards
● Each card is a colour out of red, green, blue, yellow, and purple
● Each card have a value between 1 to 10

To play Cody’s Card game, you have 3 players. Each player plays a card from the top of
their hand and they are compared to each other. Whoever has the highest value wins all 3
cards. If there is a tie for the highest value, the winner will be in order of the colour
(Red>Green>Blue>Yellow>Purple).

Here are 3 examples:

Round 1:
Player 1 = Red 5
Player 2 = Blue 3
Player 3 = Purple 10

Player 3 wins

Round 2:
Player 1 = Red 5
Player 2 = Blue 5
Player 3 = Purple 10

Player 3 wins

Round 3:
Player 1 = Red 5
Player 2 = Blue 5
Player 3 = Purple 1

Player 1 wins

As with war, the game goes until one player has all of the cards. Once one player is out, the
game continues with only 2 players. Your program should output each round and who wins. Make the program ask if you want to play again when the game is finished. As well make the program ask for the 3 player names in the beginning.
因此,该程序对3名玩家运行良好。但是有两名玩家,游戏总是进入无限循环,两名玩家之间不断交换卡片

这是分配牌的函数(仅在游戏开始时调用)

/**
*将特定数量的牌返回给玩家。还将从所有卡片列表中删除指定的卡片
*
*@param name玩家名称
*@param num要分配的卡数。(25或16)
*@返回该特定玩家的牌列表
*/
静态ArrayList赋值卡(字符串名称,int num){
整数长度;
随机r=新随机();
ArrayList a=新的ArrayList();
对于(inti=0;i)我在ie上面发布的所有代码,方法和下面的代码都放在Main.java文件中

 /**
     * Returns the 50 cards
     *
     * @return 50 Cards
     */
    static ArrayList<Card> makeAllCards() {
        ArrayList<Card> allCards = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            allCards.add(new Card(i, Card.RED));
            allCards.add(new Card(i, Card.PURPLE));
            allCards.add(new Card(i, Card.GREEN));
            allCards.add(new Card(i, Card.BLUE));
            allCards.add(new Card(i, Card.YELLOW));
        }
        return allCards;
    }
/**
*返回50张卡
*
*@return 50张卡
*/
静态ArrayList makeAllCards(){
ArrayList allCards=新的ArrayList();
对于(int i=0;i<10;i++){
所有卡片。添加(新卡片(i,卡片。红色));
添加(新卡片(i,卡片.紫色));
所有卡片。添加(新卡片(i,卡片。绿色));
所有卡片。添加(新卡片(i,卡片。蓝色));
所有卡片。添加(新卡片(i,卡片。黄色));
}
归还所有卡片;
}
主要方法

  //    Card List
    public static ArrayList<Card> allCards = new ArrayList<>();

    static Scanner scanner = new Scanner(System.in);

    //    Player List
    static ArrayList<Player> players = new ArrayList<>();
    static boolean slowModeEnabled = false;

    /**
     * Main function
     *
     * @param args Arguments for running the program
     */
    public static void main(String[] args) {
        //Scanner object to receive inputs
        // System.out.println("Welcome to my Card game;");

        //Game loop with condition to check for user conformation
        do {
            //Adding the players to the list
            // System.out.println("Enter player 1 name");
            players.add(new Player(scanner.nextLine()));

            // System.out.println("Enter player 2 name");
            players.add(new Player(scanner.nextLine()));

            // System.out.println("Enter player 3 name");
            players.add(new Player(scanner.nextLine()));

            // System.out.println("Slowmode? y/n");
            if (scanner.nextLine().equals("y")) {
                slowModeEnabled = true;
            }

//            Add all the cards
            allCards = makeAllCards();

//            initial round
            play();



//            //            Remove all the cards and re assign them
//            for (Player player : players) {
//                player.playerCards.clear();
//                player.playerCards.addAll(assignCards(player.playerName, 25));
//            }

            //Second round
            for(Player player:players) {
                System.out.println(player.playerCards.size());
            }


            playWithTwoPlayers();

            // System.out.println(playerLostSb.toString());

             System.out.println(players.get(0).playerName + " has won the match");

             System.out.println("Press 'y' to continue again!");

        } while (scanner.nextLine().equals("y"));

//        Ending
        scanner.close();
        // System.out.println("Bye. ");
}
//卡片列表
public static ArrayList allCards=new ArrayList();
静态扫描仪=新扫描仪(System.in);
//球员名单
静态ArrayList players=新建ArrayList();
静态布尔值slowModeEnabled=false;
/**
*主要功能
*
*@param args用于运行程序的参数
*/
公共静态void main(字符串[]args){
//要接收输入的扫描仪对象
//System.out.println(“欢迎来到我的纸牌游戏;”);
//带有检查用户确认条件的游戏循环
做{
//将玩家添加到列表中
//System.out.println(“输入播放器1名称”);
添加(新播放器(scanner.nextLine());
//System.out.println(“输入播放器2名称”);
添加(新播放器(scanner.nextLine());
//System.out.println(“输入播放器3名称”);
添加(新播放器(scanner.nextLine());
//系统输出打印项次(“Slowmode?是/否”);
if(scanner.nextLine().equals(“y”)){
slowModeEnabled=true;
}
//把所有的牌都加上
allCards=makeAllCards();
//首轮
play();
////移除所有卡并重新分配它们
//用于(玩家:玩家){
//player.playerCards.clear();
//player.playerCards.addAll(分配卡片(player.playerName,25));
//            }
//第二轮
用于(玩家:玩家){
System.out.println(player.playerCards.size());
}
与两名玩家一起玩();
//System.out.println(playerLostSb.toString());
System.out.println(players.get(0.playerName+“赢得了比赛”);
System.out.println(“按“y”再次继续!”);
}while(scanner.nextLine().equals(“y”));
//结束
scanner.close();
//System.out.println(“再见”);
}
这是完整的文件(Main.java):

我已经修复了您的代码

在您的PlayWithTwoPlayer方法中

if(cardList.size() <2){System.out.println("asdfasdfasdf"); break;} //2 instead of 1

似乎无限循环的原因是
PlayWithTwoPlayer()
仅在两个玩家都没有牌时停止,这是不可能发生的

while  player-0 has cards  OR  player-1 has cards
当所有的牌都由一个玩家持有时,它实际上应该停止。 因此,停止条件可能如下所示:

while (player_0_has_cards && player_1_has_cards) {
...
}
不同的卡片视图有助于实现这一点

  • 值:1-10-->数字0-9
  • 颜色:1-5
因此:

  • 卡片是介于1(01)到95之间的唯一数字。(跳过某些数字)
  • 由N个不同数字的最大值决定的一轮赢家,N=2或3(例如)
  • 对任意数量的玩家使用相同的代码
编辑:添加更多详细信息

重写比调试当前代码更容易

  • 三大类:牌、玩家、游戏

  • 玩家知道如何添加/删除牌,并知道是否可以玩更多的牌

  • 通过使用上面提到的整数权重,卡片比较更简单
  • 相同的代码处理任意数量的玩家
dealCards();
int n=0;
while(players.size()>1&&n
  • 处理初始卡
List cards=new ArrayList();
对于(int value=1;value=0;i--){
如果(!players.get(i.canPlay()){
球员。移除(i);
}
}

你能用你的主方法、播放器类等编辑你的问题吗?我们可以复制粘贴到我们的IDE并测试它。我还建议你在所有相关的地方添加一些System.out调用,这样你就可以看到发生了什么。或者使用调试器。当然我会编辑问题。我也添加了大量的print语句,还使用了调试器。但不幸的是,我无法理解它out@JavaMan我已经添加了所有的代码,到目前为止,我已经知道如果你用一个固定的种子1初始化随机数(如此新的随机数(1))你会得到一个IndexOutOfBoundException…如果你使用42而不是1,那么程序运行得很好…但我必须进一步调查…使用3,它会一直持续下去哦
  //    Card List
    public static ArrayList<Card> allCards = new ArrayList<>();

    static Scanner scanner = new Scanner(System.in);

    //    Player List
    static ArrayList<Player> players = new ArrayList<>();
    static boolean slowModeEnabled = false;

    /**
     * Main function
     *
     * @param args Arguments for running the program
     */
    public static void main(String[] args) {
        //Scanner object to receive inputs
        // System.out.println("Welcome to my Card game;");

        //Game loop with condition to check for user conformation
        do {
            //Adding the players to the list
            // System.out.println("Enter player 1 name");
            players.add(new Player(scanner.nextLine()));

            // System.out.println("Enter player 2 name");
            players.add(new Player(scanner.nextLine()));

            // System.out.println("Enter player 3 name");
            players.add(new Player(scanner.nextLine()));

            // System.out.println("Slowmode? y/n");
            if (scanner.nextLine().equals("y")) {
                slowModeEnabled = true;
            }

//            Add all the cards
            allCards = makeAllCards();

//            initial round
            play();



//            //            Remove all the cards and re assign them
//            for (Player player : players) {
//                player.playerCards.clear();
//                player.playerCards.addAll(assignCards(player.playerName, 25));
//            }

            //Second round
            for(Player player:players) {
                System.out.println(player.playerCards.size());
            }


            playWithTwoPlayers();

            // System.out.println(playerLostSb.toString());

             System.out.println(players.get(0).playerName + " has won the match");

             System.out.println("Press 'y' to continue again!");

        } while (scanner.nextLine().equals("y"));

//        Ending
        scanner.close();
        // System.out.println("Bye. ");
}
if(cardList.size() <2){System.out.println("asdfasdfasdf"); break;} //2 instead of 1
if ((players.size() > 2) && cardList.get(1).value == cardList.get(2).value) { // added the .value, you were comparing the objects, not the card values
while  player-0 has cards  OR  player-1 has cards
while (player_0_has_cards && player_1_has_cards) {
...
}
    String name;
    List<Card> cards = new ArrayList<>();

    Player(String name) {
        this.name = name;
    }

    void putCard(Card card) {
        cards.add(card);
    }

    Card deleteCard() {
        return cards.remove(0);
    }

    boolean canPlay() {
        return !cards.isEmpty();
    }
    enum Color {
        PURPLE(1),
        YELLOW(2),
        BLUE(3),
        GREEN(4),
        RED(5);

        int rank;
        static Color[] colors = colors();

        Color(int rank) {
            this.rank = rank;
        }
    }
    int value;
    Color color;
    int weight;

    Card(int value, Color color) {
        this.value = value;
        this.color = color;
        this.weight = (10 * value) + color.rank;
    }

    @Override
    public int compareTo(Card o) {
        return Integer.compare(o.weight, this.weight);
    }
        dealCards();
        int n = 0;
        while (players.size() > 1 && n < maxRounds) {
            playRound(++n);
        }
        if (players.size() == 1) {
            System.out.println("\nAnd the winner is: " + players.get(0).toString());
        } else {
            System.out.println("\nStopping as there's no winner after too many rounds: " + n);
        }
        List<Card> cards = new ArrayList<>();
        for (int value = 1; value <= 10; value++) {
            for (Color color : Color.values()) {
                cards.add(new Card(value, color));
            }
        }
        Random r = new Random();
        int nextPlayer = 0;
        while (!cards.isEmpty()) {
            players.get(nextPlayer).putCard(cards.remove(r.nextInt(cards.size())));
            nextPlayer = (++nextPlayer) % players.size();
        }
        Map<Card, Player> challenge = new HashMap<>();
        for (Player player : players) {
            Card card = player.deleteCard();
            challenge.put(card, player);
            System.out.println(player.toString() + " played card: " + card);
        }
        List<Card> keys = new ArrayList<>(challenge.keySet());
        Collections.sort(keys);

        Card winningCard = keys.get(0);
        Player winner = challenge.get(winningCard);
        for (Card card : keys) {
            winner.putCard(card);
        }

        for (int i = players.size() - 1; i >= 0; i--) {
            if (!players.get(i).canPlay()) {
                players.remove(i);
            }
        }