Question

NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET...

NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET CODE TO RUN!! THANKS

Extend the DeckofCards and the Card class in the book to implement a card game application such as BlackJack, Texas poker or others.

Your game should support multiple players (up to 5 for BlackJack).

You must build your game based on the Cards and DeckofCards class from the book.

You need to implement the logic of the game.

You can consider to add a class for Player.

Hint:

Include a UML based problem solving process in your submission.

// Card class represents a playing card.

public class Card 
{
   private String face; // face of card ("Ace", "Deuce", ...)
   private String suit; // suit of card ("Hearts", "Diamonds", ...)

   // two-argument constructor initializes card's face and suit
   public Card( String cardFace, String cardSuit )
   {
      face = cardFace; // initialize face of card
      suit = cardSuit; // initialize suit of card
   } // end two-argument Card constructor

   // return String representation of Card
   public String toString() 
   { 
      return face + " of " + suit;
   } // end method toString
} // end class Card

public class DeckOfCards
{
private Card deck[]; // array of Card objects
private int currentCard; // index of next Card to be dealt
private final int NUMBER_OF_CARDS = 52; // constant number of Cards
private Random randomNumbers; // random number generator

// constructor fills deck of Cards
public DeckOfCards()
{
String faces[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };

deck = new Card[ NUMBER_OF_CARDS ]; // create array of Card objects
currentCard = 0; // set currentCard so first Card dealt is deck[ 0 ]
randomNumbers = new Random(); // create random number generator

// populate deck with Card objects
for ( int count = 0; count < deck.length; count++ )
deck[ count ] =
new Card( faces[ count % 13 ], suits[ count / 13 ] );
} // end DeckOfCards constructor

// shuffle deck of Cards with one-pass algorithm
public void shuffle()
{
// after shuffling, dealing should start at deck[ 0 ] again
currentCard = 0; // reinitialize currentCard

// for each Card, pick another random Card and swap them
for ( int first = 0; first < deck.length; first++ )
{
// select a random number between 0 and 51
int second = randomNumbers.nextInt( NUMBER_OF_CARDS );

// swap current Card with randomly selected Card
Card temp = deck[ first ];   
deck[ first ] = deck[ second ];
deck[ second ] = temp;   
} // end for
} // end method shuffle

// deal one Card
public Card dealCard()
{
// determine whether Cards remain to be dealt
if ( currentCard < deck.length )
return deck[ currentCard++ ]; // return current Card in array
else   
return null; // return null to indicate that all Cards were dealt
} // end method dealCard
} // end class DeckOfCards

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Card.java

import java.util.Random;

public class Card

{

private String suit, rank;

private int value;

public Card(String suit, String rank)

{

this.suit = suit;

this.rank = rank;

}

public String getRank()

{

return rank;

}

public int Value()

{

if(rank.equals("2"))

{

value=2;

}

else if(rank.equals("3"))

{

value=3;

}

else if(rank.equals("4"))

{

value=4;

}

else if(rank.equals("5"))

{

value=5;

}

else if(rank.equals("6"))

{

value=6;

}

else if(rank.equals("7"))

{

value=7;

}

else if(rank.equals("8"))

{

value=8;

}

else if(rank.equals("9"))

{

value=9;

}

else if(rank.equals("10"))

{

value=10;

}

else if(rank.equals("A"))

{

Random rand = new Random();

int count = rand.nextInt(1) +1;

if(count == 1)

{

value=11;

}

else

value= 1;

}

else if(rank.equals("Q"))

{

value=10;

}

else if(rank.equals("J"))

{

value=10;

}

else if(rank.equals("K"))

{

value=10;

}

return value;

}

public String toString()

{

return(rank + " of " + suit);

}

}

Player.java

public class Player

