Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please fill in the following Code for Java.... The Card.java and CardPile.java are done....please check the BlackjackGame.java and SimpleUI and the UserInterface -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Card.java ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- package

Please fill in the following Code for Java.... The Card.java and CardPile.java are done....please check the BlackjackGame.java and SimpleUI and the UserInterface

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Card.java

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

package xyz;

public class Card implements Comparable {

// Symbolic constants

public static final int CLUB = 0;

public static final int DIAMOND = 1;

public static final int HEART = 2;

public static final int SPADE = 3;

private int rank;

private int suit;

boolean faceUp;

/**

* Construct a card of the given rank, suit and whether it is faceup or

* facedown. The rank is an integer from 2 to 14. Numbered cards (2 to 10)

* have a rank equal to their number. Jack, Queen, King and Ace have the

* ranks 11, 12, 13, and 14 respectively. The suit is an integer from 0 to 3

* for Clubs, Diamonds, Hearts and Spades respectively.

*

* @param rank

* @param suit

* @param faceUp

*/

public Card(int rank, int suit, boolean faceUp) {

this.rank = rank;

this.suit = suit;

this.faceUp = faceUp;

}

/**

* @return the faceUp

*/

public boolean isFaceUp() {

return faceUp;

}

/**

* @param faceUp

* the faceUp to set

*/

public void setFaceUp(boolean faceUp) {

if (!this.faceUp) {

this.faceUp = faceUp;

}

}

/**

* @return the rank

*/

public int getRank() {

return rank;

}

/**

* @return the suit

*/

public int getSuit() {

return suit;

}

@Override

public boolean equals(Object ob) {

Card c = (Card) ob;

if (c.rank == rank && c.suit == suit) {

return true;

}

return false;

}

@Override

public int hashCode() {// DO NOT MODIFY

int hash = 7;

hash = 31 * hash + this.getSuit();

return hash;

}

@Override

public int compareTo(Object obj) {// DO NOT MODIFY

return compareTo((Card) obj);

}

public int compareTo(Card c) {

int a = 0;

if (c.rank > rank) {

a = -1;

} else if (c.rank < rank) {

a = 1;

}

if (c.rank == rank) {

if (c.suit > suit) {

a = -1;

} else if (c.suit < suit) {

a = 1;

} else {

a = 0;

}

}

return a;

}

/**

* Return the rank as a String. For example, the 3 of Hearts produces the

* String "3". The King of Diamonds produces the String "King".

*

* @return the rank String

*/

public String getRankString() {

if(rank == 14) {

return "Ace";

} else if(rank == 13) {

return "King";

} else if(rank == 12) {

return "Queen";

} else if(rank == 11) {

return "Jack";

}

return "" + rank;

}

/**

* Return the suit as a String: "Clubs", "Diamonds", "Hearts" or "Spades".

*

* @return the suit String

*/

public String getSuitString() {

if (suit == CLUB) {

return "Clubs";

}

if (suit == DIAMOND) {

return "Diamonds";

}

if (suit == HEART) {

return "Hearts";

}

return "Spades";

}

/**

* Return "?" if the card is facedown; otherwise, the rank and suit of the

* card.

*

* @return the String representation

*/

@Override

public String toString() {

if (faceUp == false) {

return "?";

}

String s = "" + getRankString() + " of " + getSuitString();

return s;

}

public static void main(String[] args) {

// Create 5 of clubs

Card club5 = new Card(5, 0, true);

System.out.println("club5: " + club5);

Card spadeAce = new Card(14, SPADE, true);

System.out.println("spadeAce: " + spadeAce);

System.out.println("club5 compareTo spadeAce: "

+ club5.compareTo(spadeAce));

System.out.println("club5 compareTo club5: " + club5.compareTo(club5));

System.out.println("club5 equals spadeAce: " + club5.equals(spadeAce));

System.out.println("club5 equals club5: " + club5.equals(club5));

}

}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------CardPile.java

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package xyz;

import java.util.ArrayList;

