need help writing this java program here are the directions/explanation: probabl
ID: 3864274 • Letter: N
Question
need help writing this java program here are the directions/explanation:
probably going to need a DECK CLass for the deck of cards that can shuffle. and then you are going to need a Card class that has the suit and rank (number for example ,2 ,3 4, 5,6,7,8,9,10, Jack, queen king ace) these classes need to be able to be accesed by the computer class and the human player class/main class.
Your task is to write a program that plays a simple card game with one human player and one computer player. The game uses a deck of 52 cards. Each card has a value (either a number 2 through 10 or a label jack, queen, king, or ace) and a suit (hearts, diamonds, clubs or spades).
***Main Class** needs this below*
The objective of the game is to get four cards with the same value. For example, the 8 of hearts, diamonds, clubs and spades. At the beginning of the game, the cards are shuffled and each player is given four cards. The remaining cards are placed into a queue called the “draw pile.” There is another pile called the “discard pile” that starts out empty(empty queue). The discard pile is a stack (an ArrayDeque in Java).
If neither player has been dealt a winning hand, the players take turns until one of them wins. At each turn the player can either draw a new card from the draw pile or pick up the top card on the discard pile.
******HUMAN PLAYER CLASS**** what it needs below:
In this game the human player always goes first. Because the discard pile starts out empty, the human player must initially pick up a card from the draw pile. After that, the human player must select a card to put into the discard pile. This can either be the card he drew or one that was already in his hand. If the human player now has four cards with the same value, he wins.
COMPUTER PLAYER CLASS*********
Otherwise, it is the computer’s turn. The computer can either draw a card from the draw pile or pick up the card that the human player put onto the discard pile.Then the computer player must place one of its cards onto the discard pile. If the computer player now has four cards with the same value, it wins.
Otherwise, it is the human player’s turn; however, this time the human player can either take a card from the draw pile or pick up the card that the computer player put onto the discard pile. This process repeats until one player wins. At that point, the game should display the message “You win!” if the human player won or “I win!” if the computer player won. The program should then terminate. An example execution of this game is shown in the program output below
******************here is what the output should look like:*****************************
Your cards are:
Queen of Hearts
Seven of Hearts
Eight of Hearts
Seven of Diamonds
The discard pile is currently empty -- you must draw a card
You drew the Five of Spades
Now your cards are:
1. Queen of Hearts
2. Seven of Hearts
3. Eight of Hearts
4. Seven of Diamonds
5. Five of Spades
Which one do you want to discard?
1 I will draw a new card.
I will discard the Two of Spades
Your cards are:
Five of Spades
Seven of Hearts
Eight of Hearts
Seven of Diamonds
The top card in the discard pile is the Two of Spades
Do you want to pick up the Two of Spades (1) or draw a card (2)?
2
You drew the Queen of Diamonds
Now your cards are:
1. Five of Spades
2. Seven of Hearts
3. Eight of Hearts
4. Seven of Diamonds
5. Queen of Diamonds
Which one do you want to discard?
1
I will pick up the Five of Spades
I will discard the Six of Diamonds
Your cards are:
Queen of Diamonds
Seven of Hearts
Eight of Hearts
Seven of Diamonds
The top card in the discard pile is the Six of Diamonds
Do you want to pick up the Six of Diamonds (1) or draw a card (2)?
2
You drew the Five of Diamonds Now your cards are:
1. Queen of Diamonds
2. Seven of Hearts
3. Eight of Hearts
4. Seven of Diamonds
5. Five of Diamonds Which one do you want to discard?
1
I will draw a new card.
I will discard the King of Spades
Your cards are:
Five of Diamonds
Seven of Hearts
Eight of Hearts
Seven of Diamonds
The top card in the discard pile is the King of Spades
Do you want to pick up the King of Spades (1) or draw a card (2)?
2
You drew the Nine of Diamonds Now your cards are:
1. Five of Diamonds
2. Seven of Hearts
3. Eight of Hearts
4. Seven of Diamonds
5. Nine of Diamonds
Which one do you want to discard?
5
****
Your program will be graded according to this rubric (each item is worth one point):
• The program creates the deck of cards, shuffles it, and gives each player four cards.
• The human player is notified what card is on top of the discard pile (which is implemented as a stack), and he can either take that card or draw a card from the draw pile (which is implemented as a queue).
• The human player can discard either a card that was already in his hand or the card he just acquired during his turn. This card is then placed on top of the discard pile.
• The computer player is capable of both taking a card from the draw pile or picking up the card on top of the discard pile, and it does not do the same thing every time. It may choose which to do either randomly or according to some strategy, whichever you prefer.
• The computer player does not always discard the same card every time (i.e. it doesn’t always discard the card it just picked up). It may choose which card to discard randomly or according to some strategy, whichever you prefer.
• If the draw pile becomes empty before either player has won the game, all of the cards in the discard pile are shuffled and moved back to the draw pile.
• The program correctly recognizes when someone wins and displays the appropriate message.
Explanation / Answer
Here is the code with comments. Please check and rate the answer if you are happy with the program. Your feedback is valuable.Thanks
Card.java
//card class implement comparable interface so that it can be sorted
public class Card implements Comparable<Card>{
int value; //A,2,....10,J,Q,K have these values respectively - 1,2,....10,11,12,13 i.e A=1, J=11, Q=12, K=13
char suit; //H,S,D,C
static private String[] VALUE_NAMES={"","Ace","Two","Three","Four","Five","Six",
"Seven","Eight","Nine","Ten","Jack","Queen","King"};
public Card(int _value,char _suit)
{
value=_value;
suit=_suit;
}
int getValue()
{
return value;
}
char getSuit()
{
return suit;
}
public int compareTo(Card c)
{
if(value<c.value)
return -1;
else if(value>c.value)
return 1;
else
return 0;
}
public String toString()
{
String str=VALUE_NAMES[value] + " of ";
if(suit=='H')
str+="Hearts";
else if(suit=='S')
str+="Spade";
else if(suit=='D')
str+="Diamonds";
else
str+="Club";
return str;
}
}
Player.java
import java.util.ArrayList;
import java.util.Collections;
public class Player {
String name;
public Player(String _name)
{
name=_name;
}
ArrayList<Card> cards;
public Player()
{
cards=new ArrayList<Card>(); //maximum card a player can have
}
public void addCard(Card card)
{
cards.add(card);
Collections.sort(cards);//always keep the cards sorted so it is easy to pick a card to discard
}
//removes the card from the list of cards
public Card discard(int index)
{
return cards.remove(index);
}
public Card getCard(int index)
{
return cards.get(index);
}
//returns whether a player has won or not
public boolean hasWon()
{
int value=cards.get(0).getValue();//get value of first card
for(int i=1;i<cards.size();i++) //all other cards should be same value as first card to win
{
if(cards.get(i).getValue()!=value) //no match
{
return false;
}
}
return true;
}
//displays the list of cards with player
public void displayCards()
{
for(int i=0;i<cards.size();i++)
{
System.out.println(i+1+". "+cards.get(i));
}
}
}
ComputerPlayer.java
//Computerplayer is a player with added functionality to decide which card to discard
public class ComputerPlayer extends Player {
//return the index of the card the computer wants to discard
private int decideDiscard()
{
//since the cards are always kept sorted,we will count which value is occuring lowest number of times
//and discard one of the cards in that value
int prev_value=cards.get(0).getValue(),prev_count=0,index=0,count=1,min=1;
for(int i=1;i<cards.size();i++)
{
if(cards.get(i).getValue()!=prev_value)
{
if(count<min)
{
min=count;
index=i-1;
prev_value=cards.get(i).getValue();
}
count=1;
}
else
count++;
}
return index;
}
public Card discard()
{
return cards.remove(decideDiscard());
}
public int getMaxValue()
{
int prev_value=cards.get(0).getValue(),count=1,max=1;
for(int i=1;i<cards.size();i++)
{
if(cards.get(i).getValue()!=prev_value)
{
if(count>max)
{
max=count;
prev_value=cards.get(i).getValue();
}
count=1;
}
else
count++;
}
return prev_value;
}
}
Game.java
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
//class to represent the card game
public class Game {
ArrayList<Card> cardspile;//deck of cards;
ComputerPlayer computer;//represents the computer player
Player human;
Stack<Card> discardpile; //the stack of discard pile
public Game()
{
char suit[]={'H','S','D','C'} ;//heart,spade,diamond,club
cardspile=new ArrayList<Card>();
//generate 13 cards for each suit and add it deck
for(int i=0;i<4;i++) //for each suit
{
for(int j=1;j<=13;j++) //for each value
{
cardspile.add(new Card(j,suit[i]));
}
}
discardpile=new Stack<Card>();
human=new Player();//create a human player
computer=new ComputerPlayer();//create the computer player
}
private void shuffle()
{
int size=cardspile.size();
Card card;
Random random=new Random();
//repeat as many times as the umber of cards in deck. pull out a random card and add it end of the deck
for(int i=0;i<size;i++)
{
card=cardspile.remove(random.nextInt(size));
cardspile.add(card);
}
}
//start the game
public void start()
{
//first shuffle the deck
shuffle();
//give 4 cards each to human and then to computer
for(int i=0;i<4;i++)
{
human.addCard(cardspile.remove(0)); //remove a card from top always and assign
computer.addCard(cardspile.remove(0));
}
int turn=0;
int choice;
Card current,top;
Scanner scanner=new Scanner(System.in);
while(!human.hasWon() && !computer.hasWon()) //as long as human or computer has not won
{
if(cardspile.isEmpty()) //check if pile is empty if so get all from discard pile and shuffle
{
cardspile.addAll(discardpile);
discardpile.clear();
shuffle();
}
if(turn==0)
{
System.out.println("Your cards are ");
human.displayCards();
if(discardpile.isEmpty())
{
choice=2;
System.out.println("The discard pile is empty--you must draw a card");
}
else
{
top=discardpile.peek();
System.out.println("The top of the discard pile is the "+top);
System.out.println("Do you want to pick up the "+top.toString()+"(1) or draw a card (2)");
choice=scanner.nextInt();
}
if(choice==1)
{
current=discardpile.pop();
System.out.println("You picked up "+current);
}
else
{
current=cardspile.remove(0);
System.out.println("You drew "+current);
}
human.addCard(current);
System.out.println("Now your cards are ");
human.displayCards();
System.out.println("Which one do you want to discard ?");
choice=scanner.nextInt();
//discard the card from human and push it on discard pile
discardpile.push(human.discard(choice-1));//index starts from 0
}
else
{
//check if discard pile is not empty
//computer picks up if the discard pile is showing a card value which it has more
if(!discardpile.isEmpty() && discardpile.peek().getValue()==computer.getMaxValue())
{
top=discardpile.pop();
System.out.println("I will pick up the "+top);
computer.addCard(top);
}
else
{
System.out.println("I will draw a new card.");
current=cardspile.remove(0);
computer.addCard(current);
}
current=computer.discard();
discardpile.push(current);
System.out.println("I will discard the "+current);
}
turn=1-turn; //change turn
}
}
public void finish()
{
System.out.println("____________________________");
System.out.println("My cards are ");
computer.displayCards();
System.out.println("____________________________");
System.out.println("Your cards are ");
human.displayCards();
System.out.println("____________________________");
if(human.hasWon())
{
System.out.println("You won !");
}
else
{
System.out.println("I win !");
}
System.out.println("____________________________");
}
public static void main(String[] args) {
Game game=new Game();
game.start();
game.finish();
}
}
Sample output
Your cards are
1. Ace of Spade
2. Three of Hearts
3. Five of Spade
4. Nine of Spade
The discard pile is empty--you must draw a card
You drew Four of Diamonds
Now your cards are
1. Ace of Spade
2. Three of Hearts
3. Four of Diamonds
4. Five of Spade
5. Nine of Spade
Which one do you want to discard ?
1
I drew the Six of Diamonds
I will discard the Two of Spade
Your cards are
1. Three of Hearts
2. Four of Diamonds
3. Five of Spade
4. Nine of Spade
The top of the discard pile is the Two of Spade
Do you want to pick up the Two of Spade(1) or draw a card (2)
2
You drew Jack of Diamonds
Now your cards are
1. Three of Hearts
2. Four of Diamonds
3. Five of Spade
4. Nine of Spade
5. Jack of Diamonds
Which one do you want to discard ?
5
I drew the Queen of Diamonds
I will discard the Six of Diamonds
Your cards are
1. Three of Hearts
2. Four of Diamonds
3. Five of Spade
4. Nine of Spade
The top of the discard pile is the Six of Diamonds
Do you want to pick up the Six of Diamonds(1) or draw a card (2)
2
You drew Ace of Club
Now your cards are
1. Ace of Club
2. Three of Hearts
3. Four of Diamonds
4. Five of Spade
5. Nine of Spade
Which one do you want to discard ?
1
I drew the Two of Club
I will discard the Two of Club
Your cards are
1. Three of Hearts
2. Four of Diamonds
3. Five of Spade
4. Nine of Spade
The top of the discard pile is the Two of Club
Do you want to pick up the Two of Club(1) or draw a card (2)
2
You drew Eight of Club
Now your cards are
1. Three of Hearts
2. Four of Diamonds
3. Five of Spade
4. Eight of Club
5. Nine of Spade
Which one do you want to discard ?
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.