-7

The directions for the assignment are: Program a game of War. In War, if the value of your card is greater than your opponent's card, you earn one point. If your opponent's card is greater than your card, your opponent gets a point. If you tie it is a war! That means the next hand is worth 2 points. If it is another tie, then the next hand is worth 4 points, and you keep adding 2 until the tie is broken. Then the scoring goes back to normal.

1) You should use your Cards class to get 2 playing cards.

2) Ask the user how many rounds they would like to play.

3) Have a loop to catch if they enter a negative number of rounds.

4) End the program if they want to play 0 rounds.

5) Print the user's car and the computer's card for each round.

6) Print out the correct score after each round.

7) Aces are LOW (Worth 1 point).

8) After all the rounds are complete, print a message that tells the user if they won, lost, or tied.

I have the driver class ready (see below). I need help creating a class that will do the stuff stated above. After each round it should ask the user to press enter to go on to the next round. Here is my driver class code, it provides a card number and suit.

public class Cards
{

    private int A;
    private int B;
    private String c;
    private String b;

    public Cards()
    {    
         getSuit();
         getCardName();
         suit();
         name();
    }

    public int getSuit()
    {
        A = (int) (Math.random()*4+1); //rolls a number between 1 and 4
        return A;
    }

    public int getCardName()
    {
        B = (int) (Math.random()*13+1); //rolls a number between 1 and 13
        return B;
    }

    public String suit()
    {
        if (A == 1)  b = "Hearts";
        else if (A == 2) b = "Diamonds";
        else if (A == 3) b = "Clubs";
        else  b = "Spades";

        return b;
    }

    public String name()
    {
        if (B == 1)  c = "Ace";
        else if (B == 2) c = "2";
        else if (B == 3) c = "3";
        else if (B == 4) c = "4";
        else if (B == 5) c = "5";
        else if (B == 6) c = "6";
        else if (B == 7) c = "7";
        else if (B == 8) c = "8";
        else if (B == 9) c = "9";
        else if (B == 10) c = "10";
        else if (B == 11) c = "Jack";
        else if (B == 12) c = "Queen";
        else  c = "King";

        return c;
    }

    public String toString()
    {
        return c + " of " + b;
    }
}
Bryan
  • 1
  • 1
  • 2
    That's a huge ask for us. You should consult with your instructor on guidance on what to do here as opposed to us. Or, you should look into hiring a tutor to help you with this part. Don't expect either party to do all of the work for you, though (which is the impression you've left me with). – Makoto Nov 27 '18 at 17:12
  • 2
    Please see: [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – EJoshuaS - Reinstate Monica Nov 27 '18 at 17:22
  • You're making this unnecessarily difficult on yourself. Why not create classes, like 'Card' and such which handle all of this, rather than trying to make it in one main class? Make instances of 'Cards' – Frontear Nov 27 '18 at 17:28

2 Answers2

0

I would like to offer a review of your code, to hopefully help you improve it.

  • The variable names A/B/c/b do not offer any insight into what they represent. I am left guess what they are, as there are no comments either.
  • The getSuit() and getCardName() cause side effects. Calling getCardName() causes the card's number to change. This is not great in that most developers expect a getter method to not alter the object. For example, the following code would be rather confusing:

    card.name();   // "Ace"
    card.name();   // "Ace" 
    card.getCardName();  // 10. Should be 1???
    card.name();   // "10". Huh?
    
    • It would probably be better to just set the card's suit and number in the constructor. At the very least, set the methods to private.

    • The suit() and name() methods return a string. There is no need to save the value into class fields b and c, except where you are using them in the toString() method, which can just be rewritten as:

      return name() + " of " + suit();
      

      This will reduce your duplication of data.

    • You might consider generating (and storing!) your cards in a loop, rather than randomly generated, as currently both players might be able to draw the exact same card. While this might not be against the requirements, I would not expect both players to draw the same card in a game of war.

    • getCardName() is poorly named. getCardValue() might be better, but as above, it should not alter the current value.
Jamie
  • 1,786
  • 1
  • 16
  • 20
-1

I have the class written, it should look something like this:

import java.util.Scanner;

public class WarGame extends Cards {
    public static void main(String args[]) {
        boolean playing = true;
        int myScore = 0;
        int oppsScore = 0;
        int round = 0;
        int increment = 1;
        while (playing) {
            Scanner scan = new Scanner(System.in);
            int input = scan.nextInt();
            if (input == 0 || input < 0)
            {
                System.out.println("INVALID ROUND NUMBER ENTERED!");
                playing = false;
            }
            while (round <= input)
            {
                Cards myCard = new Cards();
                Cards oppsCard = new Cards();
                System.out.println("You have the " + myCard.toString());
                System.out.println("The computer has the " + oppsCard.toString());
                if (myCard.getSuit() > oppsCard.getSuit())
                {
                    myScore += 1;
                }
                else if (oppsCard.getSuit() > myCard.getSuit())
                {
                    oppsScore += 1;
                }
                else
                {
                    increment *= 2;
                    System.out.println("WAR!!!!");
                }
                System.out.println("Your score: " + myScore);
                System.out.println("Computer's score: " + oppsScore);
            }
        }
        if (myScore > oppsScore) {
            System.out.println("You win!");
        } else if (myScore < oppsScore) {
            System.out.println("You lose!");
        } else {
            System.out.println("It's a tie!");
        }
    }
}
Zach Pedigo
  • 224
  • 2
  • 9
  • 2
    We are not a code-writing service. We don't just hand out code to users, especially when they won't understand. You are jeopardizing OP by giving them code they might not understand, and if they get caught using it, could face severe repercussions. – Frontear Nov 27 '18 at 17:37