Question

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 low. Play 26 rounds dealing a full deck with no repeated cards. If the number of times the player wins is bigger than the computer’s, then the player wins. (This is the simple version of the game. Feel free to add any new rules to make your card game more interesting. Please add necessary comments in your code to explain the rules.)

Design the Card game and implement it as follows.

  1. Design a Card class that contains a String data field to hold the name of a suit (Spades, Hearts, Diamonds, or Clubs) and an int data field for a value a value of the card. Write the constructors if needed. Include get and set methods for each field. Include any methods that is needed. Save the class as Card.java. (You need to include at least one method in your class show the card value as Ace, King, Queen, Jack, or a number value).
  1. Write an application that simulate the card game War using Card objects. Save the application as War.java.

  • Some hints:

  • Start by creating an array of all 52 playing Card objects with values (from 0 to 52).
  • shuffle the cards
  • Randomly select a card for the computer and players
  • Remove the card from the deck.
  • Display the values and suits of the player and computer's cards
  • Compare the val­ues, and determine the winner.
  • When all the cards in the deck are exhausted, display a count of the num­ber of times the player wins, the number of times the computer wins, and the number of ties.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Card.java
public class Card
{

    private int cardnum;
    private char suit;
  
    //Constructors
    public Card(char suit, int cardnum){
        this.cardnum = cardnum;
        this.suit = suit;
    }//end of construcor
  
    //Get method for card
    public int getCardNumber(){
        return cardnum;
    }//end getCard method
  
    //Set method for card
    public void setCardNumber(int cardnum){
        this.cardnum = cardnum;
    }//end of SetCard method
  
    @Override
    public String toString(){
  
        StringBuilder displayCard = new StringBuilder();
      
    
        switch(cardnum){
          
            case 11:
                displayCard.append("Jack");
                break;
            case 12:
                displayCard.append("Queen");
                break;
            case 13:
                displayCard.append("King");
                break;
            case 14:
                displayCard.append("Ace");
                break;  
            default:
                displayCard.append(cardnum);
                break;
        }//end rank switch
      
        displayCard.append(" of "); //setting the format of the output
      
        switch(suit){
            case 0:
                displayCard.append("Spades");
                break;
            case 1:
                displayCard.append("Hearts");
                break;
            case 2:
                displayCard.append("Clubs");
                break;
            case 3:
                displayCard.append("Diamonds");
                break;
            default:
                break;
        }
      
     
        return displayCard.toString();
    }//end of toString
  
}


War.java


import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class War {

    /**
     * @param args the command line arguments
     */
     public static void main(String[] args) {
        //ArrayList for Deck of Cards
        List<Card> Deck = new ArrayList<Card>();
        //Four suits
        for(int x=0; x<3; x++){
          
            //13 ranks
            for(int y=2; y<15; y++){   
                Deck.add(new Card((char) x,y));
            } //end of rank for loop
        }//end of suit for loop
      
      
        //Card Shuffle
        Collections.shuffle(Deck, new Random());
      
        //Player deck
        LinkedList<Card> deck1 = new LinkedList<Card>();
        //Computer deck
        LinkedList<Card> deck2 = new LinkedList<Card>();
      
        //player Cards
        deck1.addAll(Deck.subList(0, 25));
        //computer cards     
        deck2.addAll(Deck.subList(26, Deck.size()));
      
        while(true){
            Card pCard = deck1.pop();
            Card comCard = deck2.pop();
          
            //show the card
            System.out.println("Player " + pCard.toString());
            System.out.println("Computer " + comCard.toString());
          
        
            if(pCard.getCardNumber() > comCard.getCardNumber()){
                deck1.addLast(pCard);
                deck1.addLast(comCard);
                System.out.println("Player wins");
            }//end of if

             else if(pCard.getCardNumber() < comCard.getCardNumber()){
                deck2.addLast(pCard);
                deck2.addLast(comCard);
                System.out.println("Computer wins");
            }//end else if
            else
                 System.out.println("Its a Tie!");
          
          
          
          
            if(deck1.size()>deck2.size() ){
                System.out.println("Game Over");
                break;
            }
            else if(deck2.size()>deck1.size()){
                System.out.println("Game Over");
                break;
          
        }//end while

    } // end of main
}// end of class
}

