Question

In: Computer Science

Java: Simple 21 Game (Blackjack) In this game, the dealer deals two "cards" to each player,...

Java: Simple 21 Game (Blackjack)

In this game, the dealer deals two "cards" to each player, one hidden, so that only the player who gets it knows what it is, and one face up, so that everyone can see it. There are four players: one human player (user) and three computer players. The players take turns requesting cards, trying to get as close to 21 as possible, but not going over 21. A player may pass. Once a player has passed, he or she cannot later ask for another card. When all players have passed, the game ends. The winner is the player who has come closest to 21 without exceeding it. In the case of a tie, or if everyone goes over 21, no one wins. The game is only played once (so it's actually just one "hand"). The "cards" are the numbers 1 through 10 and they are randomly generated, not drawn from a deck of limited size.

There are 3 Classes:

The GameControl Class - The GameControl class controls the entire game. It has a main method which serves as the entry point to the entire program and it will be the file you will “run”. The GameControl class will take care of creating the four players, dealing the initial two cards to each player, controlling the play of the game, and printing the final results of the game. The GameControl class should have (at least) the following instance variables: human, player1, player2, player3, and random.

The HumanPlayer Class - The HumanPlayer class represents a human player in the game. The HumanPlayer class will take care of looking at his/her own hidden and visible cards, the other players’ visible cards, and deciding whether to take another card. The HumanPlayer class should have (at least) the following instance variables: name, hiddenCard, sumOfVisibleCards, and passed.

The ComputerPlayer Class - The ComputerPlayer class represents a computer player in the game. The ComputerPlayer class will take care of looking at its own hidden and visible cards, the other players’ visible cards, and deciding whether to take another card. The ComputerPlayer class should also have (at least) the following instance variables: name, hiddenCard, sumOfVisibleCards, and passed.

THE THREE SETS OF PROVIDED CODE CAN BE FOUND HERE: https://gofile.io/d/iPp1E7

Evaluation -

1. Did you set up the project correctly? Does it compile and is everything named correctly? (2 pts)

2. Does your code function? Does it do what the specifications require? (9 pts) a. Can we play the game? Does the print out make sense? b. Does the game accurately pick the winner (if there is one)?

3. Does your code pass our tests? (**Autograded**) (5 pts)

4. Did you follow good programming practices? (3 pts) a. Did you reuse code to avoid repetition (e.g. put repeated code in a helper method)? b. Did you add javadocs to methods and comment all non-trivial code? c. Did you indent properly (Cmd+i or Ctrl+i) and use { brackets } correctly for code blocks? d. Did you name variables descriptively with camelCase?

5. Did you submit the correct file (entire Java project in a .zip) and nothing else? (1 pt)

Solutions

Expert Solution

import java.util.Random;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Calendar;

public class black {

static Calendar cal = Calendar.getInstance();
static Random r = new Random(cal.getTimeInMillis());
static Scanner s = new Scanner(System.in);

public static void main(String args[])
{
ArrayList<Integer> cards = drawTwo();   
  
System.out.println("You drew " + cards.get(0) + " and " + cards.get(1) + " giving you a current score of " + cardsTotal(cards));

if(cardsTotal(cards) == 21)
{
System.out.println("Congratulations, you got 21!");
return;
}

System.out.println("Press 1 to draw another card. Press 2 to stand");
playCards(cards);
}
  
public static void playCards(ArrayList<Integer> cards)
{
int choice = getChoice();
if(choice == 1)
{
int newCard = drawCard();
cards.add(newCard);
System.out.println("You drew a " + newCard + ".");
if(cardsTotal(cards) == 21)
{
System.out.println("Congratulations, you got 21!");
return;
}
else if(cardsTotal(cards) > 21)
{
System.out.println("Whoops, you have too many points");
return;
}
else {
System.out.println("You currently have " + cardsTotal(cards) + " cards. Press 1 or 2 again");
playCards(cards);
}
}

else {
System.out.println("Your final score is " + cardsTotal(cards));
return;
}
}

public static int drawCard()
{
int card = r.nextInt(13) + 1;
if( card == 1)
{
card = 11;
}
else if( card > 10)
{
card = 10;
}
return card;
}

public static ArrayList<Integer> drawTwo()
{
ArrayList<Integer> cards = new ArrayList<Integer>();
for(int i = 0; i < 2; i++)
{
cards.add(drawCard());
}
return cards;
}

public static int cardsTotal(ArrayList<Integer> cards)
{
int total = 0;
for(int card :cards)
{
total += card;
}
return total;
}

public static int getChoice()
{
int choice = 0;
while(choice != 1 && choice != 2)
{
String next = s.next();
try{
choice = Integer.parseInt(next);
}
catch(Exception e)
{
System.out.println("That was not a valid input");
}
}
return choice;
}

}


Related Solutions

Blackjack, or 21, is a popular casino game that begins with each player and the dealer...
Blackjack, or 21, is a popular casino game that begins with each player and the dealer being dealt two cards. The value of each hand is determined by the point total of the cards in the hand. Face cards and 10s count 10 points, aces can be counted as either 1 or 11 points, and all other cards count at their face value. For instance, the value of a hand consisting of a jack and an 8 is 18; the...
CSharp Simple BlackJack game Also known as 21. Two players (the user and computer) play against...
CSharp Simple BlackJack game Also known as 21. Two players (the user and computer) play against each other. They are drawing poker cards in order to •Make the added rank (score) as close as to 21, but without going over 21.•And make the added score larger than the opponent’s.−Each player starts with two random cards. −Then ask the user if one more card until the user is satisfied with his/her total score. If the user’s total score gets 21, announce...
The game of Pig is a simple two-player dice game in which the first player to...
The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. How to play: Players take turns rolling one six-sided dice and following these rules: If the player rolls 2 through 6, then he/she can either a. “Roll Again” or b. “Hold” At this point, the sum of all rolls is added to the player’s score, and it becomes the other player’s turn. If the player rolls 1 before...
A standard deck of cards has 52 cards, four each of 2,3,4,5,6,7,8,9,10,J,Q,K,A. In blackjack, a player...
A standard deck of cards has 52 cards, four each of 2,3,4,5,6,7,8,9,10,J,Q,K,A. In blackjack, a player gets two cards and adds their values. Cards count as their usual numbers, except Aces are 11 (or 1), while K, Q, J are all 10. “Blackjack” means getting an Ace and a value ten card. What is probability of getting a blackjack? What is probability of getting 19? (The probability that the sum of your cards is 19, using Ace as 11) Use...
PYTHON GAME OF PIG The game of Pig is a simple two player dice game in...
PYTHON GAME OF PIG The game of Pig is a simple two player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn a player rolls a six-sided die. After each roll: a) If the player rolls a 1 then the player gets no new points and it becomes the other player’s turn. b) If the player rolls 2-6 then they can either roll again or hold. If the player...
Your goal is to collect all 80 player cards in a game. The Player cards are...
Your goal is to collect all 80 player cards in a game. The Player cards are numbered 1 through 80. High numbered cards are rarer/more valuable than lower numbered cards. Albert has a lot of money to spend and loves the game. So every day he buys a pack for $100. Inside each pack, there is a random card. The probability of getting the n-th card is c(1.05)-n, For some constant c. Albert buys his first pack on June 1st....
In a particular card​ game, each player begins with a hand of 3 ​cards, and then...
In a particular card​ game, each player begins with a hand of 3 ​cards, and then draws 6 more. Calculate the probability that the hand will contain one pair​ (2 cards of one​ value, with the other cards of 7 different​ values).
In the game of blackjack, the cards 2 through 10 are counted at their face values,...
In the game of blackjack, the cards 2 through 10 are counted at their face values, regardless of suit; all face cards (jack, queen, and king) are counted as 10; and an ace is counted as a 1 or 11, depending on the total count of all cards in a player’s hand. The ace is counted as 11 only if the resulting total value of all cards in a player’s hand doesn’t exceed 21; otherwise, it’s counted as 1. Using...
(General math) In the game of blackjack, the cards 2 through 10 are counted at their...
(General math) In the game of blackjack, the cards 2 through 10 are counted at their face values, regardless of suit; all face cards (jack, queen, and king) are counted as 10; and an ace is counted as a 1 or an 11, depending on the total count of all cards in a player’s hand. The ace is counted as 11 only if the resulting total value of all cards in a player’s hand doesn’t exceed 21; otherwise, it’s counted...
Play a blackjack. Write a Java program that starts from a deck of 52 cards and...
Play a blackjack. Write a Java program that starts from a deck of 52 cards and one player plays again a dealer. Use a simple rule explained here. Ace can be counted as 1 only for simplicity. Jack, Queen, King are counted as 10. At the beginning, player receives two cards. Dealer receives two cards, but shows one and hides one. Program asks player if player wants to receive another card or not. (player can continue to receive new cards...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT