-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWinnerCheck.js
More file actions
189 lines (165 loc) · 8.1 KB
/
WinnerCheck.js
File metadata and controls
189 lines (165 loc) · 8.1 KB
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
import getCoordinates from './getCoordinates.js'
export class WinnerCheck {
constructor (table) {
this.table = table
}
checkWinHorizontally = (buttonsCoordinates, whosTurn, winCondition) => {
let leftCount = 0
// countss the player's move to the left of the button that was placed.
let rightCount = 0
// countss the player's move to the right of the button that was placed.
const [row, column] = getCoordinates(buttonsCoordinates)
// loop that moves lelt as long as innerText is === whosTurn,
// And for everone one add 1 to leftCounter
// setting up the starting values for the column index and colrdinate element
let columnIndex = column
let coordinateElement = this.table.querySelector(`#Row\\:${row}-Column\\:${--columnIndex}`)
while (coordinateElement) {
if (coordinateElement.innerText === whosTurn)
leftCount++
else
break
coordinateElement = this.table.querySelector(`#Row\\:${row}-Column\\:${--columnIndex}`)
}
// loop that moves right as long as innerText is === whosTurn,
// And for every one that match add 1 to rightCounter
// Resetting column index and colrdinate element back to there starting values
columnIndex = column
coordinateElement = this.table.querySelector(`#Row\\:${row}-Column\\:${++columnIndex}`)
while (coordinateElement) {
if (coordinateElement.innerText === whosTurn)
rightCount++
else
break
coordinateElement = this.table.querySelector(`#Row\\:${row}-Column\\:${++columnIndex}`)
}
//Then we we add them up. leftCounter and rightCounter + 1 for the one the player just put down.
// Return true if counter is = or larger then winCondition
if ((leftCount + 1 + rightCount) >= winCondition) {
return true
}
else return false
}
checkWinVertically = (buttonsCoordinates, whosTurn, winCondition) => {
let upwordsCount = 0
let downwordsCount = 0
const [row, column] = getCoordinates(buttonsCoordinates)
// loop that moves lelt as long as innerText is === whosTurn,
// And for everone one add 1 to leftCounter
// setting up the starting values for the column index and colrdinate element
let rowIndex = row
let coordinateElement = this.table.querySelector(`#Row\\:${--rowIndex}-Column\\:${column}`)
// Counts player's moves above where the player just played.
while (coordinateElement) {
if (coordinateElement.innerText === whosTurn)
upwordsCount++
else
break
coordinateElement = this.table.querySelector(`#Row\\:${--rowIndex}-Column\\:${column}`)
}
// loop that moves that are below as long as innerText is === whosTurn,
// And for every one that match add 1 to downwordsCounter
// Resetting rows index and colrdinate element back to there starting values
rowIndex = row
coordinateElement = this.table.querySelector(`#Row\\:${++rowIndex}-Column\\:${column}`)
while (coordinateElement) {
if (coordinateElement.innerText === whosTurn)
downwordsCount++
else
break
coordinateElement = this.table.querySelector(`#Row\\:${++rowIndex}-Column\\:${column}`)
}
//Then we we add them up. leftCounter and rightCounter + 1 for the one the player just put down.
// Return true if counter is = or larger then winCondition
if ((upwordsCount + 1 + downwordsCount) >= winCondition) {
return true
}
else return false
}
checkwinDiagonallyToTheLeft = (buttonsCoordinate, whosTurn, winCondition) => {
// We are going to count left first then right.
let upwordsCount = 0
let downwordsCount = 0
const [row, column] = getCoordinates(buttonsCoordinate)
// loop through moves as long as innerText is === whosTurn,
// And for everone one add 1 to the appropriate Counter
// setting up the starting values for the column index and colrdinate element
let rowIndex = row
let columnIndex = column
let coordinateElement = this.table.querySelector(`#Row\\:${--rowIndex}-Column\\:${--columnIndex}`)
// Counts player's moves above/left where the player just played.
while (coordinateElement) {
if (coordinateElement.innerText === whosTurn)
upwordsCount++
else
break
coordinateElement = this.table.querySelector(`#Row\\:${--rowIndex}-Column\\:${--columnIndex}`)
}
// reset the starting values for the loop.
rowIndex = row
columnIndex = column
coordinateElement = this.table.querySelector(`#Row\\:${++rowIndex}-Column\\:${++columnIndex}`)
// Counts player's moves lower/right where the player just played.
while (coordinateElement) {
if (coordinateElement.innerText === whosTurn)
downwordsCount++
else
break
coordinateElement = this.table.querySelector(`#Row\\:${++rowIndex}-Column\\:${++columnIndex}`)
}
//Then we we add them up + 1 (for the one the player just put down).
if ((upwordsCount + 1 + downwordsCount) >= winCondition) {
return true
}
return false
}
checkwinDiagonallyToTheRight = (buttonsCoordinate, whosTurn, winCondition) => {
// We are going to count left first then right.
let upwordsCount = 0
let downwordsCount = 0
const [row, column] = getCoordinates(buttonsCoordinate)
// loop through moves as long as innerText is === whosTurn,
// And for everone one add 1 to the appropriate Counter
// setting up the starting values for the column index and colrdinate element
let rowIndex = row
let columnIndex = column
let coordinateElement = this.table.querySelector(`#Row\\:${--rowIndex}-Column\\:${++columnIndex}`)
// Counts player's moves above/left where the player just played.
while (coordinateElement) {
if (coordinateElement.innerText === whosTurn)
upwordsCount++
else
break
coordinateElement = this.table.querySelector(`#Row\\:${--rowIndex}-Column\\:${++columnIndex}`)
}
// reset the starting values for the loop.
rowIndex = row
columnIndex = column
coordinateElement = this.table.querySelector(`#Row\\:${++rowIndex}-Column\\:${--columnIndex}`)
// Counts player's moves lower/right where the player just played.
while (coordinateElement) {
if (coordinateElement.innerText === whosTurn)
downwordsCount++
else
break
coordinateElement = this.table.querySelector(`#Row\\:${++rowIndex}-Column\\:${--columnIndex}`)
}
//Then we we add them up + 1 (for the one the player just put down).
if ((upwordsCount + 1 + downwordsCount) >= winCondition) {
return true
}
return false
}
checkwinDiagonally = (buttonsCoordinate, whosTurn, winCondition) => {
// check win diagonally to the right and if not a win do so for the other side. return false if no win.
return this.checkwinDiagonallyToTheLeft(buttonsCoordinate, whosTurn, winCondition) || this.checkwinDiagonallyToTheRight(buttonsCoordinate, whosTurn, winCondition)
}
check = (buttonsCoordinate, whosTurn, winCondition) => {
if (this.checkWinHorizontally(buttonsCoordinate, whosTurn, winCondition))
return true
else if (this.checkWinVertically(buttonsCoordinate, whosTurn, winCondition))
return true
else if (this.checkwinDiagonally(buttonsCoordinate, whosTurn, winCondition))
return true
}
}