In: Computer Science
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)
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;
}
}