You are going to create a simple card game of 21. In this game, the single playe
ID: 3627387 • Letter: Y
Question
You are going to create a simple card game of 21. In this game, the single player has to try to come as close to a score of 21 without going over and beat the computer's score.
1. Set up a class called PlayingCard. Add a method that returns a string representing its rank and suit. For example, the card with rank 1 and suit 'S' is "Ace of Spades".
2. Set up a class called DeckOfPlayingCards which contains the following information:
INSTANCE VARIABLES
an array of 52 PlayingCard objects
index of the "top" card in the deck
METHODS
constructor - initializes each PlayingCard object to a unique card in a standard deck
shuffle - randomly permute the order of the cards in the array (see below for a hint) and sets the top card to the first card in the array
deal - returns the top card in the deck and sets the top card index to the next card
3. Set up a class TwentyOne with a main method that creates the deck of cards and then allows the user to play the game of 21.
SHUFFLING
In order to shuffle the cards, use the following algorithm:
for i <-- 0 to 50
pick a random integer j between i and 51
swap card i with card j
end for
GENERAL GAME RULES
Each card has a point value based on its rank (the suit is ignored in this game). The cards with ranks 2 through 10 have point values of 2 through 10 respectively. The "face" cards (Jack, Queen, King) have a point value of 10 each. The Ace is considered as 11 points, unless that puts the player over a total of 21 points, in which case it reverts to 1 point instead. For example, the following cards are dealt to the player and the total scores are shown to their right:
CARD CARD SCORE TOTAL SCORE
5 of Diamonds 5 5
Ace of Hearts 11 16
7 of Clubs 7 13
3 of Clubs 3 16
Ace of Spades 1 17
4 of Hearts 4 21
In each game, the deck of cards is shuffled, and the user starts with the first two cards of the deck. The user may pick the next card of the deck by inputting "HIT" or the user may stop at this point by inputting "STAY". The user can pick as many cards as he or she wants in order to try to come up with a score as close to 21 without going over. If the user goes over 21 points, the user automatically loses and the computer wins. Otherwise, if the user stops with a total score less than or equal to 21, then the computer plays. The computer starts with the next two cards of the deck. The computer automatically "hits" until its score is at least 17. If the computer goes over 21 (but the user did not), then the user wins automatically. Otherwise, the winner is the player with the higher score. A tie (same total score) is won by the computer.
INPUT PROCESSING
Input will come from the keyboard in this game. The user should input "HIT" or "STAY" (lowercase ok) as the game proceeds. Any other input should flag an error "Unrecognized input", etc. and you should ask for the input again. At the end of the game, you should ask the user if he/she wants to play again. The input here will be "Y" or "N" (lowercase ok). All other input will lead to an error and you should ask the user to input again. See OUTPUT PROCESSING for an example of correct input.
OUTPUT PROCESSING
Here is a sample of the output of the program (user input in purple italics):
(reds are inputs)
LET'S PLAY 21!
SHUFFLING CARDS...
YOUR TURN
5 of Diamonds 5
Ace of Hearts 16
HIT or STAY? HIT
7 of Clubs 13
HIT or STAY? hit
3 of Clubs 16
HIT or STAY? HIT
Ace of Spades 17
HIT or STAY? hit
4 of Hearts 21
HIT or STAY? STAY
COMPUTER'S TURN
King of Clubs 10
9 of Diamonds 19
YOUR SCORE: 21
COMPUTER'S SCORE: 19
YOU WIN!
PLAY AGAIN? (Y/N) Y
LET'S PLAY 21!
SHUFFLING CARDS...
YOUR TURN
8 of Hearts 8
Queen of Spades 18
HIT or STAY? stay
COMPUTER'S TURN
Jack of Hearts 10
3 of Clubs 13
7 of Spades 20
YOUR SCORE: 18
COMPUTER'S SCORE: 20
YOU LOSE!
PLAY AGAIN? (Y/N) Y
LET'S PLAY 21!
SHUFFLING CARDS...
YOUR TURN
Queen of Hearts 10
5 of Diamonds 15
HIT or STAY? HIT
7 of Hearts 22
YOU LOSE!
PLAY AGAIN? (Y/N) Y
LET'S PLAY 21!
SHUFFLING CARDS...
YOUR TURN
4 of Spades 4
10 of Clubs 14
HIT or STAY? HIT
6 of Hearts 20
HIT or STAY? STAY
COMPUTER'S TURN
5 of Hearts 5
Jack of Spades 15
Ace of Diamonds 16
4 of Clubs 20
YOUR SCORE: 20
COMPUTER'S SCORE: 20
YOU LOSE!
PLAY AGAIN? (Y/N) Y
LET'S PLAY 21!
SHUFFLING CARDS...
YOUR TURN
8 of Hearts 8
9 of Diamonds 17
HIT or STAY? STAY
COMPUTER'S TURN
King of Clubs 10
6 of Spades 16
Jack of Diamonds 26
YOU WIN!
PLAY AGAIN? (Y/N) N
HELPFUL HINTS
Please note that there are several challenging logical problems in this assignment. Here is a good example where you should develop your software in small steps (as real software developers do) rather than as one big whole. Don't try to work on the entire problem at once. Instead work at it in stages, so you always have a working program that solves part of the problem, and then move on to add another feature. (Keep a copy of your working program just in case you run out of time... this way you have something to hand in for grading.) Here's an idea of how to break the problem down.
1. Write the PlayingCard and DeckOfPlayingCard classes. This should be easy. Make sure they compile. Test them with a simple demo program.
2. Now, set up the general framework of the game, using the ACTUAL ranks (1 to 13) for scores. That is, don't worry about the special cases yet! Just get the algorithm for the game set up (allowing the user to hit or stay, displaying the score, letting the computer play, showing final scores).
3. Once you have steps 1 and 2 working correctly, then make the finer changes to take care of scoring for the real game. For example, now add code to treat all 10s, Jacks, Queens, and Kings as 10 points. Then add code to deal with the Aces - this is a little bit more complex logically (for example, what if a player has 2 or more Aces?).
Explanation / Answer
please rate - thanks
message me if any problem
sample run
----jGRASP exec: java TwentyOne
LET'S PLAY 21!
SHUFFLING CARDS...
YOUR TURN
5 of hearts 5
5 of spades 10
HIT or STAY? hit
3 of clubs 13
HIT or STAY? H
Unrecognized input
HIT or STAY? HIT
7 of spades 20
HIT or STAY? stay
COMPUTERS TURN
6 of clubs 6
king of spades 16
ace of hearts 17
3 of hearts 20
YOUR SCORE: 20
COMPUTERS SCORE: 20
TIE GAME
PLAY AGAIN? (Y/N)
y
LET'S PLAY 21!
SHUFFLING CARDS...
YOUR TURN
ace of hearts 11
10 of hearts 21
HIT or STAY? stay
COMPUTERS TURN
jack of clubs 10
6 of spades 16
5 of spades 21
YOUR SCORE: 21
COMPUTERS SCORE: 21
TIE GAME
PLAY AGAIN? (Y/N)
y
LET'S PLAY 21!
SHUFFLING CARDS...
YOUR TURN
4 of spades 4
2 of clubs 6
HIT or STAY? hit
2 of diamonds 8
HIT or STAY? hit
9 of diamonds 17
HIT or STAY? stay
COMPUTERS TURN
king of spades 10
9 of hearts 19
YOUR SCORE: 17
COMPUTERS SCORE: 19
COMPUTER WINS!
PLAY AGAIN? (Y/N)
n
----jGRASP: operation complete.
--------------------------------------------------------------
import java.util.Scanner;
public class TwentyOne
{
public static void main (String[] args)
{Scanner in=new Scanner(System.in);
int cpts,ppts,p;
boolean first;
String hit;
do
{
DeckOfPlayingCards c1=new DeckOfPlayingCards();
PlayingCard t=new PlayingCard();
System.out.println("LET'S PLAY 21! SHUFFLING CARDS... ");
System.out.println("YOUR TURN");
first=true;
c1.shuffle();
cpts=0;
ppts=0;
do
{t=c1.deal();
p=t.getPoints();
if(p==1)
if(ppts+11<=21)
p=11;
ppts+=p;
System.out.println(t.getCard()+" "+ppts);
if(first)
{
hit="hit";
first=false;
}
else
{System.out.print("HIT or STAY? ");
hit=in.nextLine();
}
while(hit.compareToIgnoreCase("hit")!=0&&hit.compareToIgnoreCase("stay")!=0)
{System.out.println("Unrecognized input");
System.out.print("HIT or STAY? ");
hit=in.nextLine();
}
}while(hit.compareToIgnoreCase("hit")==0);
System.out.println(" COMPUTERS TURN");
do
{
t=c1.deal();
p=t.getPoints();
if(p==1)
if(cpts+11<=21)
p=11;
cpts+=p;
System.out.println(t.getCard()+" "+cpts);
}while(cpts<=17);
System.out.println(" YOUR SCORE: "+ppts);
System.out.println("COMPUTERS SCORE: "+cpts);
if(ppts>cpts)
System.out.println("YOU WIN!");
else if(cpts>ppts)
System.out.println("COMPUTER WINS!");
else
System.out.println("TIE GAME");
System.out.print("PLAY AGAIN? (Y/N)");
}while(Character.toUpperCase(in.nextLine().charAt(0))=='Y');
}
}
---------------------------------
public class PlayingCard
{
private int rank;
private int suit;
public PlayingCard()
{}
public PlayingCard(int i, int j)
{rank=i;
suit=j;
}
public String getCard()
{String suits[]={"hearts","diamonds","spades","clubs"};
String card[]={"ace","2","3","4","5","6","7","8","9","10","jack","queen","king"};
return card[rank]+" of "+suits[suit];
}
public int getRank()
{return rank;
}
public int getSuit()
{return suit;
}
public void setRank(int n)
{rank=n;
}
public void setSuit(int n)
{suit=n;
}
public int getPoints()
{int p;
p=rank+1;
if(p>=11)
p=10;
return p;
}
};
------------------------------------
import java.util.*;
public class DeckOfPlayingCards
{private PlayingCard [] c=new PlayingCard[52];
private int top;
public DeckOfPlayingCards()
{
int i,j;
for(i=0;i<4;i++)
for(j=0;j<13;j++)
c[i*13+j]=new PlayingCard(j,i);
shuffle();
}
public void shuffle()
{int i,j;
PlayingCard t=new PlayingCard(0,0);
for(i=0;i<51;i++)
{j=i+(int)(Math.random()*((51-i)+1));
t.setRank(c[i].getRank());
c[i].setRank(c[j].getRank());
c[j].setRank(t.getRank());
t.setSuit(c[i].getSuit());
c[i].setSuit(c[j].getSuit());
c[j].setSuit(t.getSuit());
}
top=0;
}
public PlayingCard deal()
{return c[top++];
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.