-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
135 lines (117 loc) · 3.85 KB
/
script.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
129
130
131
132
133
134
135
let gameController = (function() {
const winning = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
const board = [];
for (let i = 0; i < 9; i++) {
board[i] = "";
};
const players = [
{
name: "Player One",
token: "X"
},
{
name: "Player Two",
token: "O"
}
];
let activePlayer = players[0];
function switchPlayers() {
if (activePlayer == players[0]) {
activePlayer = players[1];
}
else {
activePlayer = players[0];
}
document.querySelector('#turn-text').textContent = `It is ${activePlayer.token}'s turn.`;
};
function makeMove(tileSelection) {
if (board[tileSelection] == "") {
board[tileSelection] = activePlayer.token;
document.getElementById(tileSelection).textContent = activePlayer.token;
return (true);
}
else {
return (false);
}
}
function checkIfWinning() {
for (let i = 0; i < winning.length; i++){
if (board[winning[i][0]] == ""){
continue;
}
if (board[winning[i][0]] == board[winning[i][1]] && board[winning[i][1]] == board[winning[i][2]]){
return(true);
}
}
return(false);
};
function checkIfFull() {
for (let i = 0; i < board.length; i++){
if (board[i] == "") {
return (false);
} else {
continue;
}
}
return(true);
}
function receiveInput(e) {
const tileInput = Number(e.target.id);
gameLoop(tileInput);
};
function gameLoop(tileSelection) {
if (!makeMove(tileSelection)) {
document.querySelector('#turn-text').textContent = `Illegal Move. It is ${activePlayer.token}'s turn.`;
return(true);
}
if (checkIfWinning()) {
document.querySelector('#turn-text').textContent = `${activePlayer.token} Wins!!`;
return (true);
}
if (checkIfFull()) {
document.querySelector('#turn-text').textContent = `Tie Game.`;
return (true);
}
switchPlayers();
}
function restartGame() {
for (let i = 0; i < 9; i++) {
board[i] = "";
};
activePlayer = players[0];
document.querySelectorAll(`[class^="board-tile"]`).forEach(tile => tile.textContent = ``);
document.querySelector('#turn-text').textContent = `It is ${activePlayer.token}'s turn.`;
}
return {receiveInput, restartGame}
})();
const gameBoard = (function createGameBoard () {
const gameBoardContainer = document.querySelector("#game-board");
const turnDisplay = document.querySelector("#turn-display");
const newGame = document.querySelector("#new-game");
for (let i = 0; i < 9; i++) {
const boardTile = document.createElement('div');
boardTile.setAttribute('class', `board-tile-${i}`);
boardTile.setAttribute('id', `${i}`);
boardTile.textContent = ``;
gameBoardContainer.appendChild(boardTile);
}
const turn = document.createElement('p');
turn.setAttribute('id', 'turn-text');
turn.textContent = `It is X's turn.`;
turnDisplay.appendChild(turn);
const restartGame = document.createElement('button');
restartGame.setAttribute('id', 'restart-game-button');
restartGame.textContent = "New Game";
newGame.appendChild(restartGame);
})();
document.querySelectorAll(`[class^="board-tile"]`).forEach(tile => tile.addEventListener('click', gameController.receiveInput));
document.querySelector('#restart-game-button').addEventListener('click', gameController.restartGame);