-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
78 lines (68 loc) · 1.61 KB
/
script.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
const gamescript = {
board: ['','','','','','','','',''],
simbols: {
options: ['O','X'],
turn_index: 0,
change: function(){
this.turn_index = ( this.turn_index === 0 ? 1:0 );
}
},
container_element: null,
gameover: false,
winning_sequences: [
[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[2,4,6]
],
init: function(container) {
this.container_element = container;
},
make_play: function(position) {
if (this.gameover) return false;
if (this.board[position] === ''){
this.board[position] = this.simbols.options[this.simbols.turn_index];
this.draw();
let winning_sequences_index = this.check_winning_sequences( this.simbols.options[this.simbols.turn_index] );
if (winning_sequences_index >= 0){
this.game_is_over();
} else {
this.simbols.change();
}
return true;
} else {
return false;
}
},
check_winning_sequences: function(simbol) {
for ( i in this.winning_sequences ) {
if (this.board[ this.winning_sequences[i][0] ] == simbol &&
this.board[ this.winning_sequences[i][1] ] == simbol &&
this.board[ this.winning_sequences[i][2] ] == simbol) {
console.log('winning sequences INDEX:' + i);
return i;
}
};
return -1;
},
game_is_over: function() {
this.gameover = true;
console.log('GAME OVER');
},
start: function() {
this.board.fill('');
this.draw();
this.gameover = false;
},
draw: function() {
let content = '';
for ( i in this.board ) {
content += '<div onclick="gamescript.make_play(' + i + ')">' + this.board[i] + '</div>';
};
this.container_element.innerHTML = content;
},
};