Question
This is to implement the WarGame that discussed in the lecture. Take the WarGame.java from D2L and complete the code. Do NOT change main, printRoundInfo
-
This is to implement the WarGame that discussed in the lecture. Take the WarGame.java from D2L and complete the code. Do NOT change main, printRoundInfo or printRoundResult methods. You can add more methods if it is needed. Provide internal and external documentation including the design diagram.
-
In this game, in each round the program asks a number from user that stand for a card. It also randomly selects a card for computer. Then it declares the cards that user and computer have chosen followed by the winner. The card with higher rank is the winner and gets one score. The suit of card doesnt matter for winning. If they are tie, the users total score must be doubled. The program must be executed 10 rounds and then declares the total score of the user.
-
Ace, Jack, Queen and King must be considered as 1, 11, 12 and 13 respectively.
-
The following is part of output for one run. The highlighted data is user inputs.
-
In the main method to call genInput method use the name of class and dot operator, i.e: replace comp = genInput(); (line8) with comp = WarGame.genInput(); and compile and run the program. What happens?
-
Now remove static modifier in the getInput methods header and compile the
program. What happens? Justify the result.
Source code:
import java.util.Scanner; public class WarGame{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); final int ROUND = 10; int user, comp, score = 0; for (int r=1; r < ROUND +1; ++r) { comp = genInput(); user = getInput(scan); printRoundInfo(r, user, comp); score = printRoundResult(score, user, comp); } System.out.println("End of Game!"); scan.close(); }
public static int getInput(Scanner input){ // TODO: prompt user to enter a number between 1 and 52, // then validate and return it }
public static int genInput(){ //TODO : generate and return a random integer number between 1 and 52 }
public static void printRoundInfo(int r, int user, int comp){ System.out.print("R" + r + ": "); System.out.print("Computer card is " + getRank(comp) + " of " + getSuit(comp)); System.out.println("; User card is " + getRank(user) + " of " + getSuit(user)); }
public static int printRoundResult(int score, int user, int comp){ int winner = findWinner(user, comp); switch (winner){ case 0 : score *=2; System.out.println("Its tie user score is " + score); break; case 1 : score +=1; System.out.println("User wins user score is " + score); break; default : System.out.println("Computer wins user score is " + score); break; } return score; }
public static int findWinner(int user, int comp){ //TODO: find the winner and return -1, 1 or 0 if it is tie, //user wins or computer wins }
// add more methods if needed. }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started