-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect4.js
146 lines (129 loc) · 3.38 KB
/
connect4.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
class Game {
constructor(p1, p2, height = 6, width = 7) {
this.players = [p1, p2];
this.height = height;
this.width = width;
this.currPlayer = p1;
this.makeBoard();
this.makeHtmlBoard();
this.gameOver = false;
}
makeBoard() {
this.board = [];
for (let y = 0; y < this.height; y++) {
this.board.push(Array.from({ length: this.width }));
}
}
makeHtmlBoard() {
const board = document.getElementById("board");
board.innerHTML = "";
const top = document.createElement("tr");
top.setAttribute("id", "column-top");
top.addEventListener("click", this.handleClick.bind(this));
for (let x = 0; x < this.width; x++) {
const headCell = document.createElement("td");
headCell.setAttribute("id", x);
top.append(headCell);
}
board.append(top);
// make main part of board
for (let y = 0; y < this.height; y++) {
const row = document.createElement("tr");
for (let x = 0; x < this.width; x++) {
const cell = document.createElement("td");
cell.setAttribute("id", `${y}-${x}`);
row.append(cell);
}
board.append(row);
}
}
findSpotForCol(x) {
for (let y = this.height - 1; y >= 0; y--) {
if (!this.board[y][x]) {
return y;
}
}
return null;
}
placeInTable(y, x) {
const piece = document.createElement("div");
piece.classList.add("piece");
piece.style.backgroundColor = this.currPlayer.color;
piece.style.top = -50 * (y + 2);
const spot = document.getElementById(`${y}-${x}`);
spot.append(piece);
}
endGame(msg) {
alert(msg);
}
handleClick(evt) {
const x = +evt.target.id;
const y = this.findSpotForCol(x);
if (y === null) {
return;
}
this.board[y][x] = this.currPlayer;
this.placeInTable(y, x);
if (this.board.every((row) => row.every((cell) => cell))) {
return this.endGame("Tie!");
}
if (this.checkForWin()) {
this.gameOver = true;
return this.endGame(`The ${this.currPlayer.color} player won!`);
}
this.currPlayer =
this.currPlayer === this.players[0] ? this.players[1] : this.players[0];
}
checkForWin() {
const _win = (cells) =>
cells.every(
([y, x]) =>
y >= 0 &&
y < this.height &&
x >= 0 &&
x < this.width &&
this.board[y][x] === this.currPlayer
);
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
const horiz = [
[y, x],
[y, x + 1],
[y, x + 2],
[y, x + 3],
];
const vert = [
[y, x],
[y + 1, x],
[y + 2, x],
[y + 3, x],
];
const diagDR = [
[y, x],
[y + 1, x + 1],
[y + 2, x + 2],
[y + 3, x + 3],
];
const diagDL = [
[y, x],
[y + 1, x - 1],
[y + 2, x - 2],
[y + 3, x - 3],
];
if (_win(horiz) || _win(vert) || _win(diagDR) || _win(diagDL)) {
return true;
}
}
}
}
}
class Player {
constructor(color) {
this.color = color;
}
}
document.getElementById("start-game").addEventListener("click", () => {
let p1 = new Player(document.getElementById("p1-color").value);
let p2 = new Player(document.getElementById("p2-color").value);
new Game(p1, p2);
});