Question

In: Computer Science

This game is simpler than the normal game of war, but the object is to have...

This game is simpler than the normal game of war, but the object is to have a card with a higher value than the dealer. The user makes a bet. Then, both the user and the computer draw a card. If the user’s card has a larger value than the computer’s card, then the user wins, and gets the value of their bet added to their total. Otherwise, the computer wins, and the user loses their bet. After each round, discard the cards. Keep playing until the user runs out of money, or until they say they don’t want to play anymore.

Card class - the class representing a single card in a deck. Every card has three properties. The first is a “suit,” which is either “Hearts”, “Diamonds,” “Spades”, or “Clubs”. A card also has a color, where Hearts and Diamonds are red, and Spades and Clubs are black. Finally, each card has a value from 1-13 (inclusive). Some values have special names: the 1 is an Ace, 11 is a Jack, 12 is a Queen, and 13 is a King.

Your first job is to design a class for a card, with each of these properties. It should have these methods:

  1. public Card(int value, String suit) - initializes the Card and it’s variables. For example, Card(12, “Hearts”) will create a red 12 of hearts.

  2. public int getValue( ) - returns value of the card

  3. public String getColor( ) - returns color of the card

  4. public String getSuit( ) - returns suit of the card

  5. public String toString() - returns string representation of the card, E.G. “King of Hearts.”

    Or “7 of clubs”

DECK CLASS

In an entire deck, there are 52 cards, each of the 13 values for the four different suits. You should implement a class to handle all of the logistics of the deck, so that as a user of the class, all you have to do is pick cards from the deck, and then discard them once you’re done with them. This class has a bit more logic in it, so here’s what you have to do.

  1. public Deck()

    This method will initialize the deck - creating an array of each of the 52 possible cards. After generating them, you should also shuffle them in the array (shuffling method is defined below). This method should also initialize a discard pile array, which currently has nothing in it. You may want to initialize other variables too, to assist with your other methods.

  2. public void shuffle()

This method shuffles all of the items in the deck. You can be creative with your shuffling algorithm, but I suggest you implement this one (the first algorithm under “The Modern Algorithm”). This is in “pseudo code,” which means it’s not in a real programming language, so it will be your job to convert this into real java code. Make sure if there are some “null” elements in your deck, that you don’t shuffle them into the deck.

  1. public Card drawNextCard()

    This method will give you the next card in the deck. Initially, this will be the card at index 0, then index 1, then index 2... up until index 51. After index 51, drawNextCard should take all of the cards in the discard pile, put them in the deck array, empty the discard pile, shuffle them, and then return the first one.

  2. public void discard(Card c)

    This method will add the card into the discard pile. Initially it will add the card to the first index of the discard pile, then the second index, etc...

Hints:

  1. Remember that assignments for Arrays are by reference. If you want the deck to be a copy

    of everything in the discard pile, you can just do:
    currentDeck = discardPile.clone();
    Then to empty the discard pile, you can just say: discardPile = new Card[52];

  2. Be careful with shuffling that you don’t accidentally shuffle “null” objects into the deck. This won’t happen in the beginning when the deck is full, but when you’re making the contents of the discard pile into the new deck, there will probably be some “null” elements at the end.

Feel free to add any other methods to these classes.

CLIENT CODE

Once you have these classes done, and have tested them to make sure they work, you can start using these objects to write the game in our casino, in a new file Casino.java.

you should have a main method that allows the users to play these games. There are many choices that are left up to you regarding the design of the game. For example, what do you do when the user runs out of money. How do you decide how much the user starts with? Will you implement a betting minimum and maximum?

Solutions

Expert Solution

Program :

#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>

#define spade 06
#define club 05
#define diamond 04
#define heart 03
#define RESULTS "Blackjack.txt"

//Global Variables
int k;
int l;
int d;
int won;
int loss;
int cash = 500;
int bet;
int random_card;
int player_total=0;
int dealer_total;

int clubcard();
int diamondcard();
int heartcard();
int spadecard();
int randcard();
int betting();
void asktitle();
void rules();
void play();
void dealer();
void stay();
void cash_test();
void askover();
void fileresults();

//Main Function
int main(void)
{
int choice1;

asktitle();

printf("\n");
printf("\n");
system("pause");
return(0);
} //end program

