-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlackjack.java
More file actions
155 lines (138 loc) · 6.46 KB
/
Blackjack.java
File metadata and controls
155 lines (138 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.util.Scanner;
import java.util.Random;
public class Blackjack {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random rand = new Random();
//declare variables
int playerCardValue = 0;
int playerHand = 0;
int totalGames = 1;
int playerWins = 0;
int dealerHand = 0;
int dealerWins = 0;
int numOfTies = 0;
boolean gameOver = false;
while (!gameOver) {
//when playerHand is 0, that indicates that the game has been reset (new game has started)
if (playerHand == 0) {
System.out.println("\nSTART GAME #" + totalGames);
playerCardValue = rand.nextInt(13) + 1;
if (playerCardValue == 1) {
System.out.println("\nYour card is a ACE!");
} else if (playerCardValue == 11) {
System.out.println("\nYour card is a JACK!");
} else if (playerCardValue == 12) {
System.out.println("\nYour card is a QUEEN!");
} else if (playerCardValue == 13) {
System.out.println("\nYour card is a KING!");
} else {
System.out.println("\nYour card is a " + playerCardValue + "!");
}
//determine the player's hand
if (playerCardValue > 0 && playerCardValue < 11) {
playerHand = playerHand + playerCardValue;
} else if (playerCardValue > 10) {
playerHand += 10;
}
System.out.println("Your hand is: " + playerHand);
}
//present the menu and store the player's choice in menuChoice
System.out.println("\n1. Get another card");
System.out.println("2. Hold hand");
System.out.println("3. Print statistics");
System.out.println("4. Exit");
System.out.println("\nChoose an option: ");
int menuChoice = scanner.nextInt();
//1: give the player a new card
if (menuChoice == 1) {
playerCardValue = rand.nextInt(13) + 1;
if (playerCardValue == 1) {
System.out.println("\nYour card is a ACE!");
} else if (playerCardValue == 11) {
System.out.println("\nYour card is a JACK!");
} else if (playerCardValue == 12) {
System.out.println("\nYour card is a QUEEN!");
} else if (playerCardValue == 13) {
System.out.println("\nYour card is a KING!");
} else {
System.out.println("\nYour card is a " + playerCardValue + "!");
}
if (playerCardValue > 0 && playerCardValue < 11) {
playerHand = playerHand + playerCardValue;
} else if (playerCardValue > 10) {
playerHand += 10;
}
System.out.println("Your hand is: " + playerHand);
//determine the game's flow based on the player's hand
if (playerHand == 21){
System.out.println("BLACKJACK! You win!");
playerWins ++;
totalGames ++;
playerHand = 0;
dealerHand = 0;
} else if (playerHand > 21) {
System.out.println("You exceeded 21! You lose.");
totalGames++;
dealerWins++;
playerHand = 0;
dealerHand = 0;
}
//2: give the dealer's hand and compare dealerHand to playerHand
} else if (menuChoice == 2) {
dealerHand = rand.nextInt(11) + 16;
//player automatically wins if the dealer's hand is more than 21
if (dealerHand > 21){
System.out.println("\nDealer's hand: " + dealerHand);
System.out.println("Your hand is: " + playerHand);
System.out.println("\nYou win!");
playerWins ++;
totalGames ++;
playerHand = 0;
dealerHand = 0;
//tie game scenario
} else if (dealerHand == playerHand){
System.out.println("\nDealer's hand: " + dealerHand);
System.out.println("Your hand is: " + playerHand);
System.out.println("\nIt's a tie! No one wins!");
totalGames ++;
numOfTies ++;
playerHand = 0;
dealerHand = 0;
//scenario in which dealer wins
} else if (dealerHand > playerHand) {
System.out.println("\nDealer's hand: " + dealerHand);
System.out.println("Your hand is: " + playerHand);
System.out.println("\nDealer wins!");
dealerWins ++;
totalGames ++;
playerHand = 0;
dealerHand = 0;
//scenario in which player wins
} else if (dealerHand < playerHand){
System.out.println("\n1Dealer's hand: " + dealerHand);
System.out.println("Your hand is: " + playerHand);
System.out.println("\nYou win!");
playerWins++;
totalGames++;
playerHand = 0;
dealerHand = 0;
}
//print game summary
} else if (menuChoice == 3){
System.out.println("Number of Player wins: " + playerWins);
System.out.println("Number of Dealer wins: " + dealerWins);
System.out.println("Number of tie games: " + numOfTies);
System.out.println("Total # of games played is: " + (totalGames - 1));
double percentageOfPlayerWins = playerWins * 100.0/ (totalGames - 1);
System.out.println("Percentage of Player wins: " + percentageOfPlayerWins + "%");
//exit game scenario
} else if (menuChoice == 4){
System.exit(0);
//alert to correct inappropriate input
} else {
System.out.println("\nInvalid input! \nPlease enter an integer value between 1 and 4.");
}
}
}
}