{

private int cValue;

private int cCount; //Card count used to count how many 'cards' added

Card[] deck= new Card[52];

private int sum;

public Player()

{

cCount=0;

}

public Card addCard(Card a)

{

deck[cCount] = a;

cCount++;

return a;

}

public int getcCount()

{

return cCount;

}

public int getValue()

{

int total=0;

for(int i=0; i < cCount; i++)

{

total += deck[i].Value();

}

return total;

}

BlackJackGame.java

public class BlackJackGame

{

public static void main(String [] args)

{

Card[] deck = new Card[52];

Player[] player = new Player[3];

int loopcount=0;

String p1result = " ", p2result = " ", p3result = " ", p4result = " ", dresult = " ";

String[] suit = {"Hearts", "Clubs", "Spades", "Diamonds"};

String[] rank = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};

for(int i=0; i<13; i++)

{

for(int x=0; x<4;x++)

{

deck[loopcount] = new Card(suit[x], rank[i]);

loopcount++;

}

}

System.out.println("Shuffling...");

for(int i=0; i< deck.length; i++) //Shuffle

{

Card tmp = deck[i];

int count= (int)(Math.random()* deck.length);

deck[i] = deck[count];

deck[count] = tmp;

}

Player player1 = new Player();

Player player2 = new Player();

Player player3 = new Player();

System.out.println("Welcome to our BlackJackGame!");

System.out.println("Welcome Dealer!");

Dealer dealer = new Dealer();

System.out.println("Let's deal the cards!");

player1.addCard(deck[0]);

player2.addCard(deck[1]);

player3.addCard(deck[2]);

System.out.println("And now the Dealer gets his card...");

dealer.addCard(deck[3]);

System.out.println("Now we get our second cards!");

System.out.println("Okay Dealer, deal out the cards!");

player1.addCard(deck[4]);

player2.addCard(deck[5]);

player3.addCard(deck[6]);

dealer.addCard(deck[7]);

int count =8;

int i=0;

do

{

p1result = "";

p2result = "";

p3result = "";

dresult = "";

int dvalue = dealer.getValue();

int p1value = player1.getValue();

int p2value = player2.getValue();

int p3value = player3.getValue();

while(p1value < 17) //hit

{

player1.addCard(deck[count]);

count++;

p1value = player1.getValue();

}

if(p1value > 21)

{

p1result = "Bust!";

}

if(p1value <21 && p1value >17)//stand

{

}

while(p2value < 17)//hit

{

player2.addCard(deck[count]);

count++;

p2value = player2.getValue();

}

if(p2value > 21) //bust

{

p2result = "Bust!";

}

if(p2value <21 && p2value >17) //stand

{

}

while(p3value < 17) //hit

{

player3.addCard(deck[count]);

count++;

p3value = player3.getValue();

}

if( p3value > 21)

{

p3result = "Bust!";

}

if(p3value <21 && p3value >21) //stand

{

}

while(dvalue < 17)

{

dealer.addCard(deck[count]);

count++;

dvalue = dealer.getValue();

}

if(dvalue > 21) //Bust

{

p1value = player1.getValue();

p2value = player2.getValue();

p3value = player3.getValue();

if(p1value == 21 || p1value <21)

{

p1result = "Win!";

}

if(p2value == 21 || p2value <21)

{

p2result = "Win!";

}

if(p3value == 21 || p3value <21 )

{

p3result = "Win!";

}

}

if(dvalue < 21 && dvalue >= 17) //For Dealer values in between

{

p1value = player1.getValue();

p2value = player2.getValue();

p3value = player3.getValue();

dvalue = dealer.getValue();

if(p1value == dvalue)

{

p1result = "Push!";

}

if(p1value > dvalue)

{

p1result = "Win!";

}

if(p1value < dvalue)

{

p1result = "Lose!";

}

if(p2value == dvalue)

{

p2result = "Push!";

}

if(p2value > dvalue)

{

p2result = "Win!";

}

if(p2value < dvalue)

{

p2result = "Lose!";

}

if(p3value == dvalue)

{

p3result = "Push!";

}

if(p3value > dvalue)

{

p3result = "Win!";

}

if(p3value < dvalue)

{

p3result = "Lose!";

}

}

if(dvalue == 21 )

{

p1value = player1.getValue();

p2value = player2.getValue();

p3value = player3.getValue();

dvalue = dealer.getValue();

if(p1value == dvalue)

{

p1result = "Push!";

}

if(p1value < dvalue || p1value > dvalue)

{

p1result = "Lose!";

}

if(p2value == dvalue)

{

p2result = "Push!";

}

if(p2value < dvalue || p2value > dvalue)

{

p2result = "Lose!";

}

if(p3value == dvalue)

{

p3result = "Push!";

}

if(p3value < dvalue || p3value > dvalue)

{

p3result = "Lose!";

}

}

System.out.println("The BlackJack Game is Complete: ");

System.out.println("Results: ");

System.out.println("Dealer: " +deck[3] + " " + deck[7] + " " +("total of " +dealer.getValue() ));

System.out.println("Player1: " +deck[0] + " " + deck[4] + " "+("total of " +player1.getValue() )+ ": " +p1result);

System.out.println("Player2: " +deck[1] + " " + deck[5] + " "+("total of " +player2.getValue() )+ ": " +p2result);

System.out.println("Player3: " +deck[2] + " " + deck[6] + " "+("total of " +player3.getValue() )+ ": " +p3result);

i++;

}

while(i <1);

}

}

