Write a program to implement a simple “Hi-Lo” game using a standard deck of 52 p
ID: 3725310 • Letter: W
Question
Write a program to implement a simple “Hi-Lo” game using a standard deck of 52 playing cards. The game should ask the user to enter their bankroll. Your program must verify that the user enters a positive real number. But, if the user doesn't enter a positive real number I need to continue to prompt them to enter their bankroll until they do. After a valid bankroll has been entered the program should continue to repeat until either the user indicates that they want to quit or until there is no money remaining in their bankroll. When the game ends, I need to display for the user: the total number of games played, total number of wins, total number of losses, total number of ties and the ending amount in their bankroll.
For each play of the game:
1. Ask the user to enter their bet. Verify that the user has sufficient funds remaining in their bankroll to cover the bet and that the bet is between $0.01 and $100.00.
2. Draw a card and display it for the user.
3. Ask the user if they think the next card will be higher or lower than the card which was drawn.
4. Draw a second card making sure that it is different in some way (either face, suit or both) from the first card drawn. If the card is exactly the same (e.g., both are the Ace of Clubs), continue to draw the second card until a different card is drawn.
5. Display the result for the user.
a. If the user guessed correctly add the amount of the bet to their bankroll.
b. If the user guessed incorrectly, subtract the amount of the bet from their bankroll.
c. If the second card drawn is a card of the same face value but a different suit, the game is a tie and no change should be made to the user’s bankroll.
6. Display the amount in the user’s bankroll after each play of the game before asking whether the user wants to play again.
I do have a template I need to use but are you able to help me without it? I don't need anything complex as this should be more on the basic side of coding due to the course I'm taking and what we've learned so far.
Explanation / Answer
package cheggindia;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Phoenix
*/
class Card {
String value;//Value of the card which can go from 2 to A
String suit;//The Suit of the card which can be C-Clubs, D- Diamond, S-Spade, H-Hearts
public Card(String value, String suit) {
this.value = value;
this.suit = suit;
}
}
public class HiLoGame {
HashMap<String,Integer> cardValues=new HashMap();
Card deck[] = new Card[52];//Make a deck of 52 cards
String suits[] = {"Clubs", "Diamonds", "Spades", "Hearts"};//Suits of cards
String bigCards[] = {"Ace", "Jack", "King", "Queen"};//Cards which have high value
int count = 0;
float bankRoll=0.0f;//Initial bank roll of user
Card chosen;//chosen intially for the user
Card secondCard;//Second card chosen for the user
public HiLoGame(){
makeDeck();
}
public void makeDeck() {//create the deck with cards
for (int i = 0; i < 4; i++) {
for (int j = 2; j < 11; j++) {
deck[count] = new Card((""+j), suits[i]);
count++;
}
for (int j = 0; j < 4; j++) {
deck[count] = new Card(bigCards[j], suits[i]);
count++;
}
}
//Associate a value with each card(Ranking of the card which can be used for comparison)
cardValues.put("Ace", 13);
cardValues.put("King", 12);
cardValues.put("Queen", 11);
cardValues.put("Jack", 10);
cardValues.put("10", 9);
cardValues.put("9", 8);
cardValues.put("8", 7);
cardValues.put("7", 6);
cardValues.put("6", 5);
cardValues.put("5", 4);
cardValues.put("4", 3);
cardValues.put("3", 2);
cardValues.put("2", 1);
}
public Card pickCard(){//Pick a Random card from the deck of 52 cards
return deck[new Random().nextInt(52)];
}
public int checkWin(boolean high){
if((cardValues.get((""+secondCard.value))>cardValues.get((""+chosen.value)))&&high)///user chose high and it goes high
return 1;
else if((cardValues.get((""+secondCard.value))<cardValues.get((""+chosen.value)))&&(!high))//user chose low and it goes low
return 1;
else if((cardValues.get((""+secondCard.value))>cardValues.get((""+chosen.value)))&&(!high))///user chose low but it goes high
return 0;
else if((cardValues.get((""+secondCard.value))<cardValues.get((""+chosen.value)))&&high)//user chose high but it goes low
return 0;
else if((cardValues.get((""+secondCard.value)).intValue()==cardValues.get((""+chosen.value)).intValue())&&
!(secondCard.suit.equals(chosen.suit)))
return -1;
return -2;
}
public static void main(String args[]) {
HiLoGame hlg=new HiLoGame();
int played=0,won=0,lost=0,ch=1,tie=0;
boolean high;
float bet;
Scanner sc=new Scanner(System.in);
do{
System.out.print("Enter Bank Roll(must be more than 0) ");
hlg.bankRoll=sc.nextFloat();
}while(!(hlg.bankRoll>0));
do{
played+=1;
do{
System.out.print("Enter Bet ");
bet=sc.nextFloat();
}while(bet>hlg.bankRoll ||(bet<0.01 ||bet>100));
hlg.chosen=hlg.pickCard();
System.out.println("The Chosen Card is "+hlg.chosen.value+" Of "+hlg.chosen.suit);
System.out.print("Do you want to go High(hi) or Low(lo)? ");
String choice=sc.next();
if(choice.equals("hi"))
high=true;
else
high=false;
do{
hlg.secondCard=hlg.pickCard();
if(hlg.secondCard!=hlg.chosen)//Make sure that the second card is not same as the first
break;
}while(true);
System.out.println("The Drawn Card is "+hlg.secondCard.value+" Of "+hlg.secondCard.suit);
switch (hlg.checkWin(high)) {
case 1:
System.out.println("Game Won");
won++;
hlg.bankRoll+=bet;
break;
case 0:
System.out.println("Game Lost");
lost++;
hlg.bankRoll-=bet;
break;
case -1:
System.out.println("Game Tied");
tie++;
break;
default:
break;
}
System.out.println("Available Bank Roll $"+hlg.bankRoll);
System.out.println("Number of Games Played"+played);
System.out.println("Number of Games Lost"+lost);
System.out.println("Number of Games Won"+won);
System.out.println("Number of Games Tied"+tie);
System.out.println("1. Play Again");
System.out.println("0. Quit");
System.out.print("Your Choice ");
ch=sc.nextInt();
}while(ch!=0);
}
}
Sample output:-
Enter Bank Roll(must be more than 0) 100
Enter Bet 10
The Chosen Card is 4 Of Hearts
Do you want to go High(hi) or Low(lo)? hi
The Drawn Card is 5 Of Hearts
Game Won
Available Bank Roll $110.0
Number of Games Played1
Number of Games Lost0
Number of Games Won1
Number of Games Tied0
1. Play Again
0. Quit
Your Choice 1
Enter Bet 20
The Chosen Card is Ace Of Hearts
Do you want to go High(hi) or Low(lo)? lo
The Drawn Card is King Of Clubs
Game Won
Available Bank Roll $130.0
Number of Games Played2
Number of Games Lost0
Number of Games Won2
Number of Games Tied0
1. Play Again
0. Quit
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.