-
Notifications
You must be signed in to change notification settings - Fork 4
/
game.js
118 lines (108 loc) · 2.72 KB
/
game.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
import { Column } from "./column.js";
import { ColumnWinInspector } from "./column-win.js";
import { RowWinInspector } from "./row-win.js";
import { DiagonalWinInspector } from "./diagonal-win.js";
//main
export class Game {
constructor(player1, player2) {
this.player1 = player1;
this.player2 = player2;
this.winnerNumber = 0;
this.column = [
new Column(),
new Column(),
new Column(),
new Column(),
new Column(),
new Column(),
new Column(),
];
}
isColumnFull(index) {
return this.column[index].isFull();
}
getName() {
if (this.winnerNumber === 1) {
return `${this.player1} WINS!!`;
} else if (this.winnerNumber === 2) {
return `${this.player2} WINS!!!`;
} else if (this.winnerNumber === 3) {
return `${this.player1} ties with ${this.player2}`;
}
return `${this.player1} vs ${this.player2}`;
}
getTokenAt(indexI, indexJ) {
if (this.column[indexI].getTokenAt(indexJ) === 1) {
return 1;
} else if (this.column[indexI].getTokenAt(indexJ) === 2) {
return 2;
} else {
return null;
}
}
checkForTie() {
if (this.winnerNumber === 0) {
return this.column.every(function (ele) {
return ele.isFull();
});
}
}
checkForColumnWin() {
if (this.winnerNumber === 0) {
for (let i = 0; i < this.column.length; i++) {
let something = new ColumnWinInspector(this.column[i]);
if (something.inspect() !== 0) {
this.winnerNumber = something.inspect();
}
}
}
}
checkForDiagonalWin() {
if (this.winnerNumber === 0) {
for (let i = 0; i <= 3; i++) {
let something = new DiagonalWinInspector([
this.column[i],
this.column[i + 1],
this.column[i + 2],
this.column[i + 3],
]);
if (something.inspect() !== 0) {
this.winnerNumber = something.inspect();
}
}
}
}
checkForRowWin() {
if (this.winnerNumber === 0) {
for (let i = 0; i <= 3; i++) {
let something = new RowWinInspector([
this.column[i],
this.column[i + 1],
this.column[i + 2],
this.column[i + 3],
]);
if (something.inspect() !== 0) {
this.winnerNumber = something.inspect();
}
}
}
}
}
export class CurrentPlayer extends Game {
constructor(player1, player2) {
super(player1, player2);
this.currentPlayer = 1;
}
playInColumn() {
if (this.currentPlayer === 1) {
this.currentPlayer = 2;
// console.log(this.currentPlayer)
} else {
this.currentPlayer = 1;
// console.log(this.currentPlayer)
}
}
getTurn() {
return this.currentPlayer;
}
}