public class CardPile {

//Instance variables

private ArrayList cards;

public CardPile() {

cards = new ArrayList<>();

}

/**

* Add a card to the pile.

* @param card

*/

public void add(Card card) {

if(cards.indexOf(card) == -1)

cards.add(card);

}

/**

* Remove a card chosen at random from the pile.

* @return

*/

public Card removeRandom() {

int index = (int) (Math.random()*cards.size());

Card c = cards.remove(index);

return c;

}

/**

* The string representation is a space separated list

* of each card.

* @return

*/

@Override

public String toString() {

StringBuffer sb = new StringBuffer();

for(Card c: cards) {

sb.append(c + " ");

}

return sb.toString();

}

/**

* @return the cards

*/

public ArrayList getCards() {

return cards;

}

public static void main(String[] args) {

CardPile p = new CardPile();

p.add(new Card(2, 1, true));

p.add(new Card(3, 2, true));

p.add(new Card(4, 3, false));

p.add(new Card(14, 1, true));

System.out.println("Removed: " + p.removeRandom());

System.out.println("Removed: " + p.removeRandom());

System.out.println("Removed: " + p.removeRandom());

System.out.println("Removed: " + p.removeRandom());

System.out.println("");

CardPile deck = new CardPile();

for(int i = 2; i < 15; i++) {

for(int j = 0; j < 4; j++) {

deck.add(new Card(i, j, true));

}

}

for (int i = 0; i < 52; i++) {

System.out.println((i+1) + ": " + deck.removeRandom());

}

}

}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------BlackjackGame.java

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package xyz;

/**

*

*

*/

public class BlackjackGame {

private CardPile deck;

private CardPile houseCards;

private CardPile yourCards;

private boolean houseDone;

private boolean playerDone;

private UserInterface ui;

public BlackjackGame(UserInterface ui) {

this.ui = ui;

ui.setGame(this);

deck = new CardPile();

for (int i = 2; i < 15; i++) {

for (int j = 0; j < 4; j++) {

deck.add(new Card(i, j, true));

}

}

houseCards = new CardPile();

yourCards = new CardPile();

houseDone = false;

playerDone = false;

}

public void start() {

Card c;

c = deck.removeRandom();

c.setFaceUp(false);

getHouseCards().add(c);

getHouseCards().add(deck.removeRandom());

getYourCards().add(deck.removeRandom());

getYourCards().add(deck.removeRandom());

ui.display();

}

public void play() {

while (!houseDone || !playerDone) {

if (!houseDone) {

if (score(getHouseCards()) <= 17) {

getHouseCards().add(deck.removeRandom());

ui.display();

} else {

houseDone = true;

}

}

if (!playerDone) {

if (ui.hitMe()) {

getYourCards().add(deck.removeRandom());

ui.display();

} else {

playerDone = true;

}

}

}

}

public void end() {

getHouseCards().getCards().get(0).setFaceUp(true);

ui.gameOver();

}

/**

* Determine the score of a pile of cards.

*

* @param p

* @return the score

*/

public int score(CardPile p) {

int score = 0;

for(Card House: p.getCards())

{

switch(House.getRank())

{

case 2 : score = score + 2;

break;

case 3 : score = score + 3;

break;

case 4 : score = score + 4;

break;

case 5 : score = score +5;

break;

case 6 : score = score +6;

break;

case 7: score = score + 7;

break;

case 8 : score = score + 8;

break;

case 9 : score = score + 9;

break;

case 10 : score = score + 10;

break;

case 11 : score = score + 10;

break;

case 12 : score = score + 10;

break;

case 13 : score = score + 10;

break;

case 14 : score = score + 1;

break;

default: score = 0;

}

}

return score;

}

/**

* @return the houseCards

*/

public CardPile getHouseCards() {

return houseCards;

}

/**

* @return the yourCards

*/

public CardPile getYourCards() {

return yourCards;

}

public static void main(String[] args) {

BlackjackGame game = new BlackjackGame(new SimpleUI());

game.start();

game.play();

game.end();

}

}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------SimpleUI.java

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package xyz

import java.util.Scanner;

/**

*

*

*/

public class SimpleUI implements UserInterface{

private BlackjackGame game;

private Scanner user = new Scanner(System.in);

//@Override

public void setGame(BlackjackGame game) {

this.game = game;

}

/**

* Display the cards held by the House

* and the player (you).

*/

//@Override

public void display() {

}

/**

* Prompt the user if they want another

* card. Return true if another card

* desired.

* @return

*/

//@Override

public boolean hitMe() {

boolean another = false;

Scanner answer = new Scanner(System.in);

System.out.println("Another card? ");

String ans = answer.nextLine();

if(ans.equals("y")){

another = true;

}

if (ans.equals("n")){

another = false;

}

return another;

}

/**

* Display the final cards, scores

* and winner.

*/

//@Override

public void gameOver() {

//FIX THIS

}

}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------UserInterface.java

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package xyz;

/**

*

*

*/

public interface UserInterface {

/** Set the Blackjack game

* this UserInterface belongs to.

* @param game

*/

public void setGame(BlackjackGame game);

/**

* Display the cards held by the House

* and the player (you).

*/

public void display();

/**

* Prompt the user if they want another

* card. Return true if another card

* desired.

* @return

*/

public boolean hitMe();

/**

* Display the final cards, scores

* and winner.

*/

public void gameOver();

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions