Java程序 - 纸牌游戏WAR

时间:2013-03-11 17:45:29

标签: java arrays class compiler-errors

这是一个课堂作业,我不是要求任何人为我做作业,而是我在发布之前尝试了我的最佳能力,希望能收到我收到的4个错误的帮助,以及我的由于某种原因,甲板没有被洗牌。

我的作业指示:

对于此作业,您将创建一个播放简单战争游戏的程序。在这个游戏中,每个玩家都会从完整的牌组中获得一张牌。谁拥有最高价值的卡赢了。如果发牌的牌具有相同的价值,那么这是一个平局而且两位玩家都不会获胜。赢得最多回合的玩家赢得比赛。玩家无需输入(不是很有趣!)。你应该打印每个玩家的牌 处理了该轮的结果和比赛的最终结果。您可能希望使用用户输入来实现每轮之间的延迟。

我的卡类:

public class Card {

    private int cardNum;
    final static String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
    final static String[] ranks = {"2", "3","4","5","6","7","8", "9","10", "Jack", "Queen", "King", "Ace"};

    Card (int theCard) {
        setCardNum (theCard);
    }

    public void setCardNum (int theCard) {
        cardNum = (theCard >= 0 && theCard <= 51)? theCard: 0;
    }

    public int getCardNum() {
        return cardNum;
    }

    public String toString() {
        return ranks[cardNum%13] + " of " + suits[cardNum/13];
    }

    public String getSuit() {
        return suits[cardNum/13];
    }

    public String getRank() {
        return ranks[cardNum%13];
    }

    public int getValue() {
        return cardNum%13;
    } 
}

My Deck Class(我有一个改组错误):

public class Deck {

    private Card[] deck = new Card[52];
    private int topCard;

    Deck() {

        topCard = 0;

        for (int i = 0; i < deck.length; i++)
            deck[i] = new Card(i);

    }

    public void shuffle() {

        topCard = 0;

        for (int i = 0; i < 1000; i++) {
            int j = (int)(Math.random()*52);
            int k = (int)(Math.random()*52);
            Card tmpCard = deck[j];
            deck[j] = deck[k];
            deck[k] = tmpCard;
        } 
   }

    public Card dealCard() {
        Card theCard;
        if (topCard < deck.length) {
            theCard = deck[topCard];
            topCard++;
        }
        else
            theCard = null;

        return theCard;
    }
}

我的战争游戏主程序:

import java.util.Scanner;

public class WarGame {

    public static void main(String[] args) {

        Card[][] hands = new Card[2][1];
        Deck myDeck = new Deck();

        for (int i = 0; i < 53; i++) {
            System.out.printf("\n Round %s of The War \n", i);

            for (int c = 0; c < 1; c++)
                for (int player = 0; player < hands.length; player++)
                    hands[player][c] = myDeck.dealCard();

            for (int player = 0; player < hands.length; player++) {
                System.out.printf("Player %d: ", player);
                printHand(hands[player]);

                int player1;
                int player2;

                if (player1.getValue() > player2.getValue())
                    System.out.println("Player One Wins The War");
                else if (player2.getValue() > player1.getValue())
                    System.out.println("Player Two Wins The War");
                else
                    System.out.println("The War Is A Tie");

            } 
        }
    }

    public static void printHand(Card[] hand) {

        for (int card = 0; card < hand.length; card++)
            System.out.printf("%s", hand[card].toString());

        System.out.println();

    } 
}

我的错误如下:

----jGRASP exec: javac -g WarGame.java

WarGame.java:31: error: int cannot be dereferenced
if (player1.getValue() > player2.getValue())
       ^
WarGame.java:31: error: int cannot be dereferenced
if (player1.getValue() > player2.getValue())
                            ^
WarGame.java:35: error: int cannot be dereferenced
else if (player2.getValue() > player1.getValue())
            ^
WarGame.java:35: error: int cannot be dereferenced
else if (player2.getValue() > player1.getValue())
                                 ^
4 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

非常感谢您提供的任何帮助。

player1和player2指每位玩家每轮给出的牌

5 个答案:

答案 0 :(得分:2)

player1和player2是ints而不是Integers(这是一个Java类)。这意味着它们不是对象,也没有方法。您可以直接将它们比作“Sam I Am”所说的。

您也可以将它们转换为整数:

if( (new Integer(player1)).compareTo(new Integer(player2)) > 0 ) 

但是这很无用,因为您正在进行的比较不需要任何整数方法。

所以只需使用:

if (player1 > player2)

