-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
263 lines (225 loc) · 5.98 KB
/
main.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
const DEBUG = false;
/**
* Main game grid
*/
var grid = [];
/**
* Morpion game object
*/
var game = null;
function main(){
game = new Morpion();
}
function niam() {
//clear html tags
document.querySelector("body").removeChild(document.querySelector(".showScore"));
document.querySelector(".main").removeChild(document.querySelector("table"));
console.clear();
//reset game vars
game = null;
grid = [];
main();
}
/**
* Morpion game main class
*/
class Morpion{
currentPlayer = {player:true}//player one is true
constructor(){
console.log("[+]\tMorpion");
this.build(document.querySelector(".main"));
if(DEBUG) console.log(this.grid);
}
/**
* Create the game Grid
* and generate the grid Array
* @param {} anchor DOM element
*/
build(anchor) {
var table = document.createElement("table");
var tbody = document.createElement("tbody");
for(var i=0;i<3;i++){
var line = document.createElement("tr");
var arrayLine = [];//Cellule Array
for(var j=0;j<3;j++){
var cell = document.createElement("td");
// Cellule obj
var c = new Cellule(i, j);
//c.dom = cell;
c.constructor(cell, this.currentPlayer);
arrayLine.push(c);
cell.className = "zero";
var span = document.createElement("span");
span.className = "cell";
cell.appendChild(span);
line.appendChild(cell);
}
grid.push(arrayLine);//Grid append Cellule line
tbody.appendChild(line);
}
table.appendChild(tbody);
anchor.appendChild(table);
}
}
/**
*
* @param {*} lig
* @param {*} col
*/
function Cellule(lig, col){
this.lig = lig;
this.col = col;
this.currentPlayer;
/**
* 0 => Unclicked
* 1 => player one
* 2 => player two
*/
this.state = 0;
this.dom = null;
var that = this;
var click = function(){
if(DEBUG) console.log("Player clicked on ("+that.col+","+that.lig+")");
that.dom.removeEventListener("click", click);
if(that.currentPlayer.player){
that.cross();
that.state = 1;
}else{
that.round();
that.state = 2;
}
//change the current player pointer
that.currentPlayer.player = !that.currentPlayer.player;
victoryManager(grid);
}
this.constructor = function (dom, currentPlayer) {
this.currentPlayer = currentPlayer;
this.dom = dom;
this.dom.addEventListener("click", click);
}
this.cross = function () {
that.dom.className = "one";
}
this.round = function () {
that.dom.className = "two";
}
}
function victoryManager(grid) {
var winner = victory(grid);
switch(winner){
case -1:
console.log("Game still runing");
break;
case 0:
console.log("No Winner TIE");
showVictory("No winner tie");
break;
default :
console.log("Winner is "+ (winner==1?"cross":"round"));
showVictory("Winner is "+ (winner==1?"cross":"round"));
break;
}
}
/**
* Display the end game screen
* @param {*} winner 0 if tie 1 cross and 2 round
*/
function showVictory(winnerMsg) {
//TODO : display victory screen
/*
for(var i=0;i<3;i++){
for(var j=0;j<3;j++){
//remove action of each cell
//grid[i][j].dom.removeEventListener("click", grid[i][j].click);
}
}*/
const mainTag = document.querySelector("body");
var divShowScore = document.createElement("div");
divShowScore.className = "showScore";
var container = document.createElement("div");
var h2 = document.createElement("h2");
h2.textContent = winnerMsg;
var span = document.createElement("span");
span.textContent = "Play again";
span.addEventListener("click", niam);
container.appendChild(h2);
container.appendChild(span);
divShowScore.appendChild(container);
mainTag.appendChild(divShowScore);
}
/**
* Return the winner if there is one
* return 0 if equality, no winner
* return -1 if the game is running
* @param {*} grid
*/
function victory(grid) {
var winner = 0;
for(var i=0;i<3;i++){
winner = line(grid, i);
if( winner != 0 ) return winner;
winner = column(grid, i);
if( winner != 0 ) return winner;
}
winner = diagonals(grid);
if( winner != 0 ) return winner;
if(winner==0 && isGridFull(grid)) return 0;
return -1;
}
/**
* Return the player if the line
* contains all same player
* else return 0
* @param {*} grid
* @param {*} lig
*/
function line(grid, lig) {
for(var i=1;i<3;i++){
if(grid[lig][i-1].state != grid[lig][i].state) return 0;
}
return grid[lig][0].state;
}
/**
* Return the player if the column
* contains all same player
* else return 0
* @param {*} grid
* @param {*} col
*/
function column(grid, col) {
for(var i=1;i<3;i++){
if(grid[i-1][col].state != grid[i][col].state) return 0;
}
return grid[0][col].state;
}
/**
* Return the player if
* the diagonal contains all same sign
* else return 0
* @param {*} grid
* @returns 0 or player number (1 or 2)
*/
function diagonals(grid) {
var one = grid[0][0].state;
if(one != 0 && one == grid[1][1].state && one == grid[2][2].state) return one;
var two = grid[0][2].state;
if(two != 0 && two == grid[1][1].state && two == grid[2][0].state) return two;
return 0;
}
/**
* Return false if the grid is not full
* @param {*} grid
* @returns true if the grid does not contains 0
*/
function isGridFull(grid) {
for(var i=0;i<3;i++){
for(var j=0;j<3;j++){
if(grid[j][i].state==0) return false;
}
}
return true;
}
/**
* Launch the game
*/
main();