-
Notifications
You must be signed in to change notification settings - Fork 192
/
index.js
89 lines (72 loc) · 2.04 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
const CROSS = 'X';
const ZERO = 'O';
const EMPTY = ' ';
const container = document.getElementById('fieldWrapper');
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 cellClickHandler (row, col) {
// Пиши код тут
console.log(`Clicked on cell: ${row}, ${col}`);
/* Пользоваться методом для размещения символа в клетке так:
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();
}