Question

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 they could have different values, and the player observed which of two card objects had the higher value. Now, modify the application so the suit and the value both are chosen randomly. Using two card objects play a very simple version of the card game 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 the suit is). For this game, assume the Ace (value 1) is low. Make sure that the two cards dealt are not the same card. For example, a deck cannot contain more than one card representing the 2 of spades. If two cards are chosen to have the same value, change the suit for one of them. Save the application as War.java

Now modify the game using the newly modified card class so that when a tie is declared, that each player “puts down 10 cards each” and compares the 11th card to see if there is a clear winner. If there is a tie, repeat the process until there is a clear winner. The table below shows four typical executions. Recall that in this version of War, you assume

that the ace is the lowest-valued card. Save the game as War2.java.

So I expect a Card.java, War.java and War2.java file; Each working off the other. No need to reinvent the wheel! Lastly I expect, when WAR is called, to see all ten cards displayed.

I did the part 1 war.java and i just need help on part 2

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

My Card Class:

public class Card {

private int cardNum;
final static String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
final static String[] ranks = {"2", "3","4","5","6","7","8", "9","10", "Jack", "Queen", "King", "Ace"};

Card (int theCard) {
setCardNum (theCard);
}

public void setCardNum (int theCard) {
cardNum = (theCard >= 0 && theCard <= 51)? theCard: 0;
}

public int getCardNum() {
return cardNum;
}

public String toString() {
return ranks[cardNum%13] + " of " + suits[cardNum/13];
}

public String getSuit() {
return suits[cardNum/13];
}

public String getRank() {
return ranks[cardNum%13];
}

public int getValue() {
return cardNum%13;
}
}

My Deck Class

public class Deck {

private Card[] deck = new Card[52];
private int topCard;

Deck() {

topCard = 0;

for (int i = 0; i < deck.length; i++)
deck[i] = new Card(i);

}

public void shuffle() {

topCard = 0;

for (int i = 0; i < 1000; i++) {
int j = (int)(Math.random()*52);
int k = (int)(Math.random()*52);
Card tmpCard = deck[j];
deck[j] = deck[k];
deck[k] = tmpCard;
}
}

public Card dealCard() {
Card theCard;
if (topCard < deck.length) {
theCard = deck[topCard];
topCard++;
}
else
theCard = null;

return theCard;
}
}

My War Game Main Program:

import java.util.Scanner;

public class WarGame {

public static void main(String[] args) {

Card[][] hands = new Card[2][1];
Deck myDeck = new Deck();

for (int i = 0; i < 53; i++) {
System.out.printf("\n Round %s of The War \n", i);

for (int c = 0; c < 1; c++)
for (int player = 0; player < hands.length; player++)
hands[player][c] = myDeck.dealCard();

for (int player = 0; player < hands.length; player++) {
System.out.printf("Player %d: ", player);
printHand(hands[player]);

int player1;
int player2;

if (player1.getValue() > player2.getValue())
System.out.println("Player One Wins The War");
else if (player2.getValue() > player1.getValue())
System.out.println("Player Two Wins The War");
else
System.out.println("The War Is A Tie");

}
}
}

public static void printHand(Card[] hand) {

for (int card = 0; card < hand.length; card++)
System.out.printf("%s", hand[card].toString());

System.out.println();

}
}

Add a comment
Know the answer?
Add Answer to:
In the last assignment, you created a card class. Modify the card class so the setValue()...
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
  • 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...

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

  • C++ program This program involves writing a VERY simplified version of the card game War. You...

    C++ program This program involves writing a VERY simplified version of the card game War. You may know this game or not but my rules are these 1. Split the deck between player1 and player2. Only the face values matter (2-14) and not the suits 2. Each player puts a card down on the table. The higher face value wins that hand. If the card values match, you will simply indicate tie and neither player wins.The original rules would require...

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

  • 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 am playing a simplified version of the card game, "War," with my brother. We are...

    I am playing a simplified version of the card game, "War," with my brother. We are playing with a subset of the deck, all of the numbered cards 2 through 10 (2:10). There are 4 cards of each number, resulting in 36 total cards in a single pile. First, I turn over a card. Next, he turns over a card. Whoever has the higher card wins and these "used" cards are discarded. If we turn over the same card, we...

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

  • Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack...

    Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack class, which implements a BlackJack game. Please do not use any java applet on the coding. Hint: Use a test class to test above classes. Pulic class Card {    private final String face; // face of card ("Ace", "Deuce", ...)    private final String suit; // suit of card ("Hearts", "Diamonds", ...)    // two-argument constructor initializes card's face and suit    public...

  • IN JAVA - COMMENT CODE WELL Write a class named Card which will represent a card...

    IN JAVA - COMMENT CODE WELL Write a class named Card which will represent a card from a deck of cards. A card has a suit and a face value. Suits are in order from low to high: Clubs, Diamonds, Hearts and Spades. The card values from low to high: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. Write a Deck class that contains 52 cards. The class needs a method named shuffle that...

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

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