-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapplication.js
89 lines (78 loc) · 2.8 KB
/
application.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
var Game = {
state: ['', '', '', '', '', '', '', '', '', ''],
turn: 'player',
makeMove: (position) => {
if(Game.state[position] != '') {
error.innerHTML = `AI attempted to perform an invalid action. makeMove(${position})`
}
Game.state[position] = 'O'
document.getElementById(position).innerHTML = 'O'
if(check()) {
return
}
Game.turn = 'player'
}
}
var boxes = document.getElementsByTagName('TD');
var error = document.getElementById('error')
var statusMessage = document.getElementById('status')
var box1 = document.getElementById('0')
var box2 = document.getElementById('1')
var box3 = document.getElementById('2')
var box4 = document.getElementById('3')
var box5 = document.getElementById('4')
var box6 = document.getElementById('5')
var box7 = document.getElementById('6')
var box8 = document.getElementById('7')
var box9 = document.getElementById('8')
for(var i = 0; i < boxes.length; i++) {
boxes.item(i).addEventListener('click', performMove, false);
}
function performMove() {
if(this.innerHTML != '') {
error.innerHTML = 'Invalid selection'
} else {
if(Game.turn != 'player') {
error.innerHTML = 'Not your turn yet...'
return
}
error.innerHTML = ''
this.innerHTML = 'X'
Game.state[this.id] = 'X'
if(check()) {
return
}
Game.turn = 'ai'
AI.logic(Game)
}
}
function check() {
if(
(box1.innerHTML != '' && box1.innerHTML == box2.innerHTML && box1.innerHTML == box3.innerHTML)
|| (box4.innerHTML != '' && box4.innerHTML == box5.innerHTML && box4.innerHTML == box6.innerHTML)
|| (box7.innerHTML != '' && box7.innerHTML == box8.innerHTML && box7.innerHTML == box9.innerHTML)
|| (box1.innerHTML != '' && box1.innerHTML == box4.innerHTML && box1.innerHTML == box7.innerHTML)
|| (box2.innerHTML != '' && box2.innerHTML == box5.innerHTML && box2.innerHTML == box8.innerHTML)
|| (box3.innerHTML != '' && box3.innerHTML == box6.innerHTML && box3.innerHTML == box9.innerHTML)
|| (box7.innerHTML != '' && box7.innerHTML == box5.innerHTML && box7.innerHTML == box3.innerHTML)
|| (box9.innerHTML != '' && box9.innerHTML == box5.innerHTML && box9.innerHTML == box1.innerHTML)
) {
statusMessage.innerHTML = `${Game.turn} wins`
return true
}
if(
box1.innerHTML != ''
&& box2.innerHTML != ''
&& box3.innerHTML != ''
&& box4.innerHTML != ''
&& box5.innerHTML != ''
&& box6.innerHTML != ''
&& box7.innerHTML != ''
&& box8.innerHTML != ''
&& box9.innerHTML != ''
) {
statusMessage.innerHTML = 'Cats game.'
return true
}
return false
}