Question

Hey, I need something like this output for war2 class, when there is tie in card...

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   

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/**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\n","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 \n",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

Add a comment
Know the answer?
Add Answer to:
Hey, I need something like this output for war2 class, when there is tie in card...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program 4: C++ The Game of War The game of war is a card game played by children and budding comp...

    Program 4: C++ The Game of War The game of war is a card game played by children and budding computer scientists. From Wikipedia: The objective of the game is to win all cards [Source: Wikipedia]. There are different interpretations on how to play The Game of War, so we will specify our SMU rules below: 1) 52 cards are shuffled and split evenly amongst two players (26 each) a. The 26 cards are placed into a “to play” pile...

  • War—A Card game Playing cards are used in many computer games, including versions of such classic...

    War—A Card game Playing cards are used in many computer games, including versions of such classics as solitaire, hearts, and poker. War:    Deal two Cards—one for the computer and one for the player—and determine the higher card, then display a message indicating whether the cards are equal, the computer won, or the player won. (Playing cards are considered equal when they have the same value, no matter what their suit is.) For this game, assume the Ace (value 1) is...

  • I need a function that generates and returns a list containing all integers from 0 to...

    I need a function that generates and returns a list containing all integers from 0 to 51 (inclusive) where the integers correspond to cards in a deck like this 0 - 3:        2 clubs, diamonds, hearts, spades (in that order) 4 - 7:        3 clubs, diamonds, hearts, spades (in that order) 8 - 11:       4 clubs, diamonds, hearts, spades (in that order) 12 - 15:   5 clubs, diamonds, hearts, spades (in that order) 16 -...

  • Write in Java! Do NOT write two different programs for Deck and Card, it should be...

    Write in Java! Do NOT write two different programs for Deck and Card, it should be only one program not 2 separate ones!!!!!! !!!!!!!!!!!!!!!Use at least one array defined in your code and two array lists defined by the operation of your code!!!!!!!!!!!!!!!!!!!!! The array should be 52 elements and contain a representation of a standard deck of cards, in new deck order. (This is the order of a deck of cards new from the box.) The 2 Array lists...

  • I've created a Card class and I'm asked to implement a class called DeckOfCards that stores...

    I've created a Card class and I'm asked to implement a class called DeckOfCards that stores 52 objects of the Card class. It says to include methods to shuffle the deck, deal a card, and report the number of cards left in the deck, and a toString to show the contents of the deck. The shuffle methods should assume a full deck. I also need to create a separate driver class that first outputs the populated deck to prove it...

  • C++ Your solution should for this assignment should consist of five (5) files: Card.h (class specification...

    C++ Your solution should for this assignment should consist of five (5) files: Card.h (class specification file) Card.cpp (class implementation file) DeckOfCards.h (class specification file) DeckOfCards.cpp (class implementation file) 200_assign6.cpp (application program) NU eelLS Seven UT Diamonds Nine of Hearts Six of Diamonds For your sixth programming assignment you will be writing a program to shuffle and deal a deck of cards. The program should consist of class Card, class DeckOfCards and an application program. Class Card should provide: a....

  • Using Python 3.5.2 and IDLE Write the code for a modified game of the card game...

    Using Python 3.5.2 and IDLE Write the code for a modified game of the card game crazy eights (much like a game of UNO, where the card played is now on top of the deck and its number or face value is now what is playable for the next player). Write the code to be a 2 player game with any 8 card being able to input a new face value to play on the discard pile. Rules: Use a...

  • 4. A group of students are playing a card game. The game uses a well-shuffled deck...

    4. A group of students are playing a card game. The game uses a well-shuffled deck of 56 playing cards. 52 of the cards are exactly as described in your textbook. However, there are also 4 Jokers. This means that there are 56 cards: 14 are hearts, 14 are diamonds, 14 are clubs, and 14 are spades. A hand of cards consists of Eight of the cards. Find the number of different hands that contain: a. At least 6 Diamonds....

  • bblem deals with playing cards. The Card API is given below: public class Card ( suit...

    bblem deals with playing cards. The Card API is given below: public class Card ( suit is "Clubs", "Diamonds", "Bearts", or "Spades" Gene=ination s 2", , "10" יי", ,"פ" ,"8" ,"ר" , "6" ,"5י ,-4" ,"ני- * or "A * value is the value of the card number if the card denominat, *is between 2 and 10; 11 for J, 12 for Q, 13 for K, 14 for A public Card (String suit, string denomination){} 1/returns the suit (Clubs, Diamonds,...

  • In the last assignment, you created a card class. Modify the card class so the setValue()...

    In the last assignment, you created a card class. Modify the card class so the setValue() method does not allow a card’s value to be less than 1 or higher than 13. If the argument to setValue() is out of range, assign 1 to the card’s value. You also created a PickTwoCards application that randomly selects two playing cards and displays their values. In that application, all card objects were arbitrarily assigned a suit represented by a single character, but...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT