简体   繁体   English

我怎样才能退出这个 function?

[英]How can I exit out of this function?

I am creating a java program that simulates the card game "war".我正在创建一个模拟纸牌游戏“战争”的 java 程序。 Be warned, I am pretty new to coding.请注意,我对编码很陌生。 There are 54 cards in my deck, including two jokers.我的牌库里有 54 张牌,包括两张小丑。 After a war has started, my program runs endless wars and continues adding more and more cards to my players' decks.战争开始后,我的程序会进行无休止的战争,并继续将越来越多的牌添加到玩家的牌组中。 Can anyone see what I am missing?谁能看到我错过了什么? This is my play function:这是我的玩法 function:

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

and then this is my function that runs wars:然后这是我的 function 运行战争:

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

I also have a function in this class that initializes the deck, and my print statements show that it is working correctly.我在这个 class 中还有一个 function 用于初始化卡片组,我的打印语句显示它工作正常。 I have separate classes that create the cards, the initial deck, and my players.我有单独的类来创建卡片、初始牌组和我的玩家。 If it would be helpful to show these classes I can post an update with more code.如果显示这些类会有所帮助,我可以发布更多代码的更新。 Thank you in advance.先感谢您。

(edited to add my player class) (编辑添加我的播放器类)

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 i think == comparision is good enough. @ScaryWombat 我认为 == 比较就足够了。 we want to compare the actual object reference.我们想比较实际的 object 参考。

@Shayla can you post the Player class to understand its methods? @Shayla 您可以发布播放器 class 以了解其方法吗? my suspision is around the.pop() method and isEmpty() method我的怀疑是围绕 .pop() 方法和 isEmpty() 方法

and i guess it would be better for debugging if you add the number of cards with each player after every war.我想如果你在每次战争后添加每个玩家的卡牌数量会更好地进行调试。

I didn't realize that because I used warRound in my if statement, I was calling the function again.我没有意识到因为我在if语句中使用了warRound ,所以我再次调用了 function。 I instead set a variable for the result.我改为为结果设置一个变量。 ie, IE,

roundResult = warRound(player1, player2, p1List, p2List);

and then I used the variable in my if statements instead, like this:然后我在if语句中使用了该变量,如下所示:

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

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

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