-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlackJack.java
More file actions
68 lines (58 loc) · 1.61 KB
/
BlackJack.java
File metadata and controls
68 lines (58 loc) · 1.61 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
package hw3;
import java.util.Scanner;
/*
* This class actually plays a game.
*/
public class BlackJack {
public static void main(String[] args) {
// Creates a new game.
Game game=new Game();
game.firstDeal();
Scanner in = new Scanner(System.in);
boolean flag=true; // Indicates whether to end the loop for dealing with a player or not.
boolean end=false; // Indicates whether to end the game or not.
/*
* First deal with the human player. The player can input whether he/she wants to continue or not.
* At a point where the player's score exceeds 21, the game ends.
*/
while(flag && (!end)) {
System.out.println("Please indicate if you want to get one more card. 1 for true, 2 for false.");
if(in.nextInt()==1) {
game.hPlayerStands(false);
game.hPlayerGetCard();
}
else {
game.hPlayerStands(true);
flag=false;
}
if(game.getHPlayerScore()>21) {
game.endGame(true, false, false);
flag=true;
end=true;
}
}
/*
* Then we can deal with the computer player now. Since its strategy is fixed, no input is needed.
* Similarly, at a point where the player's score exceeds 21, the game ends.
*/
flag=true;
while(flag && (!end)) {
if(!game.cPlayerStands()) {
game.cPlayerGetCard();
}
else {
flag=false;
}
if(game.getCPlayerScore()>21) {
game.endGame(false, true, false);
flag=true;
end=true;
}
}
// If both of the players stand and no one's score exceeds 21, the game ends here with a comparison to determine who is the winner.
if(!end) {
game.endGame(false, false, true);
}
in.close();
}
}