Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the Card Game project from this Unit's lecture. You need to complete those portions listed below and may complete them in this recommended order:

Complete the Card Game project from this Unit's lecture. You need to complete those portions listed below and may complete them in this recommended order:

  • GroupOfCardsclass:
  • addCard(): add to high end of deck i.e. append
  • removeCard(int index)
  • Deckclass:
  • dealCard(): a card from high end of the deck (the card with index getCurrentSize()-1)
  • Handclass:
  • playACard(): a random card
  • Gameclass:
  • playAGame(): take turns to play one card. The player with a larger card scores one point. After all cards are played the player with more points win. For simplicity of this simulation, use the compareTo() ofCardto compare twoCardobjects. It should be used just like compareTo() ofString.
  • Deckclass:
  • shuffle().

Usejava.util.concurrent.ThreadLocalRandomfor random number generation (needed by shuffle() ofDeckand playACard() ofHand).This is the standard way of random number generation since Java 1.7.Do not usejava.util.RandomorMath.random(). Here is how:

import java.util.concurrent.ThreadLocalRandom; // already done for you // will generate a random int in [min, max) int randomNum = ThreadLocalRandom.current().nextInt(min, max); 

The completed program may generate outputs like those:

Player 1: Spade 5 vs. player 2: Diamond 10 -> scores: 1 vs. 0 Player 1: Club 14 vs. player 2: Diamond 9 -> scores: 1 vs. 1 Player 1: Diamond 13 vs. player 2: Spade 12 -> scores: 1 vs. 2 Player 1: Spade 11 vs. player 2: Heart 13 -> scores: 2 vs. 2 Player 1: Diamond 6 vs. player 2: Diamond 4 -> scores: 3 vs. 2 Player 1: Diamond 3 vs. player 2: Club 3 -> scores: 4 vs. 2 Player 1: Heart 6 vs. player 2: Spade 14 -> scores: 4 vs. 3 Player 1: Club 11 vs. player 2: Club 12 -> scores: 4 vs. 4 Player 1: Spade 3 vs. player 2: Heart 3 -> scores: 5 vs. 4 Player 1: Club 2 vs. player 2: Heart 4 -> scores: 5 vs. 5 Player 1: Heart 12 vs. player 2: Spade 6 -> scores: 5 vs. 6 Player 1: Diamond 11 vs. player 2: Heart 8 -> scores: 5 vs. 7 Player 1: Diamond 8 vs. player 2: Diamond 12 -> scores: 5 vs. 8 Player 1: Heart 14 vs. player 2: Heart 9 -> scores: 6 vs. 8 Player 1: Club 10 vs. player 2: Spade 10 -> scores: 6 vs. 9 Player 1: Heart 7 vs. player 2: Diamond 5 -> scores: 7 vs. 9 Player 1: Spade 2 vs. player 2: Diamond 7 -> scores: 8 vs. 9 Player 1: Heart 2 vs. player 2: Club 7 -> scores: 9 vs. 9 Player 1: Spade 8 vs. player 2: Diamond 14 -> scores: 10 vs. 9 Player 1: Heart 11 vs. player 2: Diamond 2 -> scores: 11 vs. 9 Player 1: Spade 7 vs. player 2: Heart 10 -> scores: 12 vs. 9 Player 1: Club 6 vs. player 2: Club 4 -> scores: 13 vs. 9 Player 1: Club 9 vs. player 2: Spade 9 -> scores: 13 vs. 10 Player 1: Spade 13 vs. player 2: Club 5 -> scores: 14 vs. 10 Player 1: Heart 5 vs. player 2: Club 8 -> scores: 15 vs. 10 Player 1: Spade 4 vs. player 2: Club 13 -> scores: 16 vs. 10 Player1 won Play another game (y/n)?: n Thanks for playing. Bye-bye. 

The print for each round should come from playAGame() of Game class.

/**

* GroupOfCards.java (partial, 2 ADD CODE)

* Implements a group of cards and provides methods

* to add a card and remove a card

* Project: Ch13 Card Game

*

* CS219

*/

