Question: I need to write a java code for war game using Deck Class posted below. // // Deck - This file contains the data definitions
I need to write a java code for war game using Deck Class posted below.
// // Deck - This file contains the data definitions and the methods for the Deck class. // import java.io.*; import java.util.Collection; import java.util.Random; class Deck { private int mReshuffle; private int mRemaining; private String mCards[]; // // Deck() - this is the constructor for the class; // public Deck(int _reshuffle) { mCards = new String[52]; // set up a list of 52 items that contains a code for // ... each card in the deck... // ... example: 5 D is a Five of Diamonds mCards[0] = "01 Diamond"; mCards[1] = "01 Heart"; mCards[2] = "01 Club"; mCards[3] = "01 Spade"; mCards[4] = "02 Diamond"; mCards[5] = "02 Heart"; mCards[6] = "02 Club"; mCards[7] = "02 Spade"; mCards[8] = "03 Diamond"; mCards[9] = "03 Heart"; mCards[10] = "03 Club"; mCards[11] = "03 Spade"; mCards[12] = "04 Diamond"; mCards[13] = "04 Heart"; mCards[14] = "04 Club"; mCards[15] = "04 Spade"; mCards[16] = "05 Diamond"; mCards[17] = "05 Heart"; mCards[18] = "05 Club"; mCards[19] = "05 Spade"; mCards[20] = "06 Diamond"; mCards[21] = "06 Heart"; mCards[22] = "06 Club"; mCards[23] = "06 Spade"; mCards[24] = "07 Diamond"; mCards[25] = "07 Heart"; mCards[26] = "07 Club"; mCards[27] = "07 Spade"; mCards[28] = "08 Diamond"; mCards[29] = "08 Heart"; mCards[30] = "08 Club"; mCards[31] = "08 Spade"; mCards[32] = "09 Diamond"; mCards[33] = "09 Heart"; mCards[34] = "09 Club"; mCards[35] = "09 Spade"; mCards[36] = "10 Diamond"; mCards[37] = "10 Heart"; mCards[38] = "10 Club"; mCards[39] = "10 Spade"; mCards[40] = "11 Diamond"; mCards[41] = "11 Heart"; mCards[42] = "11 Club"; mCards[43] = "11 Spade"; mCards[44] = "12 Diamond"; mCards[45] = "12 Heart"; mCards[46] = "12 Club"; mCards[47] = "12 Spade"; mCards[48] = "13 Diamond"; mCards[49] = "13 Heart"; mCards[50] = "13 Club"; mCards[51] = "13 Spade"; mReshuffle = _reshuffle; if ( mReshuffle < 1 || mReshuffle > 51 ) mReshuffle = 1; mRemaining = 52; } // // Deal() - this method deals one card // public String Deal() { String theCard = "X"; int needCard, cardNum, huh; Random rand = new Random(); // are there cards remaining in the deck? // System.out.println("mRemaining: " + mRemaining + " mReshuffle: " + mReshuffle); if ( mRemaining > mReshuffle ) { // yes, there are cards remaining in the deck... needCard = 1; // loop, looking for an item in the list (card in the deck) // ... which has not yet been picked... while ( needCard == 1 ) { cardNum = rand.nextInt(51); // huh = rand.nextInt(51); if ( mCards[cardNum] != "0" ) { // this card has not yet been picked... theCard = mCards[cardNum]; mCards[cardNum] = "0"; // mark card as read needCard = 0; // we can get out of this loop mRemaining -= 1; // decrement remaining cards count } } // end while loop } // end if remaining... // System.out.println("Card: " + theCard + " enter an int..."); // huh = InputUtils.GetInt(); return(theCard); } } ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.io.*; import java.util.Scanner; public class WarProgram { public static void main(String[] args) { int card1Num = 0; int cardNum = 0; String thisCard = "notX"; String card1 = "kjg"; String something = "kjg"; int playerScore = 0; int compScore = 0; String thisCard1 = "notX"; String card2 = "kjg"; System.out.println("You are playing against Computer now. Good luck, Human! Press Enter to continue. "); Scanner keyboard = new Scanner(System.in); keyboard.nextLine(); Deck thisDeck = new Deck(52); while ( thisCard != "X" && card1 != "X") { thisCard = thisDeck.Deal(); card1 = thisDeck.Deal(); cardNum++; card1Num++; if ( thisCard != "X" && card1 != "X" ) { thisCard1 = thisCard.substring(6,thisCard.length()); card2 = card1.substring(6,card1.length()); System.out.println(" Computer gets: " + thisCard1); System.out.println("Human gets: " + card2); int player = Integer.parseInt(card1.substring(0,2)); int computer = Integer.parseInt(thisCard.substring(0,2)); if (computer > player) { System.out.println("Computer Wins The War!" ); compScore++; System.out.println("Computer Scores: " + compScore + " Player Scores: " + playerScore); } if (player > computer) { System.out.println("Human Wins The War!" ); playerScore++; System.out.println("Player Scores: " + playerScore + " Computer Scores: " + compScore); } if ( player == computer) System.out.println("The War Is A Tie"); System.out.println("Press Enter to see the next battle..."); something = InputUtils.GetStr(); } } } } ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Using the Deck Class above, I need to create a loop in WarProgram Class where when player one wins the card, both player one and player two cards will be placed in player one deck, and the game will keep going until one player runs out of all the cards.
The program also needs to declare a war when both player gets same card and when war is declared, players have to place one card facing down and the second card facing up. Whoever gets the highest number will win all those 6 cards.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
