This assignment is about card game called WAR that has 4 classes the main,Card,C
ID: 654009 • Letter: T
Question
This assignment is about card game called WAR that has 4 classes the main,Card,CardDeck,and Hand. I am struggling at hand class which required
public Hand()// constructor of the object that hold the players cards.
public void displayHand()// display the hand
public void take(Card c )// takes a card that is dealt
public int value()// retuns the value of the hand
could you please help me here is the code in java :
import java.util.Scanner;
public class main {
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int Choice;
int menu=0, WAR=1;
CardDeck d = new CardDeck();
System.out.println("Welcome to the War Game");
d.ShowMenu();
while (menu==0){
Choice = keyboard.nextInt();
WAR=1;
d.User=0;
d.Comp=0;
switch (Choice) {
case 1:
d = new CardDeck();
System.out.println("You Have A New Card Deck!");
d.ShowMenu();
break;
case 2:
System.out.println("-----------------------------------");
System.out.println("Here Are All The Cards In The Deck!");
System.out.println("-----------------------------------");
for (int k = 0; k<52;k++)
{
d.displayCardAt(k);
}
d.ShowMenu();
break;
case 3:
d.shuffle();
System.out.println("Your Deck Has Been Shuffled!");
d.ShowMenu();
break;
case 4:
while(WAR==1)
{
System.out.println("----------------------------------");
System.out.println("Get Ready To Play Two-Card WAR!!!!");
System.out.println("----------------------------------");
System.out.println("There are "+d.cardsLeft()+" left in the deck.");
System.out.println("...dealing...");
System.out.println("-----------------------");
System.out.println("Here Are Your Two Cards");
System.out.println("-----------------------");
d.Deal();
d.Deal();
System.out.println("---------------------");
System.out.println("Here Are My Two Cards");
System.out.println("---------------------");
d.CompDeal();
d.CompDeal();
System.out.println();
System.out.println("Your Score is " + d.User);
System.out.println();
System.out.println("My Score is " + d.Comp);
System.out.println();
if (d.User > d.Comp)
{
System.out.println("You Won!");
}
else if (d.User == d.Comp)
{
System.out.println("It is a Tie!");
}
else if (d.User < d.Comp)
{
System.out.println("I Won!");
}
System.out.println();
d.User=0;
d.Comp=0;
System.out.println("Wanna Play Again? (Yes: 1 / No: 2)");
WAR=keyboard.nextInt();
}
d.ShowMenu();
break;
case 5:
System.out.println("5");
menu=1;
break;
default: System.out.println("Sorry, That's Not Part Of the Menu. Select An Option From The Menu");
}
}
System.out.println("Thank You For Playing WAR!");
}
}
public class card {
private char suit;
private char rank;
public card()
{
}
public card(char s, char r)
{
suit = s;
rank = r;
}
public void setSuit(char s)
{
if(s=='D'||s=='C'||s=='H'||s =='S')
{
suit = s;
}
}
public void setRank(char r)
{
if(r=='A'||r=='2'||r=='3'||r=='4'||r=='5'||r=='6'||r=='7'||r=='8'||r=='9'||r=='T'||r=='J'||r=='Q'||r=='K')
{
rank = r;
}
}
public char getValue()
{
return rank;
}
public void displayCard()
{
System.out.println(" _________");
System.out.println("|"+rank+" "+rank+"|");
System.out.println("| |");
System.out.println("| |");
System.out.println("| |");
System.out.println("| "+suit+" |");
System.out.println("| |");
System.out.println("| |");
System.out.println("|"+rank+" "+rank+"|");
System.out.println("|_________|");
}
}
import java.util.Random;
public class CardDeck {
private card c = new card();
private card [] deck;
int count = 0;
public int User = 0;
public int Comp = 0;
public CardDeck()
{
deck = new card [52];
char [] rankValue = {'A','2','3','4','5','6','7','8','9','T','J','Q','K'};
char [] suitValue = {'D','C','H','S'};
for(int i = 0; i<52; i++)
{
deck[i] = c;
}
for(int i = 0; i<4; i++)
{
for (int j = 0; j<13; j++)
{
deck[i*13+j] = new card(suitValue[i],rankValue[j]);
//deck[i*13+j].setRank(rankValue[j]);
//deck[i*13+j].setSuit(suitValue[i]);
}
}
}
public void Deal()
{
int Card,CardPoint;
char CardValue;
Random r = new Random();
Card = r.nextInt(52);
CardValue = deck[Card].getValue();
switch (CardValue) {
case 'A':
CardPoint = 1;
break;
case '2':
CardPoint = 2;
break;
case '3':
CardPoint = 3;
break;
case '4':
CardPoint = 4;
break;
case '5':
CardPoint = 5;
break;
case '6':
CardPoint = 6;
break;
case '7':
CardPoint = 7;
break;
case '8':
CardPoint = 8;
break;
case '9':
CardPoint = 9;
break;
case 'T':
CardPoint = 10;
case 'J':
CardPoint = 10;
break;
case 'Q':
CardPoint = 10;
break;
case 'K':
CardPoint = 10;
break;
default:
CardPoint = 0;
}
System.out.println();
System.out.println("The Card Value is: "+CardPoint);
deck[Card].displayCard();
User = User + CardPoint;
count++;
}
public void CompDeal()
{
int Card,CardPoint;
char CardValue;
Random r = new Random();
Card = r.nextInt(52);
CardValue = deck[Card].getValue();
switch (CardValue) {
case 'A':
CardPoint = 1;
break;
case '2':
CardPoint = 2;
break;
case '3':
CardPoint = 3;
break;
case '4':
CardPoint = 4;
break;
case '5':
CardPoint = 5;
break;
case '6':
CardPoint = 6;
break;
case '7':
CardPoint = 7;
break;
case '8':
CardPoint = 8;
break;
case '9':
CardPoint = 9;
break;
case 'T':
CardPoint = 10;
case 'J':
CardPoint = 10;
break;
case 'Q':
CardPoint = 10;
break;
case 'K':
CardPoint = 10;
break;
default:
CardPoint = 0;
}
System.out.println();
System.out.println("The Card Value is: "+CardPoint);
deck[Card].displayCard();
Comp = Comp + CardPoint;
count++;
}
public int cardsLeft()
{
int cardsLeft;
cardsLeft = 52-count;
return cardsLeft;
}
public void ShowMenu()
{
System.out.println("--------------------------");
System.out.println("What Would You Like To Do?");
System.out.println("--------------------------");
System.out.println("1) Get A New Card Deck");
System.out.println("2) Show All Cards In The Deck");
System.out.println("3) Shuffle");
System.out.println("4) Play WAR!");
System.out.println("5) Exit");
}
public void displayCardAt(int i)
{
deck[i].displayCard();
}
public void shuffle()
{
int x,y;
card temp;
Random r = new Random();
for(int i=0; i<1000000;i++)
{
x = r.nextInt(52);
y = r.nextInt(52);
temp = deck[x];
deck[x]= deck[y];
deck[y] = temp;
}
}}
Explanation / Answer
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int Choice;
int menu=0, WAR=1;
CardDeck d = new CardDeck();
System.out.println("Welcome to the War Game");
d.ShowMenu();
while (menu==0){
Choice = keyboard.nextInt();
WAR=1;
d.User=0;
d.Comp=0;
switch (Choice) {
case 1:
d = new CardDeck();
System.out.println("You Have A New Card Deck!");
d.ShowMenu();
break;
case 2:
System.out.println("-----------------------------------");
System.out.println("Here Are All The Cards In The Deck!");
System.out.println("-----------------------------------");
for (int k = 0; k<52;k++)
{
d.displayCardAt(k);
}
d.ShowMenu();
break;
case 3:
d.shuffle();
System.out.println("Your Deck Has Been Shuffled!");
d.ShowMenu();
break;
case 4:
while(WAR==1)
{
System.out.println("----------------------------------");
System.out.println("Get Ready To Play Two-Card WAR!!!!");
System.out.println("----------------------------------");
System.out.println("There are "+d.cardsLeft()+" left in the deck.");
System.out.println("...dealing...");
System.out.println("-----------------------");
System.out.println("Here Are Your Two Cards");
System.out.println("-----------------------");
d.Deal();
d.Deal();
System.out.println("---------------------");
System.out.println("Here Are My Two Cards");
System.out.println("---------------------");
d.CompDeal();
d.CompDeal();
System.out.println();
System.out.println("Your Score is " + d.User);
System.out.println();
System.out.println("My Score is " + d.Comp);
System.out.println();
if (d.User > d.Comp)
{
System.out.println("You Won!");
}
else if (d.User == d.Comp)
{
System.out.println("It is a Tie!");
}
else if (d.User < d.Comp)
{
System.out.println("I Won!");
}
System.out.println();
d.User=0;
d.Comp=0;
System.out.println("Wanna Play Again? (Yes: 1 / No: 2)");
WAR=keyboard.nextInt();
}
d.ShowMenu();