void asktitle() // Function for asking player if they want to continue
{
char choice1;
int choice2;

printf("\n Are You Ready?");
printf("\n (Y/N)\n ");
scanf("\n%c",&choice1);

while((choice1!='Y') && (choice1!='y') && (choice1!='N') && (choice1!='n'))
{
printf("\n");
printf("Incorrect Choice. Please Enter Y for Yes or N for No.\n");
scanf("%c",&choice1);
}


if((choice1 == 'Y') || (choice1 == 'y')) // If yes, continue. Prints menu.
{
system("cls");
printf("\nEnter 1 to Begin the game.");
  
printf("\nEnter 2 to Exit Game. ");
printf("\nChoice: ");
scanf("%d", &choice2); // Prompts user for choice
if((choice2<1) || (choice2>3)) // If invalid choice entered
{
printf("\nIncorrect Choice. Please enter 1, 2 or 3\n");
scanf("%d", &choice2);
}
switch(choice2)
{
case 1: // Case to begin game
system("cls");
  
play();

break;
  
  
  
case 2: // Case to exit game


system("pause");
exit(0);
break;
  
default:
printf("\nInvalid Input");
}
}
  


else if((choice1 == 'N') || (choice1 == 'n')) // If no, exit program
{

system("pause");
exit(0);
}

return;
} // End function


int clubcard() //Displays Club Card Image
{


srand((unsigned) time(NULL));
k=rand()%13+1;

if(k<=9) //If random number is 9 or less, print card with that number
{
//Club Card
printf("-------\n");
printf("|%c |\n", club);
printf("| %d |\n", k);
printf("| %c|\n", club);
printf("-------\n");
}


if(k==10) //If random number is 10, print card with J (Jack) on face
{
//Club Card
printf("-------\n");
printf("|%c |\n", club);
printf("| J |\n");
printf("| %c|\n", club);
printf("-------\n");
}


if(k==11) //If random number is 11, print card with A (Ace) on face
{
//Club Card
printf("-------\n");
printf("|%c |\n", club);
printf("| A |\n");
printf("| %c|\n", club);
printf("-------\n");
if(player_total<=10) //If random number is Ace, change value to 11 or 1 depending on dealer total
{
k=11;
}
  
else
{

k=1;
}
}


if(k==12) //If random number is 12, print card with Q (Queen) on face
{
//Club Card
printf("-------\n");
printf("|%c |\n", club);
printf("| Q |\n");
printf("| %c|\n", club);
printf("-------\n");
k=10; //Set card value to 10
}


if(k==13) //If random number is 13, print card with K (King) on face
{
//Club Card
printf("-------\n");
printf("|%c |\n", club);
printf("| K |\n");
printf("| %c|\n", club);
printf("-------\n");
k=10; //Set card value to 10
}
return k;
}// End function

int diamondcard() //Displays Diamond Card Image
{


srand((unsigned) time(NULL)); //Generates random seed for rand() function
k=rand()%13+1;

if(k<=9) //If random number is 9 or less, print card with that number
{
//Diamond Card
printf("-------\n");
printf("|%c |\n", diamond);
printf("| %d |\n", k);
printf("| %c|\n", diamond);
printf("-------\n");
}

if(k==10) //If random number is 10, print card with J (Jack) on face
{
//Diamond Card
printf("-------\n");
printf("|%c |\n", diamond);
printf("| J |\n");
printf("| %c|\n", diamond);
printf("-------\n");
}

if(k==11) //If random number is 11, print card with A (Ace) on face
{
//Diamond Card
printf("-------\n");
printf("|%c |\n", diamond);
printf("| A |\n");
printf("| %c|\n", diamond);
printf("-------\n");
if(player_total<=10) //If random number is Ace, change value to 11 or 1 depending on dealer total
{
k=11;
}
  
else
{
k=1;
}
}

if(k==12) //If random number is 12, print card with Q (Queen) on face
{
//Diamond Card
printf("-------\n");
printf("|%c |\n", diamond);
printf("| Q |\n");
printf("| %c|\n", diamond);
printf("-------\n");
k=10; //Set card value to 10
}

if(k==13) //If random number is 13, print card with K (King) on face
{
//Diamond Card
printf("-------\n");
printf("|%c |\n", diamond);
printf("| K |\n");
printf("| %c|\n", diamond);
printf("-------\n");
k=10; //Set card value to 10
}
return k;
}// End function

int heartcard() //Displays Heart Card Image
{


srand((unsigned) time(NULL));
k=rand()%13+1;

if(k<=9)
{
//Heart Card
printf("-------\n");
printf("|%c |\n", heart);
printf("| %d |\n", k);
printf("| %c|\n", heart);
printf("-------\n");
}

if(k==10) //If random number is 10, print card with J (Jack) on face
{
//Heart Card
printf("-------\n");
printf("|%c |\n", heart);
printf("| J |\n");
printf("| %c|\n", heart);
printf("-------\n");
}

if(k==11) //If random number is 11, print card with A (Ace) on face
{
//Heart Card
printf("-------\n");
printf("|%c |\n", heart);
printf("| A |\n");
printf("| %c|\n", heart);
printf("-------\n");
if(player_total<=10) //If random number is Ace, change value to 11 or 1 depending on dealer total
{
k=11;
}
  
else
{
k=1;
}
}

if(k==12) //If random number is 12, print card with Q (Queen) on face
{
//Heart Card
printf("-------\n");
printf("|%c |\n", heart);
printf("| Q |\n");
printf("| %c|\n", heart);
printf("-------\n");
k=10; //Set card value to 10
}

if(k==13) //If random number is 13, print card with K (King) on face
{
//Heart Card
printf("-------\n");
printf("|%c |\n", heart);
printf("| K |\n");
printf("| %c|\n", heart);
printf("-------\n");
k=10; //Set card value to 10
}
return k;
} // End Function

int spadecard() //Displays Spade Card Image
{


srand((unsigned) time(NULL)); //Generates random seed for rand() function
k=rand()%13+1;

if(k<=9) //If random number is 9 or less, print card with that number
{
//Spade Card
printf("-------\n");
printf("|%c |\n", spade);
printf("| %d |\n", k);
printf("| %c|\n", spade);
printf("-------\n");
}

if(k==10) //If random number is 10, print card with J (Jack) on face
{
//Spade Card
printf("-------\n");
printf("|%c |\n", spade);
printf("| J |\n");
printf("| %c|\n", spade);
printf("-------\n");
}

if(k==11) //If random number is 11, print card with A (Ace) on face
{
//Spade Card
printf("-------\n");
printf("|%c |\n", spade);
printf("| A |\n");
printf("| %c|\n", spade);
printf("-------\n");
if(player_total<=10) //If random number is Ace, change value to 11 or 1 depending on dealer total
{
k=11;
}
  
else
{
k=1;
}
}

if(k==12)
{
//Spade Card
printf("-------\n");
printf("|%c |\n", spade);
printf("| Q |\n");
printf("| %c|\n", spade);
printf("-------\n");
k=10; //Set card value to 10
}

if(k==13) //If random number is 13, print card with K (King) on face
{
//Spade Card
printf("-------\n");
printf("|%c |\n", spade);
printf("| K |\n");
printf("| %c|\n", spade);
printf("-------\n");
k=10; //Set card value to 10
}
return k;
} // End Function

int randcard() //Generates random card
{
  
  
srand((unsigned) time(NULL)); //Generates random seed for rand() function
random_card = rand()%4+1;
  
if(random_card==1)
{
clubcard();
l=k;
}
  
if(random_card==2)
{
diamondcard();
l=k;
}
  
if(random_card==3)
{
heartcard();
l=k;
}
  
if(random_card==4)
{
spadecard();
l=k;
}
return l;
} // End Function

void play() //Plays game
{
  
int p=0; // holds value of player_total
int i=1; // counter for asking user to hold or stay (aka game turns)
char choice3;
  
cash = cash;
cash_test();
printf("\nCash: $%d\n",cash); //Prints amount of cash user has
randcard(); //Generates random card
player_total = p + l; //Computes player total
p = player_total;
printf("\nYour Total is %d\n", p); //Prints player total
dealer(); //Computes and prints dealer total
betting(); //Prompts user to enter bet amount
  
while(i<=21) //While loop used to keep asking user to hit or stay at most twenty-one times
// because there is a chance user can generate twenty-one consecutive 1's
{
if(p==21) //If user total is 21, win
{
printf("\nUnbelievable! You Win!\n");
won = won+1;
cash = cash+bet;
printf("\nYou have %d Wins and %d Losses. Awesome!\n", won, loss);
dealer_total=0;
askover();
}
  
if(p>21) //If player total is over 21, loss
{
printf("\nWoah Buddy, You Went WAY over.\n");
loss = loss+1;
cash = cash - bet;
printf("\nYou have %d Wins and %d Losses. Awesome!\n", won, loss);
dealer_total=0;
askover();
}
  
if(p<=21) //If player total is less than 21, ask to hit or stay
{
printf("\n\nWould You Like to Hit or Stay?");
  
scanf("%c", &choice3);
while((choice3!='H') && (choice3!='h') && (choice3!='S') && (choice3!='s')) // If invalid choice entered
{
printf("\n");
printf("Please Enter H to Hit or S to Stay.\n");
scanf("%c",&choice3);
}


if((choice3=='H') || (choice3=='h')) // If Hit, continues
{
randcard();
player_total = p + l;
p = player_total;
printf("\nYour Total is %d\n", p);
dealer();
if(dealer_total==21) //Is dealer total is 21, loss
{
printf("\nDealer Has the Better Hand. You Lose.\n");
loss = loss+1;
cash = cash - bet;
printf("\nYou have %d Wins and %d Losses. Awesome!\n", won, loss);
dealer_total=0;
askover();
}
  
if(dealer_total>21) //If dealer total is over 21, win
{
printf("\nDealer Has Went Over!. You Win!\n");
won = won+1;
cash = cash+bet;
printf("\nYou have %d Wins and %d Losses. Awesome!\n", won, loss);
dealer_total=0;
askover();
}
}
if((choice3=='S') || (choice3=='s')) // If Stay, does not continue
{
printf("\nYou Have Chosen to Stay at %d.!\n", player_total);
stay();
}
}
i++; //While player total and dealer total are less than 21, re-do while loop
} // End While Loop
} // End Function

void dealer() //Function to play for dealer AI
{
int z;
  
if(dealer_total<17)
{
srand((unsigned) time(NULL) + 1);
z=rand()%13+1;
if(z<=10)
{
d=z;
  
}
  
if(z>11)
{
d=10;
}
  
if(z==11) //If random number is 11(Ace), change value to 11 or 1 depending on dealer total
{
if(dealer_total<=10)
{
d=11;
}
  
else
{
d=1;
}
}
dealer_total = dealer_total + d;
}

printf("\nThe Dealer Has a Total of %d", dealer_total); //Prints dealer total
  
} // End Function

void stay()
{
dealer();
if(dealer_total>=17)
{
if(player_total>=dealer_total)
{
printf("\nUnbelievable! You Win!\n");
won = won+1;
cash = cash+bet;
printf("\nYou have %d Wins and %d Losses. Awesome!\n", won, loss);
dealer_total=0;
askover();
}
if(player_total<dealer_total) //If player's total is less than dealer's total, loss
{
printf("\nDealer Has the Better Hand. You Lose.\n");
loss = loss+1;
cash = cash - bet;
printf("\nYou have %d Wins and %d Losses. Awesome!\n", won, loss);
dealer_total=0;
askover();
}
if(dealer_total>21) //If dealer's total is more than 21, win
{
printf("\nUnbelievable! You Win!\n");
won = won+1;
cash = cash+bet;
printf("\nYou have %d Wins and %d Losses. Awesome!\n", won, loss);
dealer_total=0;
askover();
}
}
else
{
stay();
}
  
} // End Function

void cash_test()
{
if (cash <= 0)
{
printf("You Are Bankrupt. Game Over");
cash = 500;
askover();
}
} // End Function

int betting() //Asks user amount to bet
{
printf("\n\nEnter Bet: $");
scanf("%d", &bet);

if (bet > cash) //If player tries to bet more money than player has
{
printf("\nYou cannot bet more money than you have.");
printf("\nEnter Bet: ");
scanf("%d", &bet);
return bet;
}
else return bet;
} // End Function

void askover()
char choice1;
{
printf("\nWould You Like To Play Again?");
printf("\nPlease Enter Y for Yes or N for No\n");
scanf("\n%c",&choice1);

while((choice1!='Y') && (choice1!='y') && (choice1!='N') && (choice1!='n')) // If invalid choice entered
{
printf("\n");
printf("Incorrect Choice. Please Enter Y for Yes or N for No.\n");
scanf("%c",&choice1);
}


if((choice1 == 'Y') || (choice1 == 'y')) // If yes, continue.
{
system("cls");
play();
}
  
else if((choice1 == 'N') || (choice1 == 'n')) // If no, exit program
{
fileresults();
printf("\nBYE!!!!\n\n");
system("pause");
exit(0);
}
return;
} // End function

