Hey, I need something like this output for war2 class, when there is tie in card
ID: 674714 • Letter: H
Question
Hey, I need something like this output for war2 class, when there is tie in card game
Computer Player Human Player Result 11 of Clubs 11 of Spades Tie
WAR is CALLED!
10 of Spades 11 of Clubs Discard
4 of Clubs 2 of Hearts Discard
7 of Hearts 4 of Diamonds Discard
10 of Clubs 5 of Clubs Discard
2 of Clubs 7 of Diamonds Discard
4 of Hearts 4 of Spades Discard
7 of Diamonds 1 of Hearts Discard
9 of Clubs 2 of Clubs Discard
12 of Hearts 5 of Diamonds Discard
2 of Spades 7 of Clubs Discard
5 of Spades 3 of Spades
Computer wins
Explanation / Answer
/**The java program CardDriver that cretes two random objects of the Card
* class for computer and player .
* Then run the loop until one of the computer and player wins
* the game of card.*/
//CardDriver.java
import java.util.Random;
public class CardDriver
{
private static Random rand=new Random();
public static void main(String[] args)
{
boolean win=false;
//Create two variables of Card class
Card computer=null;
Card player=null;
System.out.printf("%-15s%-15s ","Computer Player","Human Player");
while(!win)
{
//instances of Card class for computer and player
//The method getRandomSuite returns the value in the range of 0 to 3
//The method getRandomValue returns the value in the range of 1 to 13
computer=new Card(getRandomValue(), getRandomSuite());
player=new Card(getRandomValue(), getRandomSuite());
System.out.printf("%s of %s ",computer.getValueAsString(),
computer.getSuitAsString());
System.out.printf("%s of %s ",player.getValueAsString(),
player.getSuitAsString());
//check if computer wins the game
if((computer.getValue()>player.getValue())&&
(computer.getSuit()>player.getSuit()))
{
System.out.println("Computer wins");
win=true;
}
//check if player wins the game
else if((player.getValue()>computer.getValue())&&
(player.getSuit()>computer.getSuit()))
{
System.out.println("Player wins");
win=true;
}
//check if game is tie
else if((player.getValue()== computer.getValue())&&
(player.getSuit()== computer.getSuit()))
{
System.out.println("Tie");
win=true;
}
}
}
//Returns a random value in therange of 1 to 13
public static int getRandomValue()
{
return rand.nextInt(13)+1;
}
//Returns a random value in the range of 0 to 3
public static int getRandomSuite()
{
return rand.nextInt(4);
}
}
---------------------------------------------------------------------------------------------------------------------------------------
/**
* Card class represents the suit and value of card object
*/
//Card.java
public class Card
{
// Codes for the 4 suits.
public final static int SPADES = 0,
HEARTS = 1,
DIAMONDS = 2,
CLUBS = 3;
public final static int ACE = 1,
JACK = 11,
QUEEN = 12,
KING = 13;
// The suit of this card, one of the constants
//SPADES, HEARTS, DIAMONDS, CLUBS.
private final int suit;
private final int value; // The value of this card, from 1 to 11.
//constructor to set value and suit
public Card(int theValue, int theSuit)
{
value = theValue;
suit = theSuit;
}
// Return card's suit.
public int getSuit()
{
return suit;
}
// Return card's value.
public int getValue()
{
return value;
}
//Returns the suit as string of suit value
public String getSuitAsString()
{
String str="";
switch ( suit )
{
case SPADES: str="Spades";break;
case HEARTS: str="Hearts";break;
case DIAMONDS: str="Diamonds";break;
case CLUBS: str="Clubs"; break;
}
return str;
}
//Returns the value as string
public String getValueAsString()
{
String str="";
switch ( value )
{
case 1:
str="Ace";
break;
case 2:
str="2";
break;
case 3:
str= "3";
break;
case 4:
str="4";
break;
case 5:
str="5";
break;
case 6:
str="6";
break;
case 7:
str="7";
break;
case 8:
str= "8";
break;
case 9:
str="9";
break;
case 10:
str="10";
break;
case 11:
str="Jack";
break;
case 12:
str="Queen";
break;
case 13:
str="King";
break;
}
return str;
}
//Returns the String representaion of card object
public String toString()
{
return getValueAsString() + " of " + getSuitAsString();
}
} // end class Card
---------------------------------------------------------------------------------------------------------------------------------------
Sample output:
Sample runs:
Computer PlayerHuman Player
3 of Hearts 6 of Spades
Queen of Spades Queen of Hearts
6 of Spades 3 of Hearts
5 of Clubs Queen of Hearts
Jack of Clubs 3 of Hearts
Computer wins
sample run2:
Computer PlayerHuman Player
2 of Spades 8 of Spades
Jack of Hearts King of Diamonds
Player wins
sample run3
Computer PlayerHuman Player
King of Diamonds Queen of Diamonds
9 of Spades Queen of Clubs
Player wins