Back to 111 Main Page

Mobius strip

Assignment 10

Task

Write a program that plays a single round of the card game War.

Textbook: 3.5, (3.4)
Concepts: Math.random(), review of conditionals and methods.

Steps

Since there is no strategy involved in War, you will not need to ask the user for input. Instead, you will randomly generate two playing card values--one for the player, and one for the computer. Then, you will compare the value of the two cards, announcing whether the player won, whether the player lost, or whether the game was a draw. (In the case of a draw, that's still the end of the game--we're not implementing a full War face-off here.)

Program and rule requirements

  • Although you must print the suit, cards are ranked by the their value only. (So ties are possible.)
  • Aces are high. (They have a higher value than Kings.) Otherwise, rank order is as normal for playing cards.
  • Do not ask for any user input.
  • You must define and call at least one method besides main.

Output format

  • Exactly two lines of your output should contain a colon (:) chararacter.
  • The text before the colons is the names or labels of the players. These can be whatever you want: "Player 1" and "Player 2", "You" and "Me", "Human" and "Computer", or names taken from some other source.
  • The text after the colon is the card that player drew. You can either describe this in full: "10 of Spades", "2 of Hearts", "King of Clubs", "Ace of Diamonds". Or you can use an abbreviation, such as "10S", "2H", "KC", "AD". Or you can use some other scheme. However, card values 2 through 10 must be represented using numbers; the other values must be by word or character (J, Q, K, A). Card and suit names must be capitialized, and the suit must be listed after the value.
  • On a third, separate line, you must display the outcome. The outcome must be stated relative to the first listed player. That is, you'll say if that first player WON, LOST, or TIED. (Other allowed keywords: "win", "wins", "lose", "tie", "draw"). Case does not matter.

Sample Output

D:\TA\grading\A10>java ZtomaszeA10
A single round of War...
Human: 5 of Spades
Computer: 3 of Clubs
You WON!

D:\TA\grading\A10>java ZtomaszeA10
A single round of War...
Human: 10 of Hearts
Computer: Jack of Hearts
You LOST.

D:\TA\grading\A10>java ZtomaszeA10
A single round of War...
Human: Ace of Spades
Computer: 5 of Hearts
You WON!

D:\TA\grading\A10>java ZtomaszeA10
A single round of War...
Human: 3 of Spades
Computer: 3 of Diamonds
The game was a TIE.

Hints

  • To produce a random card, you'll have to generate a random number. I recommend producing one random number for the value, and then one for the suit. You'll have to manually translate from a number to a String in some cases to print the card's name.
  • You must write at least one additional method, but you may write more than one if you like. A handy method would be one that generates and prints a playing card and returns its value as a number for easy comparison in main.

What to Submit

Upload your UsernameA10.java file to Tamarin.

Grading [8 points]

1 - Compiles + Coding Standards
Your program compiles successfully (no errors). Your code follows Java coding standards.
3 - Cards
Cards are generated randomly (1.5). Cards are printed and labelled according to the output format specifications given above (1.5).
2 - Result
You correctly print whether the first player won, lost, or tied.
2 - Method
You have at least one more method beyond main (that you actually call from within main).

FAQs

How do I define a separate method? What should be in it?
You're defining a separate method the same way you did in A09. As hinted above, a handy method would be one that generates and prints a playing card and returns its value as a number for easy comparison in main. So, you could define a method like this:
public static int printCard() {
  //randomly generate a suit and a card value
  //use a switch or if/else-if block to translate from the numbers
  //  to a string, printing out the card to the screen
  //return the value of this single card (so you can compare them in main)
}
Then, in your main method, you can have something like this:
  System.out.print("Human: ");
  int humanCardValue = printCard();
  System.out.print("Computer: ");
  int computerCardValue = printCard();
See, the printCard method, as defined above, would both print out the card to the screen and return the value of the card it generated/printed. This lets you reuse the same code to produce each player's card. Then you just need to compare the two values in main to see who won.


~ztomasze Index : TA Details: ICS111: A10
http://www2.hawaii.edu/~ztomasze
Last Edited: 23 Feb 2009
©2008 by Z. Tomaszewski.