java - 我怎样才能退出这个功能呢?

标签 java arraylist infinite-loop arraydeque

我正在创建一个模拟纸牌游戏“ war ”的java程序。请注意,我对编码还很陌生。我的牌组里有 54 张牌,其中包括两张王牌。 war 开始后,我的程序会进行无休止的 war ,并继续向玩家的牌组中添加越来越多的牌。谁能看到我缺少什么吗?这是我的播放功能:

public void play() {
        roundCount = 1;
        //make sure player decks are not empty
        while (!player1.isEmpty() && !player2.isEmpty()) {
            //lay down top card
            Card p1card = player1.pop();
            Card p2card = player2.pop();
            //report cards played
            System.out.println("Round: " + roundCount + "\n" + "Player One Card: " + p1card + "\n" + "Player Two Card: " + p2card);
            //if player one wins, add both cards to player one deck
            if (p1card.getValue() > p2card.getValue()) {
                player1.addCard(p2card);
                player1.addCard(p1card);
                System.out.println("Player One wins this Round.");
                //check card counts
                System.out.println("Player One new card count: " + player1.size());
                System.out.println("Player Two new card count: " + player2.size() + "\n");
                roundCount++;
            }
            //if player two wins, add both cards to player two deck
            else if (p2card.getValue() > p1card.getValue()) {
                player2.addCard(p1card);
                player2.addCard(p2card);
                System.out.println("Player Two wins this Round.");
                //check card counts
                System.out.println("Player One new card count: " + player1.size());
                System.out.println("Player Two new card count: " + player2.size() + "\n");
                roundCount++;
            }
            //if values are equal, start a war
            else if (p1card.getValue() == p2card.getValue()) {
                System.out.println("Time to declare war!");
                //set up arrays to store cards used in the war
                ArrayList<Card> p1List = new ArrayList<Card>();
                ArrayList<Card> p2List = new ArrayList<Card>();
                //call warRound function
                warRound(player1, player2, p1List, p2List);
                //if player one wins the war, add initial round cards
                if (warRound(player1, player2, p1List, p2List) == player1) {
                    player1.addCard(p1card);
                    player1.addCard(p2card);
                    //check card counts
                    System.out.println("Player One card count: " + player1.size());
                    System.out.println("Player Two card count: " + player2.size());
                }
                //if player two wins the war, add initial round cards
                else if (warRound(player1, player2, p1List, p2List) == player2) {
                    player2.addCard(p1card);
                    player2.addCard(p2card);
                    //check card counts
                    System.out.println("Player One card count: " + player1.size());
                    System.out.println("Player Two card count: " + player2.size());
                }

            }

        }
        //player one runs out of cards
        if (player1.isEmpty()) {
            System.out.println("Player Two has won the game.");
            System.out.println("Player One card count: " + player1.size());
            System.out.println("Player Two card count: " + player2.size());
        }
        //player two runs out of cards
        else if (player2.isEmpty()) {
            System.out.println("Player One has won the game.");
            System.out.println("Player One card count: " + player1.size());
            System.out.println("Player Two card count: " + player2.size());
        }
    }

然后这是我运行 war 的函数:

