Java-programma – The Card Game WAR

Dit is een klassikale opdracht, ik vraag niemand om de opdracht voor mij te DOEN, maar ik heb eerder mijn best gedaan voordat ik hier post in de hoop hulp te krijgen bij mijn 4 fouten die ik ontvang en dat mijn dek wordt om de een of andere reden niet geschud.

Mijn opdrachtaanwijzingen:

Voor deze opdracht ga je een programma maken dat een eenvoudig oorlogsspel speelt. In dit spel krijgt elke speler een kaart van het volledige kaartspel. Wie de kaart met de hoogste waarde heeft, wint. Als de kaarten die gedeeld worden dezelfde waarde hebben, dan is het gelijkspel en wint geen van beide spelers. De speler die de meeste rondes wint, wint het spel. Er is geen input nodig van de spelers (niet erg interessant!). U moet de kaarten afdrukken die elke speler is
gedeeld en het resultaat van die ronde en het uiteindelijke resultaat van het spel. Misschien wilt u gebruikersinvoer gebruiken om een vertraging tussen elke ronde te implementeren.

Mijn kaartklasse:

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 (waar ik een shuffle-fout heb):

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

Mijn War Game-hoofdprogramma:

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

Mijn fouten zijn als volgt:

----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.

Heel erg bedankt voor alle hulp die kan worden aangeboden.

PLAYER1 EN PLAATER2 Raadpleeg de kaarten Elke speler wordt elke ronde gegeven


1, Autoriteit 100%

SPELERS1 EN PLAYER2 zijn inten en niet gehele getallen (wat een Java-klasse is) . Dat betekent dat ze geen object zijn en geen methoden op hen hebben. Je kunt ze rechtstreeks vergelijken als “Sam I Am” zei.

U kunt ze ook als gehele getallen werpen:

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

Maar dit is vrij nutteloos omdat de vergelijking die je maakt geen van de integer-methoden nodig heeft.

Dus gebruik gewoon :

if (player1 > player2)

Je kunt dit onderwerplezen voor meer informatie over autoboxing van gehele getallen.


Antwoord 2, autoriteit 50%

// 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

3

Ik ben half raden, maar ik denk niet dat je moet bellen getValue()op je ints

Een verklaring zoals

if (player1 > player2)

is perfect goed, ervan uitgaande dat zowel player1en player2INTS

zijn


Antwoord 4

Voor de shuffle-methode zou je deze code kunnen gebruiken:

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

Other episodes