Add a comment
Know the answer?
Add Answer to:
NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack...

    Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack class, which implements a BlackJack game. Please do not use any java applet on the coding. Hint: Use a test class to test above classes. Pulic class Card {    private final String face; // face of card ("Ace", "Deuce", ...)    private final String suit; // suit of card ("Hearts", "Diamonds", ...)    // two-argument constructor initializes card's face and suit    public...

  • //main.cpp #include <iostream> #include <iomanip> #include "deck-of-cards.hpp" void RunAllTests() { int count; std::cin >> count; DeckOfCards...

    //main.cpp #include <iostream> #include <iomanip> #include "deck-of-cards.hpp" void RunAllTests() { int count; std::cin >> count; DeckOfCards myDeckOfCards; for (int i = 0; myDeckOfCards.moreCards() && i < count; ++i) { std::cout << std::left << std::setw(19) << myDeckOfCards.dealCard().toString(); if (i % 4 == 3) std::cout << std::endl; } } int main() { RunAllTests(); return 0; } //card.hpp #ifndef CARD_HPP_ #define CARD_HPP_ #include <string> class Card { public: static const int totalFaces = 13; static const int totalSuits = 4; Card(int cardFace, int...

  • I've created a Card class and I'm asked to implement a class called DeckOfCards that stores...

    I've created a Card class and I'm asked to implement a class called DeckOfCards that stores 52 objects of the Card class. It says to include methods to shuffle the deck, deal a card, and report the number of cards left in the deck, and a toString to show the contents of the deck. The shuffle methods should assume a full deck. I also need to create a separate driver class that first outputs the populated deck to prove it...

  • Java BlackJack Game: Help with 1-4 using the provided code below Source code for Project3.java: import...

    Java BlackJack Game: Help with 1-4 using the provided code below Source code for Project3.java: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class Project3 extends JFrame implements ActionListener { private static int winxpos = 0, winypos = 0; // place window here private JButton exitButton, hitButton, stayButton, dealButton, newGameButton; private CardList theDeck = null; private JPanel northPanel; private MyPanel centerPanel; private static JFrame myFrame = null; private CardList playerHand = new CardList(0); private CardList dealerHand = new CardList(0);...

  • Deck of Cards Program I need help printing a flush, which is showing the top 5...

    Deck of Cards Program I need help printing a flush, which is showing the top 5 cards of the same suite. Below is the code I already have that answers other objectives, such as dealing the cards, and finding pairs. Towards the end I have attempted printing a flush, but I cannot figure it out. public class Shuffler {    /**    * The number of consecutive shuffle steps to be performed in each call    * to each sorting...

  • In the game of blackjack, the player is initially dealt two cards from a deck of...

    In the game of blackjack, the player is initially dealt two cards from a deck of ordinary playing cards. Without going into all the details of the game, it will suffice to say here that the best possible hand one could receive on the initial dear is a combination of an ace of any suit and any face card or 10. What is the probability that the player will be dealt this combination?

  • I'm currently writing a program based on stub poker and trying to deal cards to the...

    I'm currently writing a program based on stub poker and trying to deal cards to the players, show their hands, and determine the winner, and lastly ask them to if they want to play another hand. I'm probably going to have to write a method to compare hands but i really need to just deal the cards and store in their hands Any help or suggestions? Here are my current classes. public class PlayingCard {    private final Suit suit;...

  • C++ Your solution should for this assignment should consist of five (5) files: Card.h (class specification...

    C++ Your solution should for this assignment should consist of five (5) files: Card.h (class specification file) Card.cpp (class implementation file) DeckOfCards.h (class specification file) DeckOfCards.cpp (class implementation file) 200_assign6.cpp (application program) NU eelLS Seven UT Diamonds Nine of Hearts Six of Diamonds For your sixth programming assignment you will be writing a program to shuffle and deal a deck of cards. The program should consist of class Card, class DeckOfCards and an application program. Class Card should provide: a....

  • In java---- The DeckTester.java file, provides a basic set of Deck tests. Add additional code at...

    In java---- The DeckTester.java file, provides a basic set of Deck tests. Add additional code at the bottom of the main method to create a standard deck of 52 cards and test the shuffle method ONLY in the Deck class. After testing the shuffle method, use the Deck toString method to “see” the cards after every shuffle. Deck: import java.util.List; import java.util.ArrayList; /** * The Deck class represents a shuffled deck of cards. * It provides several operations including *...

  • Java Write a complete program that implements the functionality of a deck of cards. In writing...

    Java Write a complete program that implements the functionality of a deck of cards. In writing your program, use the provided DeckDriver and Card classes shown below. Write your own Deck class so that it works in conjunction with the two given classes. Use anonymous objects where appropriate. Deck class details: Use an ArrayList to store Card objects. Deck constructor: The Deck constructor should initialize your ArrayList with the 52 cards found in a standard deck. Each card is a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT