This assignment is going to create a simulation of a deck of cards and perform s
ID: 3912813 • Letter: T
Question
This assignment is going to create a simulation of a deck of cards and perform some basic card counting. There will be no other players at the table and the game will use a single deck. The deck won’t be reshuffled until it is more than two-thirds (66%) used. Each hand the program will calculate the user’s probability to bust and the probability of drawing each individual card in the game. This will be accomplished by simulating a deck of cards with a Grab-Bag data structure. The Grab-Bag is a simple data structure that can be created either with an array or a linked structure (We are going to opt for the linked structure).
The Grab-Bag has two major functions: ? add things to the bag ? remove things RANDOMLY from the bag
You are to implement a Grab-Bag data structure in Java. You will create several classes to accomplish this:
Card Class: listing suits, values **HINT: to make it simple over-ride the toString() method to give an easy way to print the card.
BagNode: that includes a Card card & BagNode next
Bag: that includes Size, BagNode head, & BagNode tail
You will maintain any and all necessary pointers to create and maintain the bag. This should be similar to a linked-list in approach. A head pointer and possibly a tail pointer are good ideas. You will also implement several functions to create the functionality of the bag:
getCurrentSize():int – item count of bag
isEmpty():bool/int – is the bag empty or not.
add() – Add the item to the bag **Hint – think about which add function you want to use from the linked list ** Hint – Remember we remove randomly … so … do we need any fancy insertion?
grab():Card – get a card out of the bag! Remember this is random **Hint – arbitrary remove **Hint – don’t forget your cases
getFrequencyOf():int – get a count of how many of an item is in the bag
empty() – empty the bag
fillWithCards() – fill the bag with a new deck of 52!
Rules:
Deal a card to the player. The player should then guess if the next card is going to be lower, higher or the same card as the current card. Deal the next card, and repeat.The player should be allowed to keep playing until they choose to quit the game. You should re-populate the deck after the deck has been 2/3rds used. Clearly the bag is simulating the deck. A card doesn’t get fully removed until the hand is done. Count Aces as High. The game should be a very simple interface. Show all previous cards that have been drawn and show the current card. Give the player the option to guess low, high, same or to quit playing. You should also show a probability breakdown for drawing each card and the probability of going lower, higher or drawing the same value.
I'm pretty stuck and am not sure where to go from here.
My Code thus far:
Card class:
package cgame;
public class Card {
private String face, suit;
private Card next, previous;
public Card()
{
face = "0";
suit = "default";
}
public Card(String f, String s)
{
face = f;
suit = s;
}
public String getface()
{
return face;
}
public String getsuit()
{
return suit;
}
public Card getprevious()
{
return previous;
}
public Card getnext()
{
return next;
}
public void setface(String a)
{
face = a;
}
public void setsuit(String a)
{
suit = a;
}
public void setnext(Card n)
{
next = n;
}
public void setprevious(Card p)
{
previous = p;
}
public void tostring()
{
System.out.println("" + face + suit);
}
}
& My attempt at the bag structure...
class Bag {
private T[] cards;
public Bag() {
this.cards = (T[]) new Object[52]; // Bag Size
}
public void set(int index, T value) {
this.cards[index] = value;
}
@Override
public String toString() {
return "Bag{cards=" + java.util.Arrays.toString(cards) + "}";
}
public static void main(String[] args) {
Bag bag = new Bag();
//Bag.set()
//Not sure
System.out.println(bag);
}
}
Explanation / Answer
import java.util.*; // Import Random
public class SimulateGame {
// Attributes
private static Player playerOne = new Player("Bob");
private static Player playerTwo = new Player("Alice");
private static Player currentPlayer = playerOne;
private static Deck deck = new Deck(true); // Shuffle deck automatically
private static ArrayList<Card> table = new ArrayList<>();
private static Card topCard;
private static int roundsPlayed = 1;
private static boolean gameOver = false;
// Main method
public static void main(String[] args) {
playGame();
}
// Methods
// Play the simple card game
public static void playGame() {
System.out.println("Starting simple card game simulation...");
System.out.println();
dealCards(); // Deal 26 cards to each player
chooseFirstPlayer(); // Choose who goes first
playRounds(); // Start the rounds
declareWinner(); // Declare a winner
}
// Deal 26 cards to each hand in alternating order
public static void dealCards() {
for (int i = 0; i < 26; i++) {
playerOne.takeCard(deck.deal());
playerTwo.takeCard(deck.deal());
}
}
// Choose who goes first
public static void chooseFirstPlayer() {
Random random = new Random();
int n = random.nextInt(2);
if (n == 1) { // Make playerTwo the new playerOne
Player temp = playerOne;
playerOne = playerTwo;
playerTwo = temp;
}
}
// Play rounds, max 10
public static void playRounds() {
while (roundsPlayed <= 10 && (gameOver == false)) {
// Display the round number
System.out.println("ROUND " + roundsPlayed);
System.out.println();
// Display each player's hand
displayHands();
// Play individual round
playRound();
// Increment roundsPlayed counter
roundsPlayed++;
}
}
// Play an individual round
public static void playRound() {
boolean suitMatch = false; // Flag for notifying a suit match
Card cardToPlay;
if ((playerOne.handSize() == 52) || (playerTwo.handSize() == 52)) {
gameOver = true;
}
while (suitMatch == false) {
// Current player places card on table
cardToPlay = currentPlayer.playCard();
table.add(cardToPlay);
// Check if there's a suit match
suitMatch = checkSuitMatch();
if (suitMatch == false)
switchCurrentPlayer();
}
collectCards();
System.out.println();
// Sleep for a second before beginning a new round
try {
Thread.sleep(500);
}
catch (InterruptedException e) {
}
}
// Switch current player
public static void switchCurrentPlayer() {
if (currentPlayer == playerOne)
currentPlayer = playerTwo;
else if (currentPlayer == playerTwo)
currentPlayer = playerOne;
}
// Check for a suit match
public static boolean checkSuitMatch() {
int tableSize = table.size();
int lastSuit;
int topSuit;
if (tableSize < 2) {
return false;
}
else {
lastSuit = table.get(tableSize - 1).getSuit();
topSuit = table.get(tableSize - 2).getSuit();
}
// Check suit equivalence
if (lastSuit == topSuit) {
System.out.println();
System.out.println(currentPlayer.getName() + " wins the round!");
System.out.println();
return true;
}
return false;
}
// Collect cards from table
public static void collectCards() {
// Print a message
System.out.print(currentPlayer.getName() + " takes the table (" +
table.size() + "): ");
displayTable();
// Player takes each card from the table and adds to hand
for (int i = 0; i < table.size(); i++) {
Card cardToTake = table.get(i);
currentPlayer.takeCard(cardToTake);
}
table.clear();
}
// Displays all the cards currently on the table
public static void displayTable() {
for (int i = 0; i < table.size(); i++) {
if (table.get(i) != null) {
System.out.print(table.get(i).getName() + " ");
}
}
System.out.println();
System.out.println();
}
// Displays each player's current hand
public static void displayHands() {
playerOne.displayHand();
playerTwo.displayHand();
}
// Declare a winner
public static void declareWinner() {
if (playerOne.handSize() > playerTwo.handSize()) {
System.out.println(playerOne.getName().toUpperCase() + " WINS " +
"WITH A TOTAL OF " + playerOne.handSize() + " CARDS!");
}
else if (playerTwo.handSize() > playerOne.handSize()) {
System.out.println(playerTwo.getName().toUpperCase() + " WINS " +
"WITH A TOTAL OF " + playerTwo.handSize() + " CARDS!");
}
else {
System.out.println("TIE! WOW IT'S SUPER RARE!");
}
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.