您可以阅读this topic以了解有关整数自动装箱的详情。

答案 1 :(得分:1)

// import needed classes and packages
import java.util.Scanner;
import java.text.NumberFormat;
import java.io.IOException;
import java.util.Locale;
import java.text.DecimalFormat;

public class Program2
{public static void main(String[] args) throws IOException
    {
    //Declare Variables
    //Creates the cards based on their suite
    Card heartCard;
    Card diamondCard;
    Card spadeCard;
    Card clubCard;
    int countingPlays = 0;


    Scanner keyboard = new Scanner(System.in); //Allows Input

    //creates the cardPile array called DeckOfCards
    CardPile deckOfCards = new CardPile();

    //Creates Player1's Card Pile
    CardPile player1Pile = new CardPile();

    //Creates Player2's Card Pile
    CardPile player2Pile = new CardPile();

    //Creates the cards to fill the array (1-14 of hearts/diamonds/spades/clubs).
    for(int i = 2; i < 15; i++)
    {
        char heart = '\u0003';
        char diamond ='\u0004';
        char spade = '\u0005';
        char club = '\u0006';

        deckOfCards.add(heartCard = new Card(heart, i));
        deckOfCards.add(diamondCard = new Card(diamond, i));
        deckOfCards.add(spadeCard = new Card(spade, i));
        deckOfCards.add(clubCard = new Card(club, i));
    }

    //prints out the deck of Cards
    System.out.println("Deck Of Cards: " + deckOfCards);

    //shuffles the cards
    deckOfCards.shuffle();

    //Prints out the deck of cards after they are shuffled
    System.out.println("Deck of Cards after shuffled: " + deckOfCards);

    //Checking the size of the Deck
    System.out.println("" + deckOfCards.size());

    //Takes the deckOfCards and splits them up into 2 piles for Player1 and Player2
    for(int i = 0; i < 26; i++)
    {
        player1Pile.add(deckOfCards.getTopCard());
        player2Pile.add(deckOfCards.getTopCard());
    }

    //Prints out the deck of Cards and then the player 1's pile and player 2's pile

    System.out.println("Player 1's Cards: " + player1Pile);
    System.out.println("Player 2's Cards: " + player2Pile);

    //checking the size of each players Pile


    //Prints the header for the game
    System.out.println("Lets have a war!!!");


    //Testing tricky spot where the getTopCard removes a the topcard
    /*
    Card removedTopCard = player1Pile.getTopCard();
    System.out.println("Getting player1Pile: " + removedTopCard);
    player1Pile.add(removedTopCard);
    System.out.println("Player1Pile is " + player1Pile);
    System.out.println("Player1Pile size is " +player1Pile.size());
    */

    //Starts the game of War
    try
    {   //do while the sizes of the player piles are greater than 0.
        do
        {
            //gets the top cards of each players Pile
            Card player1RemovedTopCard = player1Pile.getTopCard();
            Card player2RemovedTopCard = player2Pile.getTopCard();

            //Compares the 2 cards to test which is bigger. If player 1's card is smaller than player 2 is the winner
            if(player1RemovedTopCard.compareTo(player2RemovedTopCard) < player2RemovedTopCard.compareTo(player1RemovedTopCard))
            {
                System.out.println("Player 1: " + player1RemovedTopCard + " Player 2: " + player2RemovedTopCard);
                System.out.println("Player 2 is the Winner");
                player2Pile.add(player1RemovedTopCard);
                player2Pile.add(player2RemovedTopCard);
                System.out.println("Player 1 has: " + player1Pile.size() + " cards left.");
                System.out.println("Player 2 has:" + player2Pile.size() + " cards left.");
                System.out.println("\n");
                keyboard.nextLine();
            }
            //Compares the 2 cards to test which is bigger. If player 2's card is smaller than player 1 is the winner.
            else if(player1RemovedTopCard.compareTo(player2RemovedTopCard) > player2RemovedTopCard.compareTo(player1RemovedTopCard))
            {
                System.out.println("Player 1: " + player1RemovedTopCard + " Player 2: " + player2RemovedTopCard);
                System.out.println("Player 1 is the Winner");
                player1Pile.add(player1RemovedTopCard);
                player1Pile.add(player2RemovedTopCard);
                System.out.println("Player 1 has: " + player1Pile.size() + " cards left.");
                System.out.println("Player 2 has:" + player2Pile.size() + " cards left.");
                System.out.println("\n");
                keyboard.nextLine();
            }
            //Else it is a war
            else
            {
                System.out.println("Player 1: " + player1RemovedTopCard + " Player 2: " + player2RemovedTopCard);
                System.out.println("WAR!!!!!!!");
                //War if the player has only 4 cards left.
                if(player1Pile.size() == 1 || player2Pile.size() == 1)
                {
                    Card player1RemovedTopCard2 = player1Pile.getTopCard();

                    Card player2RemovedTopCard2 = player2Pile.getTopCard();
                    System.out.println("Player1's 2nd card is: " + player1RemovedTopCard2 + " Player2's 2nd card is: " + player2RemovedTopCard2);
                    if(player1RemovedTopCard2.compareTo(player2RemovedTopCard2) > player2RemovedTopCard2.compareTo(player1RemovedTopCard2))
                    {
                        System.out.println("Player 1 is the winner of the War! ");
                        player1Pile.add(player1RemovedTopCard);
                        player1Pile.add(player1RemovedTopCard2);
                        player1Pile.add(player2RemovedTopCard);
                        player1Pile.add(player2RemovedTopCard2);
                        System.out.println("Player 1 has: " + player1Pile.size() + " cards left.");
                        System.out.println("Player 2 has: " + player2Pile.size() + " cards left.");
                        System.out.println("\n");
                        keyboard.nextLine();
                    }
                    else if(player1RemovedTopCard2.compareTo(player2RemovedTopCard2) < player2RemovedTopCard2.compareTo(player1RemovedTopCard2))
                    {
                        System.out.println("Player 2 is the winner of the War! ");
                        player2Pile.add(player1RemovedTopCard);
                        player2Pile.add(player1RemovedTopCard2);
                        player2Pile.add(player2RemovedTopCard);
                        player2Pile.add(player2RemovedTopCard2);
                        System.out.println("Player 1 has: " + player1Pile.size() + " cards left.");
                        System.out.println("Player 2 has: " + player2Pile.size() + " cards left.");
                        System.out.println("\n");
                        keyboard.nextLine();
                    }
                    else
                    {
                        if(player2Pile.size() == 0)
                        {
                            player1Pile.add(player2RemovedTopCard2);
                            player1Pile.add(player2RemovedTopCard);
                            player1Pile.add(player1RemovedTopCard2);
                            player1Pile.add(player1RemovedTopCard);
                        }
                        else
                        {
                            player2Pile.add(player2RemovedTopCard2);
                            player2Pile.add(player2RemovedTopCard);
                            player2Pile.add(player1RemovedTopCard2);
                            player2Pile.add(player1RemovedTopCard);
                        }
                    }
                }
                //War if the player has only 2 cards left.
                else if(player1Pile.size() == 2 || player2Pile.size() == 2)
                {
                    Card player1RemovedTopCard2 = player1Pile.getTopCard();
                    Card player1RemovedTopCard3 = player1Pile.getTopCard();

                    Card player2RemovedTopCard2 = player2Pile.getTopCard();
                    Card player2RemovedTopCard3 = player2Pile.getTopCard();

                    do
                    {
                        System.out.println("Player1's 3rd card is: " + player1RemovedTopCard3 + " Player2's 3rd card is: " + player2RemovedTopCard3);
                        if(player1RemovedTopCard3.compareTo(player2RemovedTopCard3) > player2RemovedTopCard3.compareTo(player1RemovedTopCard3))
                        {
                            System.out.println("Player 1 is the winner of the War! ");
                            player1Pile.add(player1RemovedTopCard);
                            player1Pile.add(player1RemovedTopCard2);
                            player1Pile.add(player1RemovedTopCard3);
                            player1Pile.add(player2RemovedTopCard);
                            player1Pile.add(player2RemovedTopCard2);
                            player1Pile.add(player2RemovedTopCard3);
                            System.out.println("Player 1 has: " + player1Pile.size() + " cards left.");
                            System.out.println("Player 2 has: " + player2Pile.size() + " cards left.");
                            System.out.println("\n");
                            keyboard.nextLine();
                        }
                        else if(player1RemovedTopCard3.compareTo(player2RemovedTopCard3) < player2RemovedTopCard3.compareTo(player1RemovedTopCard3))
                        {
                            System.out.println("Player 2 is the winner of the War! ");
                            player2Pile.add(player1RemovedTopCard);
                            player2Pile.add(player1RemovedTopCard2);
                            player2Pile.add(player1RemovedTopCard3);
                            player2Pile.add(player2RemovedTopCard);
                            player2Pile.add(player2RemovedTopCard2);
                            player2Pile.add(player2RemovedTopCard3);
                            System.out.println("Player 1 has: " + player1Pile.size() + " cards left.");
                            System.out.println("Player 2 has: " + player2Pile.size() + " cards left.");
                            System.out.println("\n");
                            keyboard.nextLine();
                        }
                    //Continues the war if the top card at the end of the war are still equal
                    }while(player1RemovedTopCard3.compareTo(player2RemovedTopCard3) == player2RemovedTopCard3.compareTo(player1RemovedTopCard3));
                }
                //War if the player has only 3 cards left.
                else if(player1Pile.size() == 3 || player2Pile.size() == 3)
                {
                    Card player1RemovedTopCard2 = player1Pile.getTopCard();
                    Card player1RemovedTopCard3 = player1Pile.getTopCard();
                    Card player1RemovedTopCard4 = player1Pile.getTopCard();

                    Card player2RemovedTopCard2 = player2Pile.getTopCard();
                    Card player2RemovedTopCard3 = player2Pile.getTopCard();
                    Card player2RemovedTopCard4 = player2Pile.getTopCard();

                    do
                    {
                        System.out.println("Player1's fourth card is: " + player1RemovedTopCard4 + " Player2's fourth card is: " + player2RemovedTopCard4);
                        if(player1RemovedTopCard4.compareTo(player2RemovedTopCard4) > player2RemovedTopCard4.compareTo(player1RemovedTopCard4))
                        {
                            System.out.println("Player 1 is the winner of the War! ");
                            player1Pile.add(player1RemovedTopCard);
                            player1Pile.add(player1RemovedTopCard2);
                            player1Pile.add(player1RemovedTopCard3);
                            player1Pile.add(player1RemovedTopCard4);
                            player1Pile.add(player2RemovedTopCard);
                            player1Pile.add(player2RemovedTopCard2);
                            player1Pile.add(player2RemovedTopCard3);
                            player1Pile.add(player2RemovedTopCard4);
                            System.out.println("Player 1 has: " + player1Pile.size() + " cards left.");
                            System.out.println("Player 2 has: " + player2Pile.size() + " cards left.");
                            System.out.println("\n");
                            keyboard.nextLine();
                        }
                        else if(player1RemovedTopCard4.compareTo(player2RemovedTopCard4) < player2RemovedTopCard4.compareTo(player1RemovedTopCard4))
                        {
                            System.out.println("Player 2 is the winner of the War! ");
                            player2Pile.add(player1RemovedTopCard);
                            player2Pile.add(player1RemovedTopCard2);
                            player2Pile.add(player1RemovedTopCard3);
                            player2Pile.add(player1RemovedTopCard4);
                            player2Pile.add(player2RemovedTopCard);
                            player2Pile.add(player2RemovedTopCard2);
                            player2Pile.add(player2RemovedTopCard3);
                            player2Pile.add(player2RemovedTopCard4);
                            System.out.println("Player 1 has: " + player1Pile.size() + " cards left.");
                            System.out.println("Player 2 has: " + player2Pile.size() + " cards left.");
                            System.out.println("\n");
                            keyboard.nextLine();
                        }
                    //Continues the war if the top card at the end of the war are still equal
                    }while(player1RemovedTopCard4.compareTo(player2RemovedTopCard4) == player2RemovedTopCard4.compareTo(player1RemovedTopCard4));
                }
                //war if player has more than 4 cards
                else if(player1Pile.size() >= 4 || player2Pile.size() >= 4)
                {
                    Card player1RemovedTopCard2 = player1Pile.getTopCard();
                    Card player1RemovedTopCard3 = player1Pile.getTopCard();
                    Card player1RemovedTopCard4 = player1Pile.getTopCard();
                    Card player1RemovedTopCard5 = player1Pile.getTopCard();

                    Card player2RemovedTopCard2 = player2Pile.getTopCard();
                    Card player2RemovedTopCard3 = player2Pile.getTopCard();
                    Card player2RemovedTopCard4 = player2Pile.getTopCard();
                    Card player2RemovedTopCard5 = player2Pile.getTopCard();
                    do
                    {
                        System.out.println("Player1's 5th card is: " + player1RemovedTopCard5 + " Player2's 5th card is: " + player2RemovedTopCard5);
                        if(player1RemovedTopCard5.compareTo(player2RemovedTopCard5) > player2RemovedTopCard5.compareTo(player1RemovedTopCard5))
                        {
                            System.out.println("Player 1 is the winner of the War! ");
                            player1Pile.add(player1RemovedTopCard);
                            player1Pile.add(player1RemovedTopCard2);
                            player1Pile.add(player1RemovedTopCard3);
                            player1Pile.add(player1RemovedTopCard4);
                            player1Pile.add(player1RemovedTopCard5);
                            player1Pile.add(player2RemovedTopCard);
                            player1Pile.add(player2RemovedTopCard2);
                            player1Pile.add(player2RemovedTopCard3);
                            player1Pile.add(player2RemovedTopCard4);
                            player1Pile.add(player2RemovedTopCard5);
                            System.out.println("Player 1 has: " + player1Pile.size() + " cards left.");
                            System.out.println("Player 2 has: " + player2Pile.size() + " cards left.");
                            System.out.println("\n");
                            keyboard.nextLine();
                        }
                        else if(player1RemovedTopCard5.compareTo(player2RemovedTopCard5) < player2RemovedTopCard5.compareTo(player1RemovedTopCard5))
                        {
                            System.out.println("Player 2 is the winner of the War! ");
                            player2Pile.add(player1RemovedTopCard);
                            player2Pile.add(player1RemovedTopCard2);
                            player2Pile.add(player1RemovedTopCard3);
                            player2Pile.add(player1RemovedTopCard4);
                            player2Pile.add(player1RemovedTopCard5);
                            player2Pile.add(player2RemovedTopCard);
                            player2Pile.add(player2RemovedTopCard2);
                            player2Pile.add(player2RemovedTopCard3);
                            player2Pile.add(player2RemovedTopCard4);
                            player2Pile.add(player1RemovedTopCard5);
                            System.out.println("Player 1 has: " + player1Pile.size() + " cards left.");
                            System.out.println("Player 2 has: " + player2Pile.size() + " cards left.");
                            System.out.println("\n");
                            keyboard.nextLine();
                        }
                    //Continues the war if the top card at the end of the war are still equal
                    }while(player1RemovedTopCard5.compareTo(player2RemovedTopCard5) == player2RemovedTopCard5.compareTo(player1RemovedTopCard5));
                }
            }
            //Adds to the plays that keep track of how many plays have been made
            countingPlays++;
            //. If there are 10 plays it shuffles and prints out a message that the cards have been shuffled.
            if(countingPlays >= 10)
            {
                player1Pile.shuffle();
                player2Pile.shuffle();
                System.out.println("Cards Shuffled");
                //resets the counter to 0
                countingPlays = 0;
            }
        //Continues the game of war while the players piles are bigger than 0
        }while(player1Pile.size() > 0 || player2Pile.size() > 0);
    }

    //Catches the Array 0 error and prints out who is the winner based on who has zero cards.
    catch (IndexOutOfBoundsException theException)  //tries to catch this type...
        {
            if(player1Pile.size() == 0)
            {
                System.out.println("Winner is Player 2" );
            }
            else
            System.out.println("Winner is Player 1" );

        }

    } //end of main
}//end of class

