Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Program - The Card Game WAR

This is a class assignment, I am not asking for anyone to DO the assignment for me but rather I have attempted to my best ability before posting on here in hopes of receiving some help on my 4 errors I am receiving and that my deck is not being shuffled for some reason.

My Assignment Directions:

For this assignment, you will create a program that plays a simple game of War. In this game, each player is dealt a card from the full deck. Whoever has the card with the highest value wins. If the cards that are dealt have the same value, then it is a tie and neither player wins. The player that wins the most rounds wins the game. There is no input required from the players (not very interesting!). You should print the cards that each player is dealt and the result of that round and the final result of the game. You may want to use user input to implement a delay between each round.

My Card Class:

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(where I have a shuffling error):

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

My War Game Main Program:

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

    } 
}

My Errors Are As Follows:

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

Thank You So Much for any Help that can be offered.

player1 and player2 refer to the cards each player is given each round

like image 387
user2157845 Avatar asked Apr 21 '26 21:04

user2157845


1 Answers

player1 and player2 are ints and not Integers (which is a Java Class). That means they are not object and don't have methods on them. You can compare them directly as "Sam I Am" said.

You can also cast them as Integers :

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

But this is pretty useless since the comparison you are doing do not need any of the integer methods.

So just use :

if (player1 > player2)

You can read this topic to learn more about autoboxing of Integers.

like image 60
Hugo Dozois Avatar answered Apr 24 '26 11:04

Hugo Dozois