-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhra.js
178 lines (154 loc) · 3.47 KB
/
hra.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
'use strict';
let player = 'circle';
const gridSquare = document.querySelectorAll('.grid__pole');
const boardSize = 10;
const symbolsToWin = 5;
const NowPlay = ({ target }) => {
if (
!target.classList.contains('grid__pole-circle') &&
!target.classList.contains('grid__pole-cross')
) {
target.classList.add(`grid__pole-${player}`);
target.disabled = true;
const SymbolElm = document.querySelector('.symbol');
if (player === 'circle') {
player = 'cross';
SymbolElm.src = 'img/cross.svg';
SymbolElm.alt = 'křížek';
} else {
player = 'circle';
SymbolElm.src = 'img/circle.svg';
SymbolElm.alt = 'kolečko';
}
if (isWinningMove(target) && player === 'cross') {
if (confirm(`Vyhrálo kolečko. Spustit další hru?`)) {
location.reload();
}
} else if (isWinningMove(target) && player === 'circle') {
if (confirm(`Vyhrál křížek. Spustit další hru?`)) {
location.reload();
}
}
}
};
const getSymbol = (field) => {
if (field.classList.contains('grid__pole-cross')) {
return 'cross';
} else if (field.classList.contains('grid__pole-circle')) {
return 'circle';
}
};
const getField = (row, column) => {
return gridSquare[row * boardSize + column];
};
const getPosition = (field) => {
let fieldIndex = 0;
while (fieldIndex < gridSquare.length && field !== gridSquare[fieldIndex]) {
fieldIndex++;
}
return {
row: Math.floor(fieldIndex / boardSize),
column: fieldIndex % boardSize,
};
};
const isWinningMove = (field) => {
const origin = getPosition(field);
const symbol = getSymbol(field);
let i;
let inRow = 1;
// Koukni doleva
i = origin.column;
while (i > 0 && symbol === getSymbol(getField(origin.row, i - 1))) {
inRow++;
i--;
}
// Koukni doprava
i = origin.column;
while (
i < boardSize - 1 &&
symbol === getSymbol(getField(origin.row, i + 1))
) {
inRow++;
i++;
}
if (inRow >= symbolsToWin) {
return true;
}
let inColumn = 1;
// Koukni nahoru
i = origin.row;
while (i > 0 && symbol === getSymbol(getField(i - 1, origin.column))) {
inColumn++;
i--;
}
// Koukni dolu
i = origin.row;
while (
i < boardSize - 1 &&
symbol === getSymbol(getField(i + 1, origin.column))
) {
inColumn++;
i++;
}
if (inColumn >= symbolsToWin) {
return true;
}
// Diagonály
let y;
let inDiagonalA = 1;
// Koukni nahoru vpravo
i = origin.row;
y = origin.column;
while (
i > 0 &&
y < boardSize - 1 &&
symbol === getSymbol(getField(i - 1, y + 1))
) {
inDiagonalA++;
i--;
y++;
}
// Koukni dolu vlevo
i = origin.row;
y = origin.column;
while (
i < boardSize - 1 &&
y > 0 &&
symbol === getSymbol(getField(i + 1, y - 1))
) {
inDiagonalA++;
i++;
y--;
}
if (inDiagonalA >= symbolsToWin) {
return true;
}
let inDiagonalB = 1;
// Koukni nahoru vlevo
i = origin.row;
y = origin.column;
while (i > 0 && y > 0 && symbol === getSymbol(getField(i - 1, y - 1))) {
inDiagonalB++;
i--;
y--;
}
// Koukni dolu vpravo
i = origin.row;
y = origin.column;
while (
i < boardSize - 1 &&
y < boardSize - 1 &&
symbol === getSymbol(getField(i + 1, y + 1))
) {
inDiagonalB++;
i++;
y++;
}
if (inDiagonalB >= symbolsToWin) {
return true;
}
return false;
};
for (let i = 0; i < gridSquare.length; i++) {
gridSquare[i].addEventListener('click', NowPlay);
}