-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
205 lines (175 loc) · 5.17 KB
/
script.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const BOARD_SIZE = 3;
const gameBoard = ((squares) => {
let board = new Array();
for (let index = 0; index < squares; index++) {
board.push(new Array(squares));
}
function clearBoard() {
for (x = 0; x < squares; x++) {
for (y = 0; y < squares; y++) {
board[x][y] = 0;
}
}
}
function getSquare(x, y) {
return board[x][y];
}
function setSquare(x, y, value) {
board[x][y] = value;
}
function _rowWin(y) {
return _stepWin(0, y, 1, 0);
}
function _columnWin(x) {
return _stepWin(x, 0, 0, 1);
}
function _diagonalWin() {
return (_stepWin(0, 0, 1, 1) || _stepWin(0, squares - 1, 1, -1));
}
function _stepWin(xStart, yStart, xStep, yStep) {
let xCurrent = xStart;
let yCurrent = yStart;
let current = getSquare(xStart, yStart);
let next;
while ((xCurrent + xStep < squares) && (yCurrent + yStep < squares)) {
next = getSquare(xCurrent + xStep, yCurrent + yStep);
if (next == 0 || current != next) {
return 0;
}
xCurrent += xStep;
yCurrent += yStep;
current = next;
}
return current;
}
function winner() {
let result = 0;
for (let i = 0; i < squares; i++) {
result = _rowWin(i);
if (result != 0) {
return result;
}
result = _columnWin(i);
if (result != 0) {
return result;
}
}
return _diagonalWin();
}
function boardFull() {
for (let x = 0; x < squares; x++) {
for (let y = 0; y < squares; y++) {
if (getSquare(x, y) == 0) {
return false;
}
}
}
return true;
}
return {
getSquare,
setSquare,
clearBoard,
board,
winner,
boardFull,
}
})(BOARD_SIZE);
const playerFactory = (name, marker) => {
return { name, marker };
};
const displayController = ((displaySelector, boardSelector, squares) => {
const container = document.querySelector(boardSelector);
const display = document.querySelector(displaySelector);
function setupBoard() {
for (y = squares - 1; y >= 0; y--) {
for (x = 0; x < squares; x++) {
let boardSquare = document.createElement("div");
boardSquare.setAttribute('class', 'game-space');
boardSquare.setAttribute('onclick', `game.clickSquare(${x},${y})`)
container.appendChild(boardSquare);
}
}
let sheet = document.createElement('style');
sheet.innerHTML = `#game-board {grid-template-columns: repeat(${squares}, 1fr); grid-template-rows: repeat(${squares}, 1fr) }`;
document.body.appendChild(sheet);
}
function renderBoard() {
let index = 0;
for (y = squares - 1; y >= 0; y--) {
for (x = 0; x < squares; x++) {
let boardSquare = container.children[index];
let value = gameBoard.getSquare(x, y);
if (value != 0) {
boardSquare.innerHTML = value;
} else {
boardSquare.innerHTML = '';
}
index++;
}
}
}
function setText(text) {
display.innerHTML = text;
}
return {
setupBoard,
renderBoard,
setText,
}
})('#alert-area', '#game-board', BOARD_SIZE);
const game = (() => {
let activePlayer = 1;
let player1;
let player2;
let gameOver = 1;
function clickSquare(x, y) {
if (!gameOver && gameBoard.getSquare(x, y) == 0) {
gameBoard.setSquare(x, y, activePlayer.marker);
_swapActivePlayer();
_displayTurn();
_updateGameOver();
displayController.renderBoard();
}
}
function startGame() {
player1 = playerFactory(document.querySelector('#p1-area').value, 'X');
player2 = playerFactory(document.querySelector('#p2-area').value, 'O');
gameOver = 0;
activePlayer = player1;
gameBoard.clearBoard();
_displayTurn();
displayController.renderBoard();
}
function _displayWinner() {
if (gameBoard.winner() != 0) {
displayController.setText(`Player ${gameBoard.winner()} wins!`);
} else {
displayController.setText("It's a draw!");
}
}
function _updateGameOver() {
if (gameBoard.winner() != 0 || gameBoard.boardFull()) {
gameOver = 1;
_displayWinner();
} else {
gameOver = 0;
}
}
function _displayTurn() {
displayController.setText(`Current turn: ${activePlayer.name}`);
}
function _swapActivePlayer() {
if (activePlayer == player1) {
activePlayer = player2;
} else {
activePlayer = player1;
}
}
return {
clickSquare,
startGame,
}
})();
displayController.setupBoard();
gameBoard.clearBoard();