-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
128 lines (100 loc) · 3.46 KB
/
index.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
let xIsNext = true;
let nextPlayer;
let player;
let nextPerson;
let gameOver = false;
function isNext(elementId) {
if (xIsNext == true) {
player = 'X';
} else {
player = 'O';
}
if (player == 'X') {
nextPerson = 'O';
} else {
nextPerson = 'X';
}
if (document.getElementById("single").checked) {
calculateWinner(player);
nextUp(player, elementId);
let positionArray = [];
let computerPlayer;
for(i = 0; i < 9; i++) {
if (document.getElementById(`position${i}`).textContent === "") {
positionArray.push(i) ;
}
}
if (player == 'X') {
computerPlayer = 'O';
nextPerson = 'X';
} else {
computerPlayer = 'X';
nextPerson = '0';
}
randomPosition = positionArray[Math.floor(Math.random() * positionArray.length)]
if (gameOver === false) {
document.getElementById("next").textContent = nextPerson + ' is next!'
nextUp(computerPlayer, `position${randomPosition}`);
} else {
console.log(gameOver);
return;
}
} else if (document.getElementById("double").checked) {
document.getElementById("next").textContent = nextPerson + ' is next!'
return nextUp(player, elementId);
}
return;
}
function nextUp(player, elementId) {
document.getElementById(elementId).textContent = player;
document.getElementById(elementId).disabled = true;
gameOver = false;
calculateWinner(player);
return xIsNext = !xIsNext;
}
function calculateWinner(player) {
box0 = document.getElementById("position0").textContent
box1 = document.getElementById("position1").textContent
box2 = document.getElementById("position2").textContent
box3 = document.getElementById("position3").textContent
box4 = document.getElementById("position4").textContent
box5 = document.getElementById("position5").textContent
box6 = document.getElementById("position6").textContent
box7 = document.getElementById("position7").textContent
box8 = document.getElementById("position8").textContent
winner = [
[box0, box1, box2],
[box3, box4, box5],
[box6, box7, box8],
[box0, box4, box8],
[box0, box3, box6],
[box1, box4, box7],
[box2, box5, box8],
[box2, box4, box6]
]
catsGame = [ box0, box1, box2, box3, box4, box5, box6, box7, box8]
for (i = 0; i < winner.length; i++) {
if (winner[i][0] != "" && winner[i][0] === winner[i][1] && winner[i][1] === winner[i][2] && winner[i][0] === winner[i][2] ) {
document.getElementById("next").textContent = player +" wins!";
console.log(`hi ${player}`);
for(y = 0; y < 9; y++) {
document.getElementById(`position${y}`).disabled = true;
}
gameOver = true;
break;
} else if (
!catsGame.includes("") && ((winner[i][0] === winner[i][1] && winner[i][1] === winner[i][2] && winner[i][0] === winner[i][2] ) != true)) {
document.getElementById("next").textContent = "Cat's game...how could you do such a thing?!";
gameOver = true;
}
}
}
function reset() {
for(i = 0; i < 9; i++) {
document.getElementById(`position${i}`).textContent = '';
document.getElementById(`position${i}`).disabled = false;
}
document.getElementById('next').textContent = 'Welcome to Tic Tac Toe you fool! X is first. Do your best!';
xIsNext = true;
return;
}