forked from Irit-Basic-JS/1-tic-tac-toe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
202 lines (175 loc) · 4.66 KB
/
index.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
const CROSS = 'X';
const ZERO = 'O';
const EMPTY = ' ';
const container = document.getElementById('fieldWrapper');
let field;
let dimension;
let movesLeft;
let isGameOver;
startGame();
addResetListener();
function startGame () {
dimension = prompt("Укажите размер поля");
movesLeft = dimension**2;
field = [];
isGameOver = false;
renderGrid(dimension);
}
function renderGrid (dimension) {
container.innerHTML = '';
for (let i = 0; i < dimension; i++) {
const row = document.createElement('tr');
field.push([]);
for (let j = 0; j < dimension; j++) {
const cell = document.createElement('td');
cell.textContent = EMPTY;
field[i][j] = EMPTY;
cell.addEventListener('click', () => cellClickHandler(i, j));
row.appendChild(cell);
}
container.appendChild(row);
}
}
function cellClickHandler (row, col) {
if (isGameOver){
return;
}
if (findCell(row, col).textContent === EMPTY){
if (movesLeft % 2 == 0){
makeMove(CROSS, row, col);
}
else{
makeMove(ZERO, row, col);
}
}
console.log(`Clicked on cell: ${row}, ${col}`);
}
function makeMove(symbol, row, col){
renderSymbolInCell(symbol, row, col);
field[row][col] = symbol;
movesLeft--;
if (checkWinner(symbol, row, col)){
switch (symbol){
case CROSS:
alert('Победили крестики!');
break;
case ZERO:
alert('Победили нолики!');
break;
}
isGameOver = true;
}
if (movesLeft === 0 && !isGameOver)
alert('Победила дружба!');
}
function checkWinner (symbol, row, col) {
let winFound = true;
let winLine;
for (let i = 0; i < dimension; i++) {
if (field[row][i] !== symbol) {
winFound = false;
break;
}
winFound = true;
}
winLine = "vertical";
if (!winFound) {
for (let i = 0; i < dimension; i++) {
if (field[i][col] !== symbol) {
winFound = false;
break;
}
winFound = true;
}
winLine = "horizontal";
}
if (!winFound) {
for (let i = 0; i < dimension; i++) {
if (field[i][i] !== symbol) {
winFound = false;
break;
}
winFound = true;
}
winLine = "diagonal";
}
if (!winFound) {
for (let i = 0; i < dimension; i++) {
if (field[i][dimension - (1 + i)] !== symbol) {
winFound = false;
break;
}
winFound = true;
}
winLine = "anti-diagonal";
}
if (winFound)
colorWinningLine(winLine, row, col);
return winFound;
}
function colorWinningLine(winLine, row, col){
for (let i = 0; i < dimension; i++) {
switch (winLine) {
case "vertical":
changeColor(findCell(row, i));
break;
case "horizontal":
changeColor(findCell(i, col));
break;
case "diagonal":
changeColor(findCell(i, i));
break;
case "anti-diagonal":
changeColor(findCell(i, dimension - (i + 1)));
break;
}
}
}
function changeColor(cell){
cell.style.backgroundColor = '#b43030';
cell.style.color = 'white';
}
function renderSymbolInCell (symbol, row, col, color = '#333') {
const targetCell = findCell(row, col);
targetCell.textContent = symbol;
targetCell.style.color = color;
}
function findCell (row, col) {
const targetRow = container.querySelectorAll('tr')[row];
return targetRow.querySelectorAll('td')[col];
}
function addResetListener () {
const resetButton = document.getElementById('reset');
resetButton.addEventListener('click', resetClickHandler);
}
function resetClickHandler () {
console.log('reset!');
startGame();
}
/* Test Function */
/* Победа первого игрока */
function testWin () {
clickOnCell(0, 2);
clickOnCell(0, 0);
clickOnCell(2, 0);
clickOnCell(1, 1);
clickOnCell(2, 2);
clickOnCell(1, 2);
clickOnCell(2, 1);
}
/* Ничья */
function testDraw () {
clickOnCell(2, 0);
clickOnCell(1, 0);
clickOnCell(1, 1);
clickOnCell(0, 0);
clickOnCell(1, 2);
clickOnCell(1, 2);
clickOnCell(0, 2);
clickOnCell(0, 1);
clickOnCell(2, 1);
clickOnCell(2, 2);
}
function clickOnCell (row, col) {
findCell(row, col).click();
}