-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
156 lines (131 loc) · 2.9 KB
/
Game.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
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
156
//Black Jack Main Game Class
public class Game {
// Game Variables
public Deck deck = new Deck();
// Player and AI objects
public Player player = new Player();
public AI aI = new AI();
//Score counters
private int playerScore = 0;
private int aiScore = 0;
private int playerBet = 0;
// Winner variable
private int winner = 0; // 1-player, 2-AI, 0-draw
// game constructor
public Game() {
deck.Shuffle();
}
// Game Reset
public void resetGame() {
player = new Player();
aI = new AI();
playerScore = 0;
aiScore = 0;
playerBet = 0;
winner = 0;
}
//Dealing of initial cards to player and AI.
public void dealCards() {
player.addCard(deck.giveCard());
player.addCard(deck.giveCard());
aI.addCard(deck.giveCard());
aI.addCard(deck.giveCard());
}
//get winner
public String getWinner()
{
if (winner == 1)
return "You";
else if (winner == 2)
return "AI";
return "No one";
}
/**
* set player's bet.
* @param bet - player's bet which is deducted from player's money.
* @param money - player's money object.
*/
public void playerBet(int bet, Money money) {
playerBet = bet;
money.updatePlayerMoney(-bet);
}
// get player's bet
public int getPlayerBet() {
return playerBet;
}
// get pot
public int getPot() {
return playerBet * 2;
}
// get Player's initial score
public int playerScore() {
playerScore = player.hand[0].getValue() + player.hand[1].getValue();
return playerScore;
}
// get AI's initial score
public int aiScore() {
aiScore = aI.hand[0].getValue() + aI.hand[1].getValue();
return aiScore;
}
// get Player's Hand
public Card[] playerHand() {
return player.hand;
}
// get AI's Hand
public Card[] aiHand() {
return aI.hand;
}
/**
* Compare scores to determine winner.
* @param money - player's money object.
*/
public void decideWinner(Money money) {
aiScore = aI.calcScore();
playerScore = player.calcScore();
// if both have same score
if(aiScore==playerScore)
{
winner = 0;
money.updatePlayerMoney(playerBet);
}
// if both player and AI bust (Have a total over 21)
else if ((aiScore > 21) && (playerScore > 21)) {
if (aiScore > playerScore) {
winner = 1;
money.updatePlayerMoney(getPot());
}
else {
winner = 2;
if (money.getValue() == 0)
BJ.isGameOver = true;
}
}
// if player busts and AI doesn't
else if ((aiScore <= 21) && (playerScore > 21)) {
winner = 2;
if (money.getValue() == 0)
BJ.isGameOver = true;
}
// if AI busts and player doesn't
else if ((aiScore > 21) && (playerScore <= 21)) {
winner = 1;
money.updatePlayerMoney(getPot());
}
// if neither busts
else
{
int a = 21 - aiScore;
int b = 21 - playerScore;
// finding the closest to 21
if (a > b) {
winner = 1;
money.updatePlayerMoney(getPot());
}
else {
winner = 2;
if (money.getValue() == 0)
BJ.isGameOver = true;
}
}
}
}