I honestly have no clue how to create this game: The game should start with a us
ID: 3670763 • Letter: I
Question
I honestly have no clue how to create this game:
The game should start with a user entering their name. We will have a simple "game save" mechanism of writing the information of a user to a file. If this is name of a user who already exists, then we should load their information from a file. If the name does not exist, a new file should be created with that user's information. At the beginning of each hand, you should ask if the player wants to continue playing, and if so, how much they would like to bet on this hand. If the player does not want to play, the player's information should be saved to a file.
The game of Blackjack is relatively simple. Players are dealt two cards. The goal is to get closest to 21 points without going over. Having 22 points or more means that you lose. If the dealer has more points than you, you lose. If you have more points than the dealer, you win. If either you or the dealer goes over 21 points, that player loses. If you have exactly 21 points with only two cards, you have a "blackjack", and win back your own money plus 1.5 times your bet.
At the beginning of the hand, the player can decide how much they would like to bet. This can be anywhere from $0.01 to the amount of money they have on hand. If they try to bet a negative amount or more than they have on hand, the system should not allow them to do so, and ask for a valid amount.
Points are determined in the following way:
Non-face cards (2, 3, 4, 5, 6, 7, 8, 9, or 10 of any suit) are worth as many points as the number on the card. For example, a 2 of Clubs is worth 2 points, and a 8 of Diamonds is worth 8 points. The suit does not make any difference when it comes to calculating the point total.
Face cards (Jack, Queen, or King of any suit) are worth 10 points.
An Ace can be worth either 1 points or 11 points, whichever is better for the current hand.
The player can do one of two things (actually, in a real game of Blackjack, there are several other options, but we are playing a simplified version of the game). The first thing they can do is "stay" - that is, accept their current hand. The second thing they can do is "hit" - accept another card. If, at any point, the player's score goes over 21, the player automatically loses the round.
Once the player stays, the "house" (that is, the dealer) will turn over their cards. The house player will follow a simple set of rules:
If the score is greater than 21, the house loses
Otherwise, if the score is greater than or equal to 18 but less than or equal to 21, the house will stay
If the score is exactly 17, and at least one card is an ace (a "soft" 17), the house will hit. If no cards are aces (a "hard" 17), the house will stay.
Otherwise, the house will hit
This can repeat for several iterations (e.g., the house has a 2 of Clubs and a 5 of Clubs (score = 7), hits and gets a 6 of Clubs (score = 13), hits and get a 2 of Diamonds (score = 15), and then hits and gets a 5 of Diamonds (score = 20). Once the house stays, the hand is over. The following possibilities should be coded for:
The player has a Blackjack (exactly 21 points from only two cards, e.g., an Ace and a King). The player receives all of their money back, plus 1.5 times their bet. For example, if the player bet 5 dollars, they would receive back $12.50 (original $5.00 + won $7.50).
The house has a higher score than the player. In this case, the player loses. All of the money bet by the player this hand is lost.
The player has a higher score than the house. In this case, the player wins, and receives all of their money back, plus wins that exact same amount. For example, if the player bet 5 dollars, they would receive back 10 dollars (original $5.00 + won $5.00).
The house and the player have equal hands. This is called a "push", and the player does not lose or gain any money.
At each round of betting, the player has the option to (H)it or (S)tay. If the player enters "H", "h", "Hit", or "hit", the player will hit. If the player enters "S", "s", "Stay", or "stay", the player will stay. The player always has the option to do either (even if they have 21 points!). If the player goes over 21 points, however, they automatically lose that hand. The dealer (house) should not do anything after that.
That comprises one hand of Blackjack.
At the beginning of each hand, the system should display the name of the user, the number of hands played, the number of blackjacks the user has has had, and the amount of money they have remaining, as in the following format:
All values shall be right-justified.
At the end of each hand, the amount of money won or lost, along with the result, should be displayed - one of the following:
Push
Dealer busted (went over 21 points)
Player busted (went over 21 points)
Player won
Dealer won
Blackjack
Cards should be indicated by Value/Suit, according to the following chart:
Card values should be in uppercase (or numeric) and located in the first position
Numbers 2 through 9 should be printed as is
A 10 should be printed as T, a J for a Jack, a Q for a Queen, a K for a King, and an A for an Ace
Suits should be in lowercase and located in the second position - s for Spades, d for Diamonds, c for Clubs, h for Hearts
Examples:
As - Ace of Spaces
5d - Five of Diamonds
Tc - Ten of Clubs
Please see the sample_output.txt file to see exactly what the output should look like!
What makes the game "infinite" is that we assume that any card is possible to come up, instead of shuffling a deck. So every time a card is generated, it can be of any possible suit and any possible card value. All card values and suits should always be equally likely to come up (no card counters can work in our Infinite Casino)
Remember that quitting the program saves the current user's information. The file should be a simple textfile of the format:
The file will be named NAME.txt, where NAME is equal to the username. Assume that all names will be only one name with no spaces (so don't worry about saving "Joe Schmoe.txt"). Money will be a floating-point value, and the number of hands played and won will be integers.
If the file NAME.txt does not exist for a given name, then you can assume that the player's file does not exist and that it is a new player. You can use the File.exists() method for this functionality.
Unlike the first assignment, input may not be of the appropriate type! That is, the grader may enter "poodle" or "kwyjibo" for a floating-point value, or 7.3333 when you are expecting an integer. For all inputs, any data of the wrong type can be considered an invalid value.
You will need to use methods and classes appropriately, as well as comment appropriately, for full credit.
Implementation Details:
You must use at least the following classes:
Player, which contains all player information
Card, which contains all of the information about a particular card
Blackjack, which contains the main method for the program
The Player class should contain information related to the player, including the current amount of money, number of hands played, and number of hands won. It should include accessors and mutators for these three attributes. It should also contain methods to load information from a file and to save the information to a file.
The Card class should contain information about the card value (2, 8, King, etc.) and suit (clubs, diamonds, etc.). Upon instantation of a Card, both of these should be randomly generated using a Random object. This Random object should be instantiated once - at the class level - and not each time that a Card object is instantiated (that is, do not assign the Random object in the constructor! There should be accessors for both of these, as well as a .toString() method which will return a string following the display for cards mentioned above (e.g. Tc for Ten of Clubs).
The Blackjack class should be the main class of the program. By running java Blackjack, I should be able to start the program up. No other names for this main class are acceptable!
Explanation / Answer
BlackJack.java
import java.io.*;
import java.util.Scanner;
public class Blackjack{
public static void main(String args[]) throws IOException{
//main where the objects are called and comparisons are made to see who wins
Scanner sc = new Scanner(System.in);//declare scanner
Player player = new Player(); //player object
Cards dealerCards = new Cards();
Cards playerCards = new Cards(); //Cards object
boolean done = false;
String playAgain = "y"; //sets if to keep playing or not
String hitOrStay = null;
System.out.println("Welcome to Infinite Blackjack!");
player.getInfo(); //send name to getName
player.showInfo();//this prints out the players info
System.out.println("Would you like to play a hand? (Y/N)");
playAgain = sc.next();
while (!playAgain.equalsIgnoreCase("y") && !playAgain.equalsIgnoreCase("n")){ //checks input
System.out.println("Play a hand? (Y/N) >> ");
playAgain = sc.next();
}
while (playAgain.equalsIgnoreCase("y")){
player.checkBet();
System.out.println("PLAYER DEAL");
playerCards.firstHand(); //calls the users first 2 card hand
if (playerCards.value == 21){
done = true;
player.blackJack(); //stops everything because of blackjack
}
if (done == false){
//keeps going if no blackjack and if under 21
System.out.println("[H]it or [S]tay?");
hitOrStay = sc.next();
}
while ((!hitOrStay.equalsIgnoreCase("s") && !hitOrStay.equalsIgnoreCase("stay")) && done == false){
if ((hitOrStay.equalsIgnoreCase("hit") || hitOrStay.equalsIgnoreCase("h")) && done == false){
System.out.println("Hit!");
playerCards.newCard(); //calls 1 new card for the user and updates total
if (playerCards.total > 21){ //checks for the user busting
System.out.println("Bust, you lose!");
done = true;
player.lostHand(); //updates player info for losing
hitOrStay = "s";
}
if (done != true){
System.out.println("[H]it or [S]tay?");
hitOrStay = sc.next();
}
}//hit if
}
if ((hitOrStay.equalsIgnoreCase("s") || hitOrStay.equalsIgnoreCase("stay")) && done == false ){
//this starts after user busts or stays
System.out.println("Stay!");
System.out.println("DEALER DEAL");
dealerCards.firstHand(); //dealers first 2 card hand
if (dealerCards.value == 21){
System.out.println("DEALER has blackjack, you lose!");
player.lostHand();
done = true;
}
while (dealerCards.value < 18 || dealerCards.total < 18){
dealerCards.newCard();
//the dealer hits on 17
}
if (dealerCards.total > 21){ //checks for dealer bust, updates players money and hands if true
System.out.println("Dealer Busts!");
player.wonHand();
done = true;
}
}
if (done != true){ //this is used if neither player or dealer has blackjack and has not busted
if (playerCards.total > dealerCards.total){
System.out.println("You Win!");
player.wonHand();
}
if (playerCards.total < dealerCards.total){
System.out.println("You lose!");
player.lostHand();
}
if (playerCards.total == dealerCards.total){
System.out.println("Push!");
}
}
player.showInfo(); //prints new player info after update from last hand
System.out.println("Would you like to play a hand? [Y/N]"); //determines if loop runs again
playAgain = sc.next();
}
sc.close();
}
}
Cards.java
import java.util.Random;
public class Cards{
public int total;
public int value;
public String cards[] = {"A","2","3","4","5","6","7","8","9","T","J","Q","K"};
public String suits[] = {"s","c","d","h"};
public int value1, value2, newValue;
Random rng = new Random();
public int finalValue;
public String finalSuit;
public Cards(){ //Cards constructor
//I do realized that this could contain the card, but i didnt do that
//instead I made a first hand method that picks 2 hands
}
public void firstHand(){
//this is the first hand, picks 2 cards and calculates the values, then prints them back to main
int cardChooser1 = rng.nextInt(cards.length);
int cardChooser2 = rng.nextInt(cards.length);
String card1 = cards[cardChooser1];
String suit1 = suits[rng.nextInt(suits.length)];
String card2 = cards[cardChooser2];
String suit2 = suits[rng.nextInt(suits.length)];
switch (cardChooser1){ //this determines the value of the cards based on the position in the array
case 0:
value1 = 11;
if (value > 21){
value1 = 1;
}
break;
case 1:
value1 = 2;
break;
case 2:
value1 = 3;
break;
case 3:
value1 = 4;
break;
case 4:
value1 = 5;
break;
case 5:
value1 = 6;
break;
case 6:
value1 = 7;
break;
case 7:
value1 = 8;
break;
case 8:
value1 = 9;
break;
case 9:
value1 = 10;
break;
case 10:
value1 = 10;
break;
case 11:
value1 = 10;
break;
case 12:
value1 = 10;
break;
}
switch (cardChooser2){ //second card
case 0:
value2 = 11;
if (value1 + value2 > 21){
value2 = 1;
}
break;
case 1:
value2 = 2;
break;
case 2:
value2 = 3;
break;
case 3:
value2 = 4;
break;
case 4:
value2 = 5;
break;
case 5:
value2 = 6;
break;
case 6:
value2 = 7;
break;
case 7:
value2 = 8;
break;
case 8:
value2 = 9;
break;
case 9:
value2 = 10;
break;
case 10:
value2 = 10;
break;
case 11:
value2 = 10;
break;
case 12:
value2 = 10;
break;
}
value = value1 + value2; //this adds the cards together for the players actual total
System.out.println("Cards Dealt : " + card1 + suit1 + " " + card2 + suit2); //prints the cards to main
System.out.println("Score: " + value );
if (value == 21){
System.out.println("Blackjack!");
}
total = value;
}
public void newCard(){ //this is used when the player chooses hit, or when the dealer has under 18
Random rng = new Random();
//this is essentially the same thing in the firstHand method, but with only 1 card
int cardChooser = rng.nextInt(cards.length);
String card = cards[cardChooser];
String suit = suits[rng.nextInt(suits.length)];
switch (cardChooser){
case 0:
newValue = 11;
if (newValue + value > 21){
newValue = 1;
}
break;
case 1:
newValue = 2;
break;
case 2:
newValue = 3;
break;
case 3:
newValue = 4;
break;
case 4:
newValue = 5;
break;
case 5:
newValue = 6;
break;
case 6:
newValue = 7;
break;
case 7:
newValue = 8;
break;
case 8:
newValue = 9;
break;
case 9:
newValue = 10;
break;
case 10:
newValue = 10;
break;
case 11:
newValue = 10;
break;
case 12:
newValue = 10;
break;
}
//this sums the first hand and the new card
value = value + newValue;
total = value;
System.out.println(card + suit);
System.out.println("Score: " + (total));
}
}
Player.java
import java.io.*;
import java.util.Scanner;
public class Player{
public Scanner sc = new Scanner(System.in);
public float money;
public float bet;
public String name = null, ignore = null, handsPlayedString = null, handsWonString = null, moneyString = null;
public int handsPlayed = 0, handsWon = 0;
public Player(){ //constructor for player
}
public void getInfo() throws IOException{
//this gets the persons name, then goes to the text file and gets the info
System.out.println("Please enter your name >");
name = sc.next();
System.out.println("Welcome " + name);
File filename = new File(name + ".txt");
if(!filename.exists()) { //this creates new text file if the person is new player
System.out.println("Have fun on your first time playing Infinite Blackjack.");
filename.createNewFile();
PrintWriter writer = new PrintWriter(name + ".txt");
writer.println(100.00);
writer.println(0);
writer.println(0);
writer.close();
}
//the following gets the info out as strings, then changes them to ints or floats
Scanner filesc = new Scanner(filename);
moneyString = filesc.nextLine();
money = Float.parseFloat(moneyString);
handsPlayedString = filesc.nextLine();
handsPlayed = Integer.parseInt(handsPlayedString);
handsWonString = filesc.nextLine();
handsWon = Integer.parseInt(handsWonString);
} //for getInfo
public void checkBet(){
boolean loop = true;
while (loop){ //this checks the input to make sure its a float
try{
System.out.println("How much would you like to bet? > ");
bet = sc.nextFloat();
loop = false;
}
catch (Exception e) {
sc.next();
}
}
while (bet > money || bet < 0){
System.out.println("Please enter a valid bet >");
bet = sc.nextInt();
}
}
public void lostHand() throws FileNotFoundException{
//this now updates hands played and money, as well as rewrites the info to the users text file
money = money - bet;
handsPlayed++;
System.out.printf("You lost $%.2f ",bet);
PrintWriter writer = new PrintWriter(name + ".txt");
writer.println(money);
writer.println(handsPlayed);
writer.println(handsWon);
writer.close();
}
public void wonHand() throws FileNotFoundException{
//updates the info for a win, and rewrites them to the users text file
money = money + bet;
handsPlayed++;
handsWon++;
System.out.printf("You won $%.2f ",bet);
PrintWriter writer = new PrintWriter(name + ".txt");
writer.println(money);
writer.println(handsPlayed);
writer.println(handsWon);
writer.close();
}
public void blackJack(){
//this is for a blackjack only. kind of unnecessary, but this is just how i did it
money = (money + bet + (bet/2));
System.out.printf("You won %.2f", (bet+bet*.5));
}
public void showInfo(){
//this shows the persons info
System.out.println("Name: " + name);
System.out.println("Total Hands: " + handsPlayed);
System.out.println("Hands Won: " + handsWon);
System.out.printf("Money: %.02f ", money);
}
}
output
Welcome to Infinite Blackjack!
Please enter your name >
Ram
Welcome Ram
Have fun on your first time playing Infinite Blackjack.
Name: Ram
Total Hands: 0
Hands Won: 0
Money: 100.00
Would you like to play a hand? (Y/N)
n