-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
85 lines (70 loc) · 2.25 KB
/
scripts.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
let gameBoard = [];
let player = 1;
let score = [0, 0];
let tie = false;
function startGame(){
tie = false;
gameBoard = [0, 0, 0, 0, 0, 0, 0, 0, 0]
document.querySelectorAll('.game-tile').forEach(tile => {
tile.textContent = '';
});
document.getElementById("win-container").style.display = "none";
}
function gameLogic(tileNumber){
let tile = document.querySelectorAll('.game-tile')[tileNumber - 1];
if(gameBoard[tileNumber - 1] === 0){
gameBoard[tileNumber - 1] = player;
let playerDiv = document.createElement('div');
if(player === 1){
playerDiv.className = 'player-one';
}else{
playerDiv.className = 'player-two';
}
tile.appendChild(playerDiv);
if(winCheck() === true){
winScreen();
}
player = (player === 1 ? 2 : 1);
}
}
function winCheck(){
// Horizontal and vertical
for (let i = 0; i < 3; i++) {
if (gameBoard[i * 3] !== 0 && gameBoard[i * 3] === gameBoard[i * 3 + 1] && gameBoard[i * 3] === gameBoard[i * 3 + 2]) {
return true;
}
if (gameBoard[i] !== 0 && gameBoard[i] === gameBoard[i + 3] && gameBoard[i] === gameBoard[i + 6]) {
return true;
}
}
// Top-left to bottom-right diagonal
if (gameBoard[0] !== 0 && gameBoard[0] === gameBoard[4] && gameBoard[0] === gameBoard[8]) {
return true;
}
// Top-right to bottom-left diagonal
if (gameBoard[2] !== 0 && gameBoard[2] === gameBoard[4] && gameBoard[2] === gameBoard[6]) {
return true;
}
// Check for tie
if (!gameBoard.includes(0)) {
tie = true;
winScreen()
}
}
function winScreen(){
if(tie === true) {
winScreenText("It's a tie!")
}else{
score[player - 1]++;
if (player === 1) {
document.getElementById('player-one-score').innerText = `Score: ${score[0]}`;
} else {
document.getElementById('player-two-score').innerText = `Score: ${score[1]}`;
}
winScreenText("Player " + player + " is the winner!");
}
document.getElementById("win-container").style.display = "block";
}
function winScreenText(str){
document.getElementById('win-text').innerText = str;
}