Add a comment
Know the answer?
Add Answer to:
War—A Card game Playing cards are used in many computer games, including versions of such classic...
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
  • 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...

  • CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing...

    CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties an integer for the rank (1 (corresponding to Ace) ,2,3, 13 (correspond ing to King) and suit (Spades, Hearts, Diamonds, or Clubs). Make the suit an enumerated data type. Include getters and setters and a method that tests if a card is valid. Write a class named Deck whose instances are full...

  • 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...

  • I need to build the card game of War in C++. It will be a 2...

    I need to build the card game of War in C++. It will be a 2 player game. Each player will have their own deck of 52 cards. 2-10, Jack=11, Queen=12, King=13, Ace=14. Each player will draw one card from their deck. The player with the higher card wins both cards. If the card drawn is the same, then each player will draw 3 cards and on the 4th card drawn will show it. The player that shows the higher...

  • Please to indent and follow structure!!!!! Assignment 3 - The card game: War Due Date: June...

    Please to indent and follow structure!!!!! Assignment 3 - The card game: War Due Date: June 9th, 2018 @ 23:55 Percentage overall grade: 5% Penalties: No late assignments allowed Maximum Marks: 10 Pedagogical Goal: Refresher of Python and hands-on experience with algorithm coding, input validation, exceptions, file reading, Queues, and data structures with encapsulation. The card game War is a card game that is played with a deck of 52 cards. The goal is to be the first player to...

  • NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET...

    NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET CODE TO RUN!! THANKS Extend the DeckofCards and the Card class in the book to implement a card game application such as BlackJack, Texas poker or others. Your game should support multiple players (up to 5 for BlackJack). You must build your game based on the Cards and DeckofCards class from the book. You need to implement the logic of the game. You can...

  • 19. A Card Game 19. A Card Game Three students are playing a card game. They...

    19. A Card Game 19. A Card Game Three students are playing a card game. They decide to choose the first person to play by each selectinga card from the 52-card deck and look- ing for the highest card in value and suit. They rank the suits from lowest to highest: clubs, diamonds, hearts, and spades. a. If the card is replaced in the deck after each student chooses, how many possible configurations of the three choices are possible? b....

  • 2) Broard Games Rules 1) Game equipment: 1 game broard, 1 deck of playing cards, 25...

    2) Broard Games Rules 1) Game equipment: 1 game broard, 1 deck of playing cards, 25 playing pieces (5 each of 5 colors 2) To start : Each player chooses a color and places the pieces of that color on the matching HOME circle. One player shuffles the deck and places at it face down the space marked CARDS: select a player to play first, the winter of one game goes first in the net. 3) the object of the...

  • 1. (25 total points) Probability and card games; Recall that an ordinary decdk of playing cards...

    1. (25 total points) Probability and card games; Recall that an ordinary decdk of playing cards has 52 cards of which 13 cards are from each of the four suits hearts, diamonds, spades, and clubs. Each suit contains the cards 2 to 10, ace, jack, queen, and king. (a) (10 points) Three cards are randomly selected, without replacement, from an or- dinary deck of 52 playing cards. Compute the conditional probability that the first card selected is a spade, given...

  • 6. Alicia is playing a game by drawing a card from a standard deck and replacing...

    6. Alicia is playing a game by drawing a card from a standard deck and replacing it. If the card is an Ace card, Alicia win $100. If it is not an Ace card, she pay $10. There are 4 Ace cards in a deck of 52 cards. What is the expected value of playing this game? A. 2.934 B.-0.572 C.-1.273 D.-1.538 E.-1.792 F.-2.682

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