void fileresults() y
{
FILE *fpresults;
fpresults = fopen(RESULTS, "w");
if(fpresults == NULL)
printf("\nError: File Missing\n");
system("pause");
exit(1);
}
else
{
fprintf(fpresults,"\n\t RESULTS");
fprintf(fpresults,"\n\t---------\n");
fprintf(fpresults,"\nYou Have Won %d Times\n", won);
fprintf(fpresults,"\nYou Have Lost %d Times\n", loss);
fprintf(fpresults,"\nKeep Playing !");
}
fclose(fpresults);
return;
} // End Function

Output:

Are You Ready?
(Y/N)
Y   
Enter 1 to Begin the game.
Enter 2 to Exit Game.
Choice: 1
Cash: $500
-------
| |
| Q |
| |
-------

Your Total is 10

The Dealer Has a Total of 10

Enter Bet: $40

Would You Like to Hit or Stay?
Please Enter H to Hit or S to Stay.
S
You Have Chosen to Stay at 10.
The Dealer Has a Total of 20
Dealer Has the Better Hand. You Lose.

You have 0 Wins and 1 Losses. Awesome!

Would You Like To Play Again?
Please Enter Y for Yes or N for No
N

I hope this answer is helpful to you, please upvote my answer.thank you....


Related Solutions

the waveform of a flute is much simpler than that of an clarinet,why?
the waveform of a flute is much simpler than that of an clarinet,why?
Is it possible for an object to have more than one type of charge at the...
Is it possible for an object to have more than one type of charge at the same time?
In your opinion, why do you think housekeeping genes have simpler promoter sequences than tissue-specific and...
In your opinion, why do you think housekeeping genes have simpler promoter sequences than tissue-specific and dynamically expressed genes?
An object will sink in a liquid if the density of the object is greater than...
An object will sink in a liquid if the density of the object is greater than that of the liquid. The mass of a sphere is 0.723 g. If the volume of this sphere is less than ________ cm3, then the sphere will sink in liquid mercury (density = 13.6 g/cm3). A) 0.0532 B) 18.6 C) 9.31 D) 7.48 E) 0.345
What are the reasons to having lower than normal or higher than normal hematocrit
What are the reasons to having lower than normal or higher than normal hematocrit
3 models of aggression (Collectively called Game Theory Models): 1) The Hawk-Dove Game, 2) The War...
3 models of aggression (Collectively called Game Theory Models): 1) The Hawk-Dove Game, 2) The War of Attrition Model and 3) The Sequential Assessment Model The Hawk-Dove Game Developed by Maynard Smith and Prince •In simplest form, heuristic model •Imagine that individuals can adopt one of two behavioral strategies 1.Hawk – player escalates and continues to escalate until either it is injured or its opponent cedes the resource 2.Dove – in which a player displays as it if will escalate...
In 6A, you created an object class encapsulating a Trivia Game which INHERITS from Game. Now...
In 6A, you created an object class encapsulating a Trivia Game which INHERITS from Game. Now that you have successfully created Trivia objects, you will continue 6B by creating a linked list of trivia objects. Add the linked list code to the Trivia class. Your linked list code should include the following: a TriviaNode class with the attributes: 1. trivia game - Trivia object 2. next- TriviaNode 3. write the constructor, accessor, mutator and toString methods. A TriviaLinkedList Class which...
Create a java program. War is a card game for two players. A standard deck of...
Create a java program. War is a card game for two players. A standard deck of 52 cards is dealt so that both players have 26 cards. During each round of play (or "battle"), both players play a card from the top of their hand face up. The player who plays the card of the higher rank wins both cards and places them at the bottom of his stack of cards. If both cards played are of the same rank,...
Can I have this reworded in simpler form of writting, because I do not understand it...
Can I have this reworded in simpler form of writting, because I do not understand it There are various repayments for using private financing. Such as, permit you to choose your own stakeholders. This raises the chances of having depositors with like reasons as you and funds they will probably be capable to convey commercial information and aid, also including beneficial aid from the government. Also, permit you to continue a private business, provide suppleness in the quantity and type...
The Civil War was a ruinous war for both North and South. Could it have been...
The Civil War was a ruinous war for both North and South. Could it have been avoided and if so how? If you think it could not have been avoided, why not?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT