forked from appacademy-starters/oop-connect-four
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
147 lines (133 loc) · 3.82 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
import { Column } from "./column.js";
import { ColumnWinInspector } from "./column-win-inspector.js";
import { RowWinInspector } from "./row-win-inspector.js";
import { DiagonalWinInspector } from "./diagonal-win-inspector.js";
export class Game {
constructor(playerOneName, playerTwoName) {
this.playerOneName = playerOneName;
this.playerTwoName = playerTwoName;
this.currentPlayer = 1;
this.columns = [];
for (let i = 0; i < 7; i++) {
this.columns.push(new Column());
}
this.winnerNum = 0; // 3 for tie
}
getName() {
if (this.winnerNum === 3) {
return `${this.playerOneName} ties with ${this.playerTwoName}!`;
} else if (this.winnerNum === 2) {
return `${this.playerTwoName} wins!`;
} else if (this.winnerNum === 1) {
return `${this.playerOneName} wins!`;
}
return `${this.playerOneName} vs. ${this.playerTwoName}`;
}
playInColumn(index) {
this.checkForTie();
if (this.winnerNum === 0) {
this.columns[index].add(this.currentPlayer);
this.checkForColumnWin();
this.checkForRowWin();
this.checkForDiagonalWin();
}
if (this.currentPlayer === 1) {
this.currentPlayer = 2;
} else {
this.currentPlayer = 1;
}
}
getTokenAt(rowIndex, colIndex) {
return this.columns[colIndex].getTokenAt(rowIndex);
}
updateUI() {
const clickTarget = document.getElementById("click-targets");
if (this.currentPlayer === 1) {
clickTarget.classList.add("red");
clickTarget.classList.remove("black");
} else {
clickTarget.classList.remove("red");
clickTarget.classList.add("black");
}
for (let i = 5; i >= 0; i--) {
for (let j = 0; j <= 6; j++) {
const square = document.getElementById(`square-${i}-${j}`);
const playerNum = this.getTokenAt(i, j);
square.innerHTML = "";
if (playerNum === 1) {
const blackToken = document.createElement("div");
blackToken.setAttribute("class", "token red");
square.appendChild(blackToken);
} else if (playerNum === 2) {
const redToken = document.createElement("div");
redToken.setAttribute("class", "token black");
square.appendChild(redToken);
}
}
}
for (let i = 0; i <= 6; i++) {
const col = document.getElementById(`column-${i}`);
if (this.isColumnFull(i)) {
col.classList.add("full");
} else {
col.classList.remove("full");
}
}
}
isColumnFull(index) {
if (this.winnerNum === 1 || this.winnerNum === 2) {
return true;
}
return this.columns[index].isFull();
}
checkForTie() {
for (let i = 0; i <= 6; i++) {
if (!this.columns[i].isFull()) {
return;
}
}
this.winnerNum = 3;
}
checkForColumnWin() {
for (let i = 0; i < 7; i++) {
const colInspect = new ColumnWinInspector(this.columns[i]);
if (colInspect.inspect() === 1 || colInspect.inspect() === 2) {
this.winnerNum = colInspect.inspect();
return;
}
}
}
checkForRowWin() {
if (this.winnerNum > 0) {
return;
}
for (let i = 0; i < 4; i++) {
let args = this.columns.slice(i, i + 4);
const rowInspect = new RowWinInspector(...args);
if (rowInspect.inspect() > 0) {
this.winnerNum = rowInspect.inspect();
break;
}
}
}
checkForDiagonalWin() {
if (this.winnerNum > 0) {
return;
}
for (let i = 0; i < 4; i++) {
let args = this.columns.slice(i, i + 4);
const diagonalInspect = new DiagonalWinInspector(...args);
if (diagonalInspect.inspect() > 0) {
this.winnerNum = diagonalInspect.inspect();
break;
}
}
}
setCurrentPlayer(number) {
if (number === 1) {
this.currentPlayer = 2;
} else {
this.currentPlayer = 1;
}
}
}