我如何退出此功能?

问题描述 投票:0回答:1

我正在创建一个模拟纸牌游戏“战争”的Java程序。请注意,我对编码还很陌生。我的甲板上有54张纸牌,其中包括两个小丑。战争开始后,我的程序将进行无休止的战争,并继续向玩家的牌组中添加越来越多的卡牌。谁能看到我想念的东西?这是我的播放功能:

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

然后这是我执行战争的功能:

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;
    }
}
java arraylist infinite-loop arraydeque
1个回答
0
投票

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

@@ Shayla您可以发布Player类以了解其方法吗?我的疑虑是围绕.pop()方法和isEmpty()方法

而且我想如果在每场战争之后增加每位玩家的卡数,那将更好地进行调试。

© www.soinside.com 2019 - 2024. All rights reserved.