public Player warRound(Player player1, Player player2, ArrayList<Card> p1List, ArrayList<Card> p2List) {
        warCount = 1;
        //check that players have enough cards for the war
        if (player1.size()>=4 && player2.size()>=4) {
            //lay down three cards for each player
            for (int i = 0; i < 3; i++) {
                p1List.add(player1.pop());
            }
            for (int i = 0; i < 3; i++) {
                p2List.add(player2.pop());
            }
            //temporary test to make sure for loops add correct amount of cards
            System.out.println("Test: " + p1List.size() + p2List.size());

            //cards used to determine war winner
            Card p1War = player1.pop();
            System.out.println("Player one war card: " + p1War);
            Card p2War = player2.pop();
            System.out.println("Player two war card: " + p2War);

            //war outcomes
            if (p1War.getValue() > p2War.getValue()) {
                //if player one wins, add cards from both arrays to player one deck
                for (Card retrieveCard : p1List) {
                    player1.addCard(retrieveCard);
                }
                for (Card wonCard : p2List) {
                    player1.addCard(wonCard);
                }
                //add cards that determined winner to player one deck
                player1.addCard(p1War);
                player1.addCard(p2War);
                System.out.println("Player One won the war!");
                return player1;
            } else if (p1War.getValue() < p2War.getValue()) {
                //if player two wins, add cards from both arrays to player two deck
                for (Card retrieveCard : p2List) {
                    player2.addCard(retrieveCard);
                }
                for (Card wonCard : p1List) {
                    player2.addCard(wonCard);
                }
                //add cards used to determine war winner to player two deck
                player2.addCard(p2War);
                player2.addCard(p1War);
                System.out.println("Player Two won the war!");
                //check card counts
                return player2;
            } else {
                //if there is a double war, add initial comparison cards to arrayList
                //call for another war round
                System.out.println("A war within a war has begun!");
                p1List.add(p1War);
                p2List.add(p2War);
                //check each player has enough cards for another war
                if (player1.size()>=4 && player2.size()>=4) {
                    warRound(player1, player2, p1List, p2List);
                }
                //if one player does not have enough cards for another war, other player wins
                else if (player1.size()<4 || player2.size()<4) {
                    System.out.println("One of the players does not have enough cards for a war!");
                    if (player1.size() > player2.size()) {
                        System.out.println("Player Two ran out of cards, and Player One is the winner!");
                        return null;
                    } else if (player2.size() > player1.size()) {
                        System.out.println("Player One ran out of cards, and Player Two is the winner!");
                        return null;
                    }
                }
                warCount++;
            }
        }
        //if one player does not have enough cards for war, other player wins
        else if (player1.size()<4 || player2.size()<4) {
            if (player1.size() > player2.size()) {
                System.out.println("Player Two ran out of cards, and Player One is the winner!");
                return player1;
            } else if (player2.size() > player1.size()) {
                System.out.println("Player One ran out of cards, and Player Two is the winner!");
                return player2;
            }
        }
        warCount++;
        throw new IllegalStateException();
    }

我在这个类中还有一个函数可以初始化牌组,并且我的打印语句显示它工作正常。我有单独的类来创建卡片、初始套牌和我的玩家。如果显示这些类有帮助,我可以发布包含更多代码的更新。预先感谢您。

(编辑以添加我的玩家类别)

package midterm_proj;

import java.util.ArrayDeque;

public class Player {
    //instance variables
    private ArrayDeque<Card> playerdeck;
    //constructor
    public Player() {
        playerdeck = new ArrayDeque<Card>(); //construct w/empty deck
    }
    //methods
    public void addCard(Card c) {
        playerdeck.addLast(c);
    }
    public String toString() {
        String sb = "";
        for (var card : playerdeck) {
            sb += card.getValue() + card.getSuit() + "\n";
        }
        return sb;
    }
    public boolean isEmpty() {
        if (playerdeck.size()!=0) {
            return false;
        }
        else {
            return true;
        }
    }
    public int size() { return playerdeck.size(); }
    public Card pop() {
        Card topCard = playerdeck.pop();
        return topCard;
    }
}

最佳答案

@ScaryWombat 我认为 == 比较就足够了。我们想要比较实际的对象引用。

@Shayla 你可以发布 Player 类来了解它的方法吗?我的怀疑是围绕 .pop() 方法和 isEmpty() 方法

我想如果你在每次 war 后添加每个玩家的卡牌数量,那么调试会更好。

关于java - 我怎样才能退出这个功能呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61219483/

相关文章:

php - 简单 xml 元素上的 foreach 中的无限循环

python - 我用Python编写了一段代码,即使我使用break语句并继续,它仍然会进入Python :的无限循环

java - 如何公开第三方类(class)?

java - java中的数组列表

java - 如何在java中保存记录并使用记录值更新同一页面

java - 如何创建一个二维ArrayList?

java - ArrayList 在监听器外部不可用

infinite-loop - 所有的无限循环都是坏的吗?

java - 根据流中的匹配条件返回元素

java - 在没有默认构造函数的情况下调试 XStream