Please help write a Java application that allows a user to play Blackjack agains
ID: 3567309 • Letter: P
Question
Please help write a Java application that allows a user to play Blackjack against the computer. That is, the computer will act as the house, dealing the cards and paying when you win. For this assignment, implement just a simple version of the game using the following basic rules:
This application does not include betting nor does it track a player's performance; it simply plays one hand at a time and declares a winner after each hand.
There are many other variations, including splitting, doubling down, surrender possibilities, insurance, and more. Please do not implement any of these rules.
Classes: Your application must include the following six classes: BlackJack (the test class), Card, Deck, Player, Dealer, and Game. The BlackJack class and the Card class are complete. Do not change them. For the other four classes I have included partially complete templates to help get you started. NOTE: Your application must use all of these and must work with the BlackJack test class. For maximum points all I/O operations (printing to the monitor and reading from the keyboard) should happen in the Game class or the Player class.
BlackJack class: (DO NOT CHANGE)
public class BlackJack {
public static void main(String[] args){
Game blackjack = new Game();
blackjack.play();
System.out.println("Have a great day!");
}
}
Card Class: (DO NOT CHANGE)
public class Card {
private String suit;
private int value;
public Card(int aValue, String aSuit){
value=aValue;
suit=aSuit;
}
public String toString(){
String myCard;
if(value==1)//if the card is an Ace
myCard="Ace of ";
else if(value==11)//Jack
myCard="Jack of ";
else if(value==12)//Queen
myCard="Queen of ";
else if(value==13)//King
myCard="King of ";
else //any number/suit between 2-10
myCard=value +" of ";
//Adds the suit type to the card value
myCard+=suit;
return myCard;
}
public int getValue(){
return value;
}
}
Deck Class:
public class Deck {
private Card[] cards;
private int cardsDrawn;
//any more instance variables you may want here
public Deck(){
// This constructor should instatiate 52 distinct Card
// objects and place them in the cards array.
// your code here
}
public Card draw(){
// this method deals the top card of the deck
// your code here
}
public void shuffle(){
// this method shuffles the deck and resets cardsdrawn
// your code here
}
public int getCardsDrawn(){
// leave this method as is.
// This is for the graders to test your code.
return cardsDrawn;
}
public String toString(){
// This method should return a string consisting of 52 lines
// each line should contain a description of the card in the
// deck at the corresponding position top-to-bottom
// your code here
}
// any more methods you may need here
}
Dealer Class:
public class Dealer {
private ArrayList<Card> hand; // the dealer's cards
private int handTotal; // The total value of the hand
// add any more instance variables you need here
public Dealer(){
// Dealer constructor
// your code here
public int getTotal(){
// leave this method as is
return handTotal;
}
// add more methods as needed here
}
Game Class:
import java.util.Scanner;
public class Game {
private Deck deck;
private Player player;
private Dealer dealer;
private Scanner input;
// you may wish to have more instance variables
// include your methods for this class below
}
Player Class:
import java.util.Scanner;
public class Player {
private ArrayList<Card> hand; // the player's cards
private int handTotal; // The total value of the hand
private Scanner input;
// add any more instance variables you need here
public Player(){
// player constructor
// your code here
}
public int getTotal(){
// leave this method as is
return handTotal;
}
// add more methods as needed here
}
Explanation / Answer
game.java
import java.util.Scanner;
public class Game {
private Deck deck;
private Player player;
private Dealer dealer;
private Scanner input;
// you may wish to have more instance variables
// include your methods for this class below
public void play() {
input = new Scanner(System.in);
int hand = 1;
do {
deck = new Deck();
player = new Player();
dealer = new Dealer();
deck.shuffle();
boolean anyWinner = true;
while (player.getTotal() <= 21 && dealer.getTotal() <= 21) {
if (deck.getCardsDrawn() > 52) {
anyWinner = false;
break;
}
if (dealer.hit(deck)) {
System.out.println("Dealer hits !");
if (dealer.getTotal() > 21) {
System.out.println("Player Looses !");
break;
}
} else {
System.out.println("Dealer Folds !");
if (dealer.getTotal() > player.getTotal()) {
System.out.println("Dealer wins !!");
} else {
System.out.println("player wins !!");
}
break;
}
if (player.hit(deck)) {
System.out.println("Player Hits !");
if (player.getTotal() > 21) {
System.out.println("Player Looses !");
break;
}
} else {
System.out.println("Player Folds !");
if (dealer.getTotal() > player.getTotal()) {
System.out.println("Dealer wins !!");
} else {
System.out.println("player wins !!");
}
break;
}
}
if (anyWinner == false) {
System.out.print("no winner !!");
}
System.out.println("");
System.out.println("Enter 1 to play another hand: ");
hand = input.nextInt();
} while (hand == 1);
}
}
dealer.java
import java.util.ArrayList;
public class Dealer {
private ArrayList<Card> hand; // the dealer's cards
private int handTotal; // The total value of the hand
// add any more instance variables you need here
public Dealer() {
// Dealer constructor
// your code here
hand = new ArrayList<Card>();
handTotal = 0;
}
public int getTotal() {
// leave this method as is
return handTotal;
}
public boolean hit(Deck d)
{
if(handTotal <= 16)
{
Card drawnCard = d.draw();
String x = drawnCard.toString();
String[] t =x.split(" ");
x = t[t.length - 1];
Card c = new Card(drawnCard.getValue(), x);
hand.add(c);
handTotal += drawnCard.getValue();
return true;
}
return false;
}
// add more methods as needed here
}
player.java
import java.util.Scanner;
import java.util.ArrayList;
public class Player {
private ArrayList<Card> hand; // the player's cards
private int handTotal; // The total value of the hand
private Scanner input;
// add any more instance variables you need here
public Player() {
// player constructor
// your code here
hand = new ArrayList<Card>();
handTotal = 0;
}
public int getTotal() {
// leave this method as is
return handTotal;
}
public boolean hit(Deck d) {
System.out.println("Enter 1 to draw: ");
if (input.nextInt() == 1) {
Card drawnCard = d.draw();
String x = drawnCard.toString();
String[] t = x.split(" ");
x = t[t.length - 1];
Card c = new Card(drawnCard.getValue(), x);
hand.add(c);
handTotal += drawnCard.getValue();
return true;
} else {
return false;
}
}
// add more methods as needed here
}