-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
140 lines (121 loc) · 4.85 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
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
136
137
138
139
140
/******* START: STATE *******/
const gameState = {
player1Turn: true,
p1Boxes: [],
p2Boxes: [],
totalTurns: 0,
winner: false
}
/******* START: STATE *******/
/******* START: RENDER FUNCTIONS *******/
function renderMain() {
const docBody = document.getElementById('body');
const bodyTemplate = `<header>
<h1>Tic Tac Toe!</h1>
</header>
<main id='main'>
<section id='game-board'></section>
<section id='outcome'>Player X Goes First!<br>Click a square!</section>
<section id='restart-box' onclick='resetGame()'><h2>Click to Restart</h2></section>
</main>
<footer>
<div>© Emily Smith 2021</div>
<div>
<a href='https://github.com/emilydrakesmith/GA-tic-tac-toe' target='_blank' class='footer-link'>GitHub Repo</a> •
<a href='https://emilysmith.tech/' target='_blank' class='footer-link'>Developer Webpage</a>
</div>
</footer>`;
docBody.innerHTML = bodyTemplate;
}
function renderGameDivs() {
const gameBoard = document.getElementById('game-board');
for (let i=0; i<9; i++) {
const gameBoxTemplate = `<div class='game-box unclaimed-box' id=${i+1} onclick='handleClick(event)'></div>`;
gameBoard.insertAdjacentHTML('beforeend', gameBoxTemplate);
}
}
/******* END: RENDER FUNCTIONS *******/
/******* START: EVENT LISTENERS *******/
function handleClick(event) {
const clickedBox = event.currentTarget;
const clickedBoxId = parseInt(clickedBox.getAttribute('id'));
const currentPlayer = gameState.player1Turn === true ? 'player1' : 'player2';
const outcomeBox = document.getElementById('outcome');
processTurn(currentPlayer, clickedBox, clickedBoxId, outcomeBox);
checkEndGame(currentPlayer);
}
function resetGame() {
gameState.player1Turn = true;
gameState.p1Boxes = [];
gameState.p2Boxes = [];
gameState.totalTurns = 0;
gameState.winner = false;
initialize();
}
/******* END: EVENT LISTENERS *******/
/******* START: STRUCTURAL FUNCTIONS *******/
function processTurn(player, clickedBox, clickedBoxId, outcomeBox) {
gameState.totalTurns ++;
clickedBox.onclick = null;
if (player === 'player1') processPlayerOneMove(clickedBox, clickedBoxId, outcomeBox);
if (player === 'player2') processPlayerTwoMove(clickedBox, clickedBoxId, outcomeBox);
}
function processPlayerOneMove(box, boxId, resultsBox) {
box.innerHTML = 'X';
box.classList.replace('unclaimed-box', 'p1-box');
gameState.p1Boxes.push(boxId);
resultsBox.innerHTML = 'Player O Goes!';
gameState.player1Turn = false;
}
function processPlayerTwoMove(box, boxId, resultsBox) {
box.innerHTML = 'O';
box.classList.replace('unclaimed-box', 'p2-box');
gameState.p2Boxes.push(boxId);
resultsBox.innerHTML = 'Player X Goes!';
gameState.player1Turn = true;
}
function checkEndGame(currentPlayer) {
if (gameState.totalTurns >= 5) checkForWin(currentPlayer);
if (gameState.totalTurns === 9 && gameState.winner === false) declareStalemate();
}
function checkForWin(player) {
const winConditions = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 4, 7],
[2, 5, 8],
[3, 6, 9],
[1, 5, 9],
[3, 5, 7]
];
const playerBoxes = player === 'player1' ? gameState.p1Boxes : gameState.p2Boxes;
winConditions.forEach(winCondition => {
if (playerBoxes.includes(winCondition[0])
&& playerBoxes.includes(winCondition[1])
&& playerBoxes.includes(winCondition[2])) {
declareWinner(player, winCondition);
}
})
}
function declareWinner(player, winArray) {
gameState.winner = true;
const messageBox = document.getElementById('outcome');
winArray.forEach(boxId => document.getElementById(boxId).classList.add('win-background'));
const p1WinOutput = `<h2 class='outcome-text'>X Wins!</h2>`;
const p2WinOutput = `<h2 class='outcome-text'>O Wins!</h2>`;
messageBox.innerHTML = (player === 'player1') ? p1WinOutput : p2WinOutput;
document.getElementById('game-board').setAttribute('style', 'pointer-events:none');
}
function declareStalemate() {
document.getElementById('outcome').innerHTML = `<h2 class='outcome-text'>It's a stalemate!</h2>`;
document.getElementById('game-board').setAttribute('style', 'pointer-events:none');
}
/******* END: STRUCTURAL FUNCTIONS *******/
/******* START: INITIALIZE FUNCTION *******/
function initialize() {
renderMain();
renderGameDivs();
}
/******* END: INITIALIZE FUNCTION *******/
initialize();