-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlackjack.java
107 lines (95 loc) · 3.06 KB
/
Blackjack.java
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
package blackjack_sim;
import java.util.Scanner;
import java.util.Random;
public class Blackjack {
public static void main(String[] args) {
System.out.println("Welcome to Blackjack!");
int balance = 100;
String playagain;
do {
/*place your bet*/
Scanner scanbet = new Scanner(System.in);
System.out.println("You have "+balance+". Place your bet ($): ");
int bet = scanbet.nextInt();
/*deal first cards - 2 each*/
Random random = new Random();
int dealercards = random.nextInt(13) + 1;
System.out.println("One of dealer's cards is: "+dealercards);
Random random1 = new Random();
dealercards += random1.nextInt(13) + 1;
Random random2 = new Random();
int playercards = random2.nextInt(13) + 1;
Random random3 = new Random();
playercards += random3.nextInt(13) + 1;
System.out.println("Your total: "+playercards);
while (playercards < 21) {
/*ask player for move*/
Scanner in = new Scanner(System.in);
System.out.println("Enter an action (Options: Hit, Stand): ");
String firstaction = in.nextLine();
/*prepare next card*/
Random random4 = new Random();
/*execute move*/
/*if hit*/
if (firstaction.equalsIgnoreCase("hit")){
playercards += random4.nextInt(13) + 1;
System.out.println(playercards);
}
/*if stand*/
else if (firstaction.equalsIgnoreCase("stand")) {
{
if (playercards > dealercards) {
System.out.println("You won!");
balance = balance + bet;
}
else if (playercards == dealercards)
System.out.println("Push");
else if (playercards < dealercards && dealercards <= 21) {
System.out.println("You lose.");
balance = balance - bet;
}
else if (playercards < dealercards && dealercards > 21) {
System.out.println("Dealer busts, you win!");
balance = balance + bet;
}
}
break;
}
/*if other*/
else {
System.out.println("Invalid move.");
}
}
/*BJ or bust*/
if (playercards > 21) {
System.out.println("Bust");
balance = balance - bet;
}
else if (playercards == 21) {
System.out.println("Black jack!");
balance = balance + bet;
}
/*results*/
System.out.println("Dealer's cards: "+dealercards);
Scanner again = new Scanner(System.in);
System.out.println("Play again? (Y/N): ");
playagain = again.nextLine();
System.out.println("");
}
while (playagain.equalsIgnoreCase("y"));
}
/*to do: double, split, insurance*/
/*if (playercards % 2 == 0) {
Scanner splitter = new Scanner(System.in);
System.out.println("Split? (Y/N): ");
String splitdecision = splitter.nextLine();
if (splitdecision.equalsIgnoreCase("y")){
playercards = playercards/2;
int playercards2 = playercards;
}
else if (splitdecision.equalsIgnoreCase("n")) {
continue;
}
}
*/
}