-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.js
63 lines (63 loc) · 2.03 KB
/
tictactoe.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
module.exports.TicTacToe = class {
constructor(player1, player2) {
this.Player1 = [player1, "🇽"]
this.Player2 = [player2, "🇴"]
this.Table = [
[["⬜"], ["⬜"], ["⬜"]],
[["⬜"], ["⬜"], ["⬜"]],
[["⬜"], ["⬜"], ["⬜"]]
]
this.Available = 8
}
Move(x, y, player) {
var player2move = null
if (player == this.Player1[0]){player2move = this.Player1}else{player2move=this.Player2}
this.Table[x][y] = player2move[1]
this.Available -= 1
return true
}
CheckForWinner() {
let winner = null;
// Horisontaalne
for (let i = 0; i < 3; i++){
if (this.Table[i][0] == this.Table[i][1] && this.Table[i][1] == this.Table[i][2]){
if (this.Player1[1] == this.Table[i][0]){
winner = this.Player1[0]
}else{
winner = this.Player2[0]
}
break
}
}
//Vertikaalne
for (let i = 0; i < 3; i++){
if (this.Table[0][i] == this.Table[1][i] && this.Table[1][i] == this.Table[2][i]){
if (this.Player1[1] == this.Table[0][i]){
winner = this.Player1[0]
}else{
winner = this.Player2[0]
}
break
}
}
//Diagonaal
if (this.Table[0][0] == this.Table[1][1] && this.Table[1][1] == this.Table[2][2]){
if (this.Player1[1] == this.Table[0][0]){
winner = this.Player1[0]
}else{
winner = this.Player2[0]
}
}
if (this.Table[0][2] == this.Table[1][1] && this.Table[1][1] == this.Table[2][0]){
if (this.Player1[1] == this.Table[0][2]){
winner = this.Player1[0]
}else{
winner = this.Player2[0]
}
}
if (this.Available == 0){
winner == "Tie"
}
return winner
}
}