答案 2 :(得分:0)

我猜不到,但我认为您不需要在getValue()上致电int

这样的陈述
if (player1 > player2)
假设player1player2都是整数

完全没问题

答案 3 :(得分:0)

您需要在hands中获取Card对象的值。您目前实际上并未向player1player2分配任何内容。要获得卡片的价值,您需要在实际对象上调用.getValue(),例如int player1 = hands[0][0].getValue();

我更新了您的main功能,以演示其外观,并进行了一些其他可能会对您有所帮助的更改。

public static void main(String[] args) {

    Card[][] hands = new Card[2][1];
    Deck myDeck = new Deck();

    //reduced this to 26 iterations because two cards are dealt each iteration
    for (int i = 0; i < 26; i++) {
        System.out.printf("\n Round %s of The War \n", i);

        //You really don't need to loop just once here...
        //Simply assign the card to hands[player][0] since that's the only option
        //for (int c = 0; c < 1; c++)
            for (int player = 0; player < hands.length; player++)
                hands[player][0] = myDeck.dealCard();

        for (int player = 0; player < hands.length; player++) {
            System.out.printf("Player %d: ", player);
            printHand(hands[player]);
        }

        int player1 = hands[0][0].getValue(); //get the value from the Card object
        int player2 = hands[1][0].getValue();

        if (player1 > player2)
            System.out.println("Player One Wins The War");
        else if (player2 > player1)
            System.out.println("Player Two Wins The War");
        else
            System.out.println("The War Is A Tie");

    }
}

答案 4 :(得分:0)

对于随机播放方法,您可以使用以下代码:

public void shuffle()
{
    Collections.shuffle(deck);
}