package game;

import java.util.ArrayList;

// Implements a group of cards

public class GroupOfCards

{

private ArrayList cards; // list of cards in the group

//*******************************************

/**

* Createan object which is capable of

* holding multiple cards. Doesn't actually

* createand add card objects. That's up

* to individual child class

*/

public GroupOfCards() {

cards = new ArrayList();

}

//*******************************************

public int getCurrentSize() {

return cards.size();

}

//*******************************************

public void display() {

System.out.println(cards);

}

//*******************************************

/**

* Add a card to high end of list: append

*

* @param card

*/

public void addCard(Card card) {

// ADD CODE #1

}

//*******************************************

/**

* Remove and return a card object at a specified location

*

* @param indexthe index of the element to be removed

* @returnthe card object just removed.

*/

public Card removeCard(int index) {

// ADD CODE #2: remove return null line and add code

return null; // fake return

}

} // end class GroupOfCards

/**

* Deck.java (partial, 2 ADD CODE)

* Implements a deck of 52 cards and provides methods

* to work with cards such as deal a card and shuffle

* the whole deck

* Project: Ch13 Card Game

*

* CS219

*/

package game;

import java.util.concurrent.ThreadLocalRandom;// will be used

// Implements a deck of 52 cards

public class Deck extends GroupOfCards

