forked from kontur-web-courses/tic-tac-toe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
160 lines (143 loc) · 4.74 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
const CROSS = 'X';
const ZERO = 'O';
const EMPTY = ' ';
const container = document.getElementById('fieldWrapper');
let field = new Array([EMPTY, EMPTY, EMPTY], [EMPTY,EMPTY,EMPTY], [EMPTY,EMPTY,EMPTY])
let player = 1;
let haveWinner = false;
let countEmptyCells = 9;
startGame();
addResetListener();
function startGame () {
renderGrid(3);
}
function renderGrid (dimension) {
container.innerHTML = '';
for (let i = 0; i < dimension; i++) {
const row = document.createElement('tr');
for (let j = 0; j < dimension; j++) {
const cell = document.createElement('td');
cell.textContent = EMPTY;
cell.addEventListener('click', () => cellClickHandler(i, j));
row.appendChild(cell);
}
container.appendChild(row);
}
}
function isThereWinner(){
if (!haveWinner && countEmptyCells === 0) {
alert("Победила дружба");
} else {
for (let i = 0; i < field.length; i++) {
if (field[0][i] !== EMPTY && field[0][i] === field[1][i] && field[1][i] === field[2][i]) {
haveWinner = true;
renderSymbolInCell(field[0][i], 0, i, '#FF0000');
renderSymbolInCell(field[0][i], 1, i, '#FF0000');
renderSymbolInCell(field[0][i], 2, i, '#FF0000');
if (field[0][i] == 'X') {
alert('Выиграл cross!');
} else {
alert('Выиграл zero!');
}
}
if (field[i][0] !== EMPTY && field[i][0] === field[i][1] && field[i][1] === field[i][2]) {
haveWinner = true;
renderSymbolInCell(field[i][0], i, 0, '#FF0000');
renderSymbolInCell(field[i][0], i, 1, '#FF0000');
renderSymbolInCell(field[i][0], i, 2, '#FF0000');
if (field[i][0] == 'X') {
alert('Выиграл cross!');
} else {
alert('Выиграл zero!');
}
}
}
if (field[0][0] !== EMPTY && field[0][0] === field[1][1] && field[1][1] === field[2][2]) {
haveWinner = true;
renderSymbolInCell(field[0][0], 0, 0, '#FF0000');
renderSymbolInCell(field[1][1], 1, 1, '#FF0000');
renderSymbolInCell(field[2][2], 2, 2, '#FF0000');
if (field[0][0] == 'X') {
alert('Выиграл cross!');
} else {
alert('Выиграл zero!');
}
}
}
if(field[0][2] !== EMPTY && field[0][2] === field[1][1] && field[1][1] === field[2][0]){
haveWinner = true;
renderSymbolInCell(field[0][2], 0, 2, '#FF0000');
renderSymbolInCell(field[0][2], 1, 1, '#FF0000');
renderSymbolInCell(field[0][2], 2, 0, '#FF0000');
if(field[0][2] == 'X'){
alert('Выиграл cross!');
}
else{
alert('Выиграл zero!');
}
}
}
function cellClickHandler (row, col) {
// Пиши код тут
if (field[row][col] === EMPTY && !haveWinner)
{
countEmptyCells -= 1;
console.log(`Clicked on cell: ${row}, ${col}`);
if (player === 1) {
field[row][col] = 'X';
renderSymbolInCell(CROSS, row, col);
player = 2;
} else {
field[row][col] = 'O';
renderSymbolInCell(ZERO, row, col);
player = 1;
}
isThereWinner();
}
/* Пользоваться методом для размещения символа в клетке так:
renderSymbolInCell(ZERO, row, col);
*/
}
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!');
}
/* 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();
}