-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect4-oo.js
171 lines (153 loc) · 4.55 KB
/
connect4-oo.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
169
170
171
/** Connect Four
*
* Player 1 and 2 alternate turns. On each turn, a piece is dropped down a
* column until a player gets four-in-a-row (horiz, vert, or diag) or until
* board fills (tie)
*/
// const WIDTH = 7;
// const HEIGHT = 6;
// let currPlayer = 1; // active player: 1 or 2
// let board = []; // array of rows, each row is array of cells (board[y][x])
class Game {
constructor(height = 6, width = 7, p1 = 1, p2 = 2) {
this.height = height;
this.width = width;
this.currPlayer = p1;
this.players = [ p1, p2 ];
// this.handleClickStart();
}
handleClickStart(player) {
this.player = player;
const start = document.getElementById('flex-box');
let leiahThinking = document.getElementById("really") ;
start.setAttribute('class', 'hidden');
leiahThinking.style.visibility = "visible" ;
this.makeBoard();
this.makeHtmlBoard();
}
makeBoard() {
this.board = [];
for (let i = 0; i < this.height; i++) {
let arr = [];
for (let j = 0; j < this.width; j++) {
arr.push(0);
}
this.board.push(arr);
}
}
makeHtmlBoard() {
const board1 = document.getElementById('board');
// make column tops (clickable area for adding a piece to that column)
const top = document.createElement('tr');
top.setAttribute('id', 'column-top');
this.moveHuman = this.moveHuman.bind(this); /////?????
top.addEventListener('click', this.moveHuman);
for (let x = 0; x < this.width; x++) {
const headCell = document.createElement('td');
headCell.setAttribute('id', x);
top.append(headCell);
}
board1.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);
}
board1.append(row);
}
}
findSpotForCol(x,board) {
for (let y = this.height - 1; y >= 0; y--) {
if (!board[y][x]) {
return y;
}
}
return null;
}
placeInTable(y, x) {
const piece = document.createElement('div');
piece.classList.add('piece');
piece.classList.add(`p${this.currPlayer}`);
piece.style.top = -50 * (y + 2);
const spot = document.getElementById(`${y}-${x}`);
spot.append(piece);
}
endGame(msg) {
setTimeout(()=>{if(confirm(msg)) {
location.reload();
}},300)
}
moveHuman(evt) {
// get x from ID of clicked cell
const x = +evt.target.id;
// get next spot in column (if none, ignore click)
const y = this.findSpotForCol(x,this.board);
if (y === null) {
return;
}
// place piece in board and add to HTML table
this.board[y][x] = this.currPlayer;
this.placeInTable(y, x);
this.evaluateMove();
}
evaluateMove() {
// check for win
if (this.checkForWin(this.currPlayer, this.board)) {
this.gameOver = true;
if(this.currPlayer===2){
return this.endGame(`Leiah won!`);
}
return this.endGame(`Leiah let you win!`);
}
// check for tie
if (this.board.every((row) => row.every((cell) => cell))) {
return this.endGame('Tie!');
}
// switch players
if (this.currPlayer === this.players[0]) {
this.currPlayer = this.players[1];
if (this.player) this.moveAI();
} else {
this.currPlayer = this.players[0];
}
}
moveAI() {
let leiahMoving = document.getElementById("whatThe") ;
let leiahThinking = document.getElementById("really") ;
let move = aiTurn(this.board);
let y = move[0];
let x = move[1];
leiahMoving.style.visibility = "visible" ;
leiahThinking.style.visibility = "hidden" ;
this.board[y][x] = this.currPlayer;
setTimeout(()=>{
this.placeInTable(y, x);
this.evaluateMove();
leiahMoving.style.visibility = "hidden" ;
leiahThinking.style.visibility = "visible" ;
}
,700)
}
checkForWin(player, board) {
const _win = (cells) =>
cells.every(([ y, x ]) => y >= 0 && y < this.height && x >= 0 && x < this.width && board[y][x] === player);
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
// get "check list" of 4 cells (starting here) for each of the different
// ways to win
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 ] ];
// find winner (only checking each win-possibility as needed)
if (_win(horiz) || _win(vert) || _win(diagDR) || _win(diagDL)) {
return true;
}
}
}
}
}
let game = new Game();