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. The First half of the code is written and the second half is the image.

import java.util.ArrayList;

import java.util.Scanner;

public class GameOfWar {

/**

*

*

*

* 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!");

}

image text in transcribed

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!"); 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" Stringll suit = { "Clubs", "Diamonds", "Hearts", "Spades" String[] card = {"A", "2", "3", "4", "S", "6", "/", "g", "9", "T", ")", "O", "K"}} public static void deal(ArrayList pile, ArrayList playeri, ArrayList player2) // randomly deal 26 cards to each player // 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, I is 10, J 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 0; 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 playeri, ArrayList player2) // empty all array lists 50 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. 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!"); 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" Stringll suit = { "Clubs", "Diamonds", "Hearts", "Spades" String[] card = {"A", "2", "3", "4", "S", "6", "/", "g", "9", "T", ")", "O", "K"}} public static void deal(ArrayList pile, ArrayList playeri, ArrayList player2) // randomly deal 26 cards to each player // 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, I is 10, J 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 0; 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 playeri, ArrayList player2) // empty all array lists 50 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

Step: 3

blur-text-image

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 10g Database Administrator Implementation And Administration

Authors: Gavin Powell, Carol McCullough Dieter

2nd Edition

1418836656, 9781418836658

More Books

Students also viewed these Databases questions

Question

2-6. What are the six main categories of nonverbal signals? [LO-5]

Answered: 1 week ago

Question

=+In what ways were the two situations similar?

Answered: 1 week ago

Question

=+Does this solve the moral hazard problem? Why or why not?

Answered: 1 week ago