-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.js
115 lines (96 loc) · 2.31 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
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
// var main = function() {
var xo = ["X", "O"];
var i = 0;
var x = [];
var o = [];
var winCombo = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]];
var won = false;
var allBox;
var catsGame = false;
function changeColor(id) {
box = document.getElementById(id);
var boxes = document.getElementsByClassName("box");
//For the X
if(i == 0 && box.innerHTML == "" && won == false){
box.style.backgroundColor = "#A66D60";
box.style.color = "#260F0A"
box.innerHTML = xo[i];
x.push(id);
won = winCheck(x);
if(won){
alert("Player X won the game!");
}
//No winner
if(won == false && x.length == 5){
catsGame = true;
alert("The cat won the game!")
}
i = 1;
//change hover color
for( var boxInc = 0; boxInc < boxes.length; boxInc++){
boxes[boxInc].classList.remove("x-hov");
boxes[boxInc].classList.add("o-hov");
}
//For the O
}else if(i == 1 && box.innerHTML == "" && won == false){
box.style.backgroundColor = "#0D0603";
box.style.color = "#EBEFF2"
box.innerHTML = xo[i];
o.push(id);
won = winCheck(o);
if(won){
alert("Player O won the game!");
}
i = 0;
//change hover color
for( var boxInc = 0; boxInc < boxes.length; boxInc++){
boxes[boxInc].classList.remove("o-hov");
boxes[boxInc].classList.add("x-hov");
}
//reset everything!
}else if(won == true || catsGame == true){
reset();
}
}
function winCheck(player){
var index = -1;
var bingo = [0,0,0,0,0,0,0,0];
for( var outer = 0; outer < winCombo.length; outer++) {
var winSet = winCombo[outer];
for( var inner = 0; inner < player.length; inner++) {
index = winSet.indexOf(player[inner])
if( index > -1) {
bingo[outer]++;
}
}
}
console.log("bingo: ",bingo);
if(bingo.indexOf(3) > -1) {
return true;
}else{
return false;
}
}
// $(".box").hover(function(){
// if( i == 0 ){
// $(".box:hover").css('background-color', 'green');
// }else{
// $(".box:hover").css('background-color', 'red');
// }}, function() {
// $(".box:hover").css('background-color', 'white');
// })
function reset(){
//Can I name this var i?
for(var b = 1; b < 10; b++) {
allBox = document.getElementById(b);
allBox.style.backgroundColor = "#EBEFF2";
allBox.innerHTML = "";
}
//initialize variables
i = 0;
x = [];
o = [];
won = false;
catsGame = false;
}
// $(document).ready(main());