-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.java
More file actions
136 lines (127 loc) · 5.21 KB
/
Driver.java
File metadata and controls
136 lines (127 loc) · 5.21 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
import java.util.Scanner;
/**
* This class is the driver for the blackjack game.
* @author Crishna Iyengar
* @version 1.0
*/
public class Driver {
/**
* This method is the main method. Executes all the code.
* @param args
*/
public static void main(String[] args) {
while (true) {
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to Blackjack! What is your name?");
String playerName = scan.nextLine();
boolean willPlay = true;
Player player = new Player();
Player dealer = new Player();
Driver swag = new Driver();
System.out.println("Goal is to get as close to 21 "
+ "as possible without going over.");
System.out.println("The first cards will now be dealt.\n");
scan.nextLine();
Cards card0 = swag.dealFirstCards(player, dealer, playerName);
boolean giveMeAnother = true;
while (giveMeAnother
&& (player.getPlayerScore() < 22)) {
System.out.println("Would you like another card?"
+ " (1 - yes 2 - no 3 - view stats)");
int answer = scan.nextInt();
scan.nextLine();
if (answer == 1) {
swag.hitMePlayer(player, playerName);
} else if (answer == 3) {
System.out.println("You: " + player.getPlayerScore()
+ " Dealer: " + dealer.getPlayerScore());
} else {
giveMeAnother = false;
}
}
if (player.getPlayerScore() > 21) {
System.out.println("You lose! Would you like to play again? "
+ " (1-yes 2-no)");
int answer = scan.nextInt();
scan.nextLine();
if (answer == 2) {
willPlay = false;
System.exit(0);
}
} else {
System.out.println("Now it is the dealer's turn!");
scan.nextLine();
swag.dealerTurn(dealer, card0);
while (dealer.getPlayerScore() < 17) {
swag.hitMeDealer(dealer);
scan.nextLine();
}
}
if (player.getPlayerScore() < 22) {
if (dealer.getPlayerScore() > 21) {
System.out.println("The dealer busted!");
System.out.println(playerName + " wins! "
+ "Would you like to play again (1-yes 2-no)");
int answer = scan.nextInt();
if (answer == 2) {
System.exit(0);
}
} else if (dealer.getPlayerScore() > player.getPlayerScore()) {
System.out.println("The dealer will "
+ "not draw any more cards.");
System.out.println("The House wins! "
+ "Would you like to play again? (1-yes 2-no)");
int answer = scan.nextInt();
scan.nextLine();
if (answer == 2) {
System.exit(0);
}
} else if (dealer.getPlayerScore() == player.getPlayerScore()) {
System.out.println("The scores are even!");
System.out.println("The House wins!"
+ " Would you like to play again? (1-yes 2-no)");
int answer = scan.nextInt();
scan.nextLine();
if (answer == 2) {
System.exit(0);
}
} else {
System.out.println("Congrats!");
System.out.println(playerName + " wins! "
+ "Would you like to play again? (1-yes 2-no)");
int answer = scan.nextInt();
scan.nextLine();
if (answer == 2) {
System.exit(0);
}
}
}
Cards.setDealtCards();
Cards.setCardCount();
}
}
private Cards dealFirstCards(Player player,
Player dealer, String playerName) {
System.out.println("The first two cards have now been dealt!");
Cards card0 = new Cards(false, "The Dealer");
Cards card1 = new Cards(true, "The Dealer");
Cards card2 = new Cards(true, playerName);
Cards card3 = new Cards(true, playerName);
player.setPlayerScore(card2.getCardValue());
player.setPlayerScore(card3.getCardValue());
dealer.setPlayerScore(card1.getCardValueDealer(dealer));
return card0;
}
private void hitMePlayer(Player player, String playerName) {
Cards card = new Cards(true, playerName);
player.setPlayerScore(card.getCardValue());
}
private void dealerTurn(Player dealer, Cards card0) {
card0.setFaceUp(true);
dealer.setPlayerScore(card0.getCardValueDealer(dealer));
}
private void hitMeDealer(Player dealer) {
Cards card = new Cards(true, "The Dealer");
dealer.setPlayerScore(card.getCardValueDealer(dealer));
}
}