forked from allbymatty/cs174-billiards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
168 lines (150 loc) · 6.08 KB
/
game.js
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
157
158
159
160
161
162
163
164
165
166
167
168
const PLAYER1 = 0;
const PLAYER2 = 1;
class Game {
constructor(gui) {
this.gui = gui;
// The numbered balls remaining on the table
this.remaining_balls = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
// Player 1 vs Player 2
// Player 1 starts first
this.turnName = 'player1';
this.turnNum = PLAYER1;
// Indicates the teams of the players (striped vs solid)
this.teams = ['', ''];
}
setTeams(player, team) {
if (player == 'player1') {
this.teams[PLAYER1] = (team == 'solid' ? 'solid': 'striped');
this.teams[PLAYER2] = (team == 'solid' ? 'striped' : 'solid');
}
else {
this.teams[PLAYER2] = (team == 'solid' ? 'solid': 'striped');
this.teams[PLAYER1] = (team == 'solid' ? 'striped' : 'solid');
}
}
// takes list of pocket balls, in order of sinking
// returns true if game over
pocketedBalls(ballNums) {
//check if they scratched and also got 8-ball in, losing game
if (ballNums.includes(0) && ballNums.includes(8)) {
winner = this.turnName == 'player1' ? 'player2' : 'player1';
this.gui.endGame(winner);
return true;
}
else if (ballNums.includes(0)) {
// remove any other pocketed balls from remaining_balls
// set teams if needed
let ballType = 'none';
for (let i = 0; i < ballNums.length; i++) {
if (ballNums[i] != 0) {
// set teams if not set
if (this.teams[PLAYER1] == '') {
// Find the type of the pocketed ball
if (ballType == 'none') {
if (ballNums[i] < 8)
ballType = 'solid';
else
ballType = 'striped';
}
this.setTeams(this.turnName, ballType);
}
for (let j = 0; j < this.remaining_balls.length; j++) {
if (ballNums[i] == this.remaining_balls[j]) {
this.remaining_balls.splice(j, 1);
break;
}
}
}
}
// Scratch
alert("SCRATCH!");
this.turnName = (this.turnName == 'player1' ? 'player2' : 'player1');
this.turnNum = (this.turnNum == PLAYER1 ? PLAYER2 : PLAYER1);
this.gui.updateTurn(this.turnName);
this.gui.updateHUD(this.remaining_balls, this.teams);
}
else if (ballNums.includes(8)) {
// remove any other pocketed balls from remaining_balls
// remove ONLY those that were sunk before 8 ball
let i = 0;
for (; i < ballNums.length && ballNums[i] != 8; i++) {
for (let j = 0; j < this.remaining_balls.length; j++) {
if (ballNums[i] == this.remaining_balls[j]) {
this.remaining_balls.splice(j, 1);
break;
}
}
}
this.gui.updateHUD(this.remaining_balls, this.teams);
// Assume winning
let valid = true;
// Check remaining balls of player who made the 8-ball
if (this.teams[this.turnNum] == 'solid') { // SOLID
for (var k = 0; k < this.remaining_balls.length; k++) {
if (this.remaining_balls[k] < 8)
valid = false;
}
}
else { // STRIPED
for (var k = 0; k < this.remaining_balls.length; k++) {
if (this.remaining_balls[k] > 8)
valid = false;
}
}
// Declare winner
var winner;
if (valid)
winner = this.turnName;
else
winner = this.turnName == 'player1' ? 'player1' : 'player2',
this.gui.endGame(winner);
return true;
}
else {
// remove balls from remaining_balls
let ballType = 'none';
for (let i = 0; i < ballNums.length; i++) {
// Find the type of the pocketed ball
if (ballType == 'none') {
if (ballNums[i] < 8)
ballType = 'solid';
else
ballType = 'striped';
}
else {
if (ballType == 'solid' && ballNums[i] > 8)
ballType = 'mixed';
else if (ballType == 'striped' && ballNums[i] < 8)
ballType = 'mixed';
}
// Set teams if have not already
if (this.teams[PLAYER1] == '')
this.setTeams(this.turnName, ballType);
// Remove ball from remaining
for (var k = 0; k < this.remaining_balls.length; k++) {
if (ballNums[i] == this.remaining_balls[k]) {
this.remaining_balls.splice(k, 1);
break;
}
}
}
// If player made other player's ball, switch turns
// Otherwise keep turn
console.log(this.turnName);
console.log(this.teams[this.turnNum]);
console.log(ballType);
console.log("DONE");
if (this.teams[this.turnNum] != ballType) {
this.turnName = (this.turnName == 'player1' ? 'player2' : 'player1');
this.turnNum = (this.turnNum == PLAYER1 ? PLAYER2 : PLAYER1);
console.log(this.turnName);
console.log(this.turnNum);
}
this.gui.updateTurn(this.turnName);
// Make corresponding updates to HUD
this.gui.updateHUD(this.remaining_balls, this.teams);
}
// game not over, so return false
return false;
}
}