-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
128 lines (111 loc) · 2.84 KB
/
app.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
const boxes = Array.from(document.getElementsByClassName('box'));
const gameboard = document.getElementById("gameboard");
const playText = document.getElementById('playText');
const restartBtn = document.getElementById('restart');
const spaces =[];
const O_TEXT ="O";
const X_TEXT = "X";
let currentPlayer= O_TEXT;
console.log(boxes);
const drawBoard =() => {
boxes.forEach((box, index) =>
{
let styleString = '';
if(index<3)
{
styleString+= 'border-bottom: 3px solid var(--colour);';
}
if(index%3===0)
{
styleString += 'border-right: 3px solid var(--colour);';
}
if(index%3===2)
{
styleString += 'border-left: 3px solid var(--colour);';
}
if(index>=6)
{
styleString+= 'border-top: 3px solid var(--colour);';
}
box.style= styleString;
box.addEventListener('click',boxClicked);
});
};
function boxClicked(e)
{
const id = e.target.id;
console.log(id);
if(!spaces[id])
{
spaces[id] = currentPlayer;
e.target.innerText= currentPlayer;
if(playerHasWon(currentPlayer))
{
playText.innerHTML = `${currentPlayer} wins!`;
return;
}
currentPlayer=currentPlayer===O_TEXT? X_TEXT:O_TEXT;
}
}
const playerHasWon = () =>
{
if(spaces[1]===currentPlayer)
{
if(spaces[2]===currentPlayer && spaces[3]===currentPlayer)
{
console.log(`${currentPlayer} wins up top`)
return true;
}
if(spaces[4]===currentPlayer && spaces[7]===currentPlayer)
{
console.log(`${currentPlayer} wins through left`);
return true;
}
if(spaces[5]===currentPlayer && spaces[9]===currentPlayer)
{
console.log(`${currentPlayer} wins diagonally`);
return true;
}
}
else if(spaces[9]===currentPlayer)
{
if(spaces[8]===currentPlayer && spaces[7]===currentPlayer)
{
console.log(`${currentPlayer} wins down`)
return true;
}
if(spaces[3]===currentPlayer && spaces[6]===currentPlayer)
{
console.log(`${currentPlayer} wins through right`);
return true;
}
}
else if(spaces[5]===currentPlayer)
{
if(spaces[4]===currentPlayer && spaces[6]===currentPlayer||spaces[3]===currentPlayer && spaces[7]===currentPlayer)
{
console.log(`${currentPlayer} wins middle`)
return true;
}
if(spaces[2]===currentPlayer && spaces[8]===currentPlayer)
{
console.log(`${currentPlayer} wins middle up`);
return true;
}
}
};
drawBoard();
const restart = () => {
spaces.forEach((space, index)=>{
spaces[index]=null;
});
boxes.forEach(box =>{
box.innerText='';
});
playText.innerText= `Let's Play!`;
currentPlayer= O_TEXT;
};
restartBtn.addEventListener('click',restart);
restart();
year = document.getElementById('year');
year.innerHTML = new Date().getFullYear();