Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with a java game of card program. The code should be written within the code in the picture. Directions are in the comment

Need help with a java game of card program. The code should be written within the code in the picture. Directions are in the comment code. Thanks.

import java.util.ArrayList;

import java.util.Scanner;

public class GameOfWar {

/**

*

* War

*

* Objective: Play until one player runs out of cards.

* Compare card values. The bigger value wins both cards.

* When there is a tie in the card value, decide based on suit.

* Winning a tie hand results in gaining two more of the opponents cards.

*

* Tie Game:

* Use suit ranking

* Ascending alphabetical order: clubs (lowest), followed by diamonds, hearts, and spades (highest).

* FYI... This ranking is used in the game of bridge.

*/

public static void main(String[] args) {

// The main method structure is generally setup for you but

// is missing several lines. You must have the three ArrayLists

//

Scanner myScan = new Scanner(System.in);

ArrayList pile = new ArrayList();

ArrayList player1 = new ArrayList();

ArrayList player2 = new ArrayList();

int restart = 1;

System.out.println("Welcome to War!");

while(restart == 1)

{

restartGame(pile, player1, player2);

createPile(pile);

deal(pile, player1, player2);

shuffle(player1);

shuffle(player2);

while(player1.size() > 0 && player2.size() > 0)

{

int value1 = getCardValue(player1.get(0));

int value2 = getCardValue(player2.get(0));

System.out.println(player1.get(0) + " vs. " + player2.get(0));

if(value1 > value2)

{

System.out.println("Player 1 wins that hand");

}

else if(value1

{

System.out.println("Player 2 wins that hand");

}

else // tie

{

// Winning tie hand not only get the two cards played, but two ADDITIONAL cards (see example)

// NOTE: You can only give cards that a player has.

// Suit order from value to most value "Clubs" , "Diamonds" , "Hearts", "Spades"

int player1suit = getSuitValue(player1.get(0));

int player2suit = getSuitValue(player2.get(0));

if(player1suit > player2suit)

{

System.out.println("TIE except Player 1 wins that hand by suit order!");

}

else

{

System.out.println("TIE except Player 2 wins that hand by suit order!");

}

// Shuffle the cards for the players when there is a tie... it reduces the pattern for the same cards.

// Without occasional shuffling, I found myself in an infinite loop after a while. It seemed like the

// cards had ordered themselves into big/small/big/small by the way I was placing the cards at the end of the deck.

// NOTE: You can only shuffle decks that have cards in them.

shuffle(player1);

shuffle(player2);

}

}

if(player1.size() == 0)

{

System.out.println("Player 2 won!");

}

else

{

System.out.println("Player 1 won!");

}

System.out.print("Would you like to play again? Press 1 for yes, any other number to exit.");

restart = myScan.nextInt();

}

System.out.println("Thanks for playing war!");

image text in transcribed

please ask me if you need the code in text format. Heres the code for 3/4 of the program cant put more due to limit.

public static void createPile(ArrayList pile) // This method creates the cards by looping through suit and card arrays. // The cards in the pile should read "A of Clubs" or "3 of Diamonds" String[] suit = {"Clubs", "Diamonds", "Hearts", "Spades" }; String[] card = {"A", "2", "3", "4", "5", "6","7", "8", "9", "T", ")", "O", "K"); public static void deal (ArrayList pile, ArrayList playeri, ArrayList player2), // randomly deal 26 cards to each player 11 after dealing, the original pile should be empty public static int getcardValue(String card) // return the value of the card sent to this method // for example, Tis 10, ) is 11, Q is 12 // Aces are high so they have a value of 14 return 0; public static int getSuitValue(String card) // return a value of the suit in the card return ; public static void shuffle(ArrayList pile) // This is already written for you. You don't have to change this. int numberOfShuffles = (int) (Math.random() * pile.size(); while(numberOfShuffles >= 0) int card = (int) (Math.random() * pile.size() pile.add(pile.get(card)); pile.remove(card); numberOfShuffles--; public static void restartGame(ArrayList pile, ArrayList player1, ArrayList player2) // empty all array lists so when you deal again, all arrays do not have any remaining cards in them. // DO NOT use any built in arraylist functions to clear the decks. public static void createPile(ArrayList pile) // This method creates the cards by looping through suit and card arrays. // The cards in the pile should read "A of Clubs" or "3 of Diamonds" String[] suit = {"Clubs", "Diamonds", "Hearts", "Spades" }; String[] card = {"A", "2", "3", "4", "5", "6","7", "8", "9", "T", ")", "O", "K"); public static void deal (ArrayList pile, ArrayList playeri, ArrayList player2), // randomly deal 26 cards to each player 11 after dealing, the original pile should be empty public static int getcardValue(String card) // return the value of the card sent to this method // for example, Tis 10, ) is 11, Q is 12 // Aces are high so they have a value of 14 return 0; public static int getSuitValue(String card) // return a value of the suit in the card return ; public static void shuffle(ArrayList pile) // This is already written for you. You don't have to change this. int numberOfShuffles = (int) (Math.random() * pile.size(); while(numberOfShuffles >= 0) int card = (int) (Math.random() * pile.size() pile.add(pile.get(card)); pile.remove(card); numberOfShuffles--; public static void restartGame(ArrayList pile, ArrayList player1, ArrayList player2) // empty all array lists so when you deal again, all arrays do not have any remaining cards in them. // DO NOT use any built in arraylist functions to clear the decks

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Upgrade Migration And Transformation Tips And Techniques

Authors: Edward Whalen ,Jim Czuprynski

1st Edition

0071846050, 978-0071846059

More Books

Students also viewed these Databases questions

Question

demonstrate the importance of induction training.

Answered: 1 week ago