{

public final static int TOTAL_NUM_CARDS = 52; // Number of cards in a deck

//*******************************************

/**

* Createa deck of cards and add cards of (num, suit)

* with

*num: 2-14, use 11-14 for J, Q, K, A

*suit: 0-3 for "Club"<"Diamond"<"Heart"<"Spade"

*

*Cards are created and added to deck in order of:

*(2, 0/Club) - (14, 0/Club) then

*(2, 1/Diamond) - (14, 1/Diamond) then

*(2, 2/Heart) - (14, 2/Heart) and finally

*(2, 3/Spade) - (14, 3/Spade)

*/

public Deck() {

for (int i=0; i

{

addCard(new Card(2 + i%13, i/13));

}

}

//*******************************************

/**

* Deal the card at the end of the deck by removing

* it from the deck and return it

*

* @returnthe card removed

*/

public Card dealCard() {

// ADD CODE #1: remove return null line

return null; // fake return

}

/**

* Shuffle all cards currently in a deck

*/

public void shuffle() {

// ADD CODE #2

/*

The approach described in Exercise #8 of Ch13:

for (int unshuffled = getCurrentSize()-1;

unshuffled >= 0;

unshuffled--)

{

// 1. pick a random card

// 2. remove that random card

// 3. add that card back to high end of the deck

}

*/

} // end shuffle

} // end class Deck

/**

* Hand.java (partial, 1 ADD, 1 optional ADD)

* Implements a hand of cards and provides methods

* to work with cards such as play a card and sort

* all cards currently in hand

* Project: Ch13 Card Game

*

* CS219

*/

package game;

import java.util.concurrent.ThreadLocalRandom; // will be used

/*Implements a hand of cards and provides methods

* to work with cards such as play a card and sort

* all cards currently in hand

*/

public class Hand extends GroupOfCards

{

/**

* Play a card depending on playing strategy.

* Will remove and return that card

*

* @return the card being played

*/

public Card playACard() {

// ADD CODE #1: remove return null line and add code

// 1. decide which card to move: pick a random card

// 2. remove and return that card

return null; // fake return

}

/**

* (Optional) Sort all cards in hand following some strategy

*/

public void sort() {

// ADD CODE #2 (optional)

}

} // end class Hand

**

* Game.java (partial, 1 ADD)

* Implements a card game involving one deck of cards and

* 2 players. Provides methods to play a card game

* Project: Ch13 Card Game

*

* CS219

*/

package game;

// Implements a card game involving one deck of cards and 2 players

public class Game

{

private Deck deck;// The deck of cards to be played with

private Hand player1; // Hand of cards held by player 1

private Hand player2; // Hand of cards held by player 2

//*******************************************

/**

* Createa new game with a default deck of

* cards and two default hands

*/

public Game() {

deck = new Deck();

player1 = new Hand();

player2 = new Hand();

}

//*******************************************

public void playAGame() {

// 0. prepare the deck of cards

deck.shuffle();

// 1. deal all the cards to the two players

while (deck.getCurrentSize() > 0)

{

player1.addCard(deck.dealCard());

player2.addCard(deck.dealCard());

}

// 2. play

int score1 = 0;// score of player 1

int score2 = 0;// score of player 2

// ADD CODE

/*

while (player1.getCurrentSize()>0 && player2.getCurrentSize()>0)

{

// 2.1 player 1 plays a card, display the card

// 2.2 player 2 plays a card, display the card

// 2.3 decide who scores and update scores

// 2.4 print result of this round: print two cards played and current scores

}

*/

// 3. result of the game

String resultStr = "";

if (score1 == score2)

resultStr = "Tie";

else if (score1 < score2)

resultStr = "Player2 won";

else // 1 > 2

resultStr = "Player1 won";

System.out.println(resultStr);

} // end PlayAGame

} // end class Game

/**

* Card.java (complete)

* Describes a card with a suit value and a num value

* Project: Ch13 Card Game

* CS219

*/

package game;

// Describes a card with a suit value and a num value

public class Card

{

private static final String[] SUIT_STR = {"Club", "Diamond", "Heart", "Spade"};

// Used for display suit value such as Club instead of 0

private int num;// number value of a card: 2-14, use 11-14 for J, Q, K, A

private int suit; // suit value of a card: 0-3 for "Club"<"Diamond"<"Heart"<"Spade"

//*******************************************

public Card() {

// default constructor, intentionally empty

}

/**

* Createa Card object with specified num and suit

*

* @param numnum value. 2-14, use 11-14 for J, Q, K, A

* @param suitsuit value. 0-3 for "Club"<"Diamond"<"Heart"<"Spade"

*/

public Card(int num, int suit) {

this.num = num;

this.suit = suit;

}

//*******************************************

public int getNum() {

return num;

}

public int getSuit() {

return suit;

}

//*******************************************

/**

* Displays a card in format of suit and num

* for example: Club 10

*/

public void display() {

System.out.print(toString());

}

//*******************************************

/**

* Compare two cards. The comparison is based on first num

* and then suit. The result is a negative integer if this

* Card object is smaller than the argument Card. The result

* is a positive integer if this Card object is larger. The

* result is zero if they are equal i.e. when equals() would

* return true.

*

* @param anotherCard

* @returna negative value means

*/

public int compareTo(Card anotherCard) {

if (suit == anotherCard.suit)

return num - anotherCard.num;

return suit - anotherCard.suit;

}

//*******************************************

/**

* Returns a string representation of

* a card object in format of "Club 10".

*/

public String toString() {

return (SUIT_STR[suit]+" "+num);

}

//*******************************************

/**

* Compare this object with the specified obj. The

* result is true if and only if the argument is a Card

* and has the same num and suit values.

*/

public boolean equals(Object anotherObj) {

if (this == anotherObj)

return true;

if (anotherObj instanceof Card) {

Card tmp = (Card) anotherObj;

return num == tmp.num && suit == tmp.suit;

}

return false;

}

} // end class Card

**

* GameDriver.java (complete)

* Set up a game playing environment and simulate

* playing a card game

* Project: Ch13 Card Game

*

* CS219

*/

package game;

import java.util.Scanner;

// Set up a game playing environment and simulate playing a card game

public class GameDriver

{

public static void main(String[] args)

{

Scanner stdIn = new Scanner(System.in);

String again;

Game game;

do // loop for game playing

{

// one game

game = new Game();

game.playAGame();

// again?

System.out.print("Play another game (y/n)?: ");

again = stdIn.next();

} while (again.equalsIgnoreCase("y"));

stdIn.close();

// good-bye msg

System.out.println("Thanks for playing. Bye-bye.");

}// end main

}// end GameDriver

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions