-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
172 lines (139 loc) · 4.4 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
let gameState = {
players: ["x", "o"],
board: [
[null, null, null],
[null, null, null],
[null, null, null],
],
turn: {
count: 0,
value: " ",
},
fullBoard:[],
currentPlayer: true,
};
let {players, board, turn, currentPlayer } = gameState
const boardArray = Array.from(document.querySelectorAll('div.cell'))
const winningCombinations = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
]
let win
const title = document.getElementById('title');
const p1 = document.getElementsByName('p1');
const p2 = document.getElementsByName('p2');
const msg = document.getElementById('message');
title.addEventListener('click', function () {
let player1 = document.getElementById('p1').value;
let player2 = document.getElementById('p2').value;
if (p1.name || p2.name) {
msg.innerHTML = p1.name+ '<br />' + p2.name + ' : ' + p2.val;
}
});
const winMessage = document.getElementById('winMessage')
const winText = document.querySelector('[winText]')
const restartbtn = document.getElementById('restartButton')
restartbtn.addEventListener("click", buildInitialState)
const turnmessage = document.getElementById('gameMessage')
// state
let fullBoard = gameState.board.flat(9)
console.log(boardArray, board, gameState)
console.log(fullBoard)
function startingTurn(){ //Randomizes the starting player
turn.count = Math.round(Math.random());
if(turn.count === 1){
currentPlayer = !currentPlayer
}
return currentPlayer
}
function buildInitialState() {
fullBoard.fill(null)
winMessage.innerText = ''
win = false
startingTurn()
fullBoard.forEach( (element, idx) =>{
turnmessage.innerText= currentPlayer ? (`'It is ${player1}'s Turn!`) : `'It is ${player2}'s turn'`
boardArray[idx].innerText = element
console.log(boardArray)
})
boardArray.forEach(cell => {
cell.removeEventListener('click',onBoardClick)
cell.addEventListener('click', onBoardClick, {once:true})
})
renderState()
}
let player1 = 'X'
let player2 = 'O'
function isDraw() {
return [...boardArray].every(cell => {
if(cell){
return cell.innerText === ('x') || cell.innerText === ('o') ;
}
})
}
function winCheck(){
// let row1= boardArray.slice(0,2)
// let row2= boardArray.slice(3,5)
// let row3= boardArray.slice(6,8)
winningCombinations.forEach(function(row) {
let a = row[0], b = row[1], c = row[2];
console.log(a,b,c, boardArray[a].innerHTML,boardArray[b].innerHTML,boardArray[c].innerHTML)
if(boardArray[a].innerText!='' && boardArray[a].innerText==boardArray[b].innerText && boardArray[b].innerText==boardArray[c].innerText){
return win=true
}
});
}
function renderState() {
fullBoard.forEach((element, idx) => {
turnmessage.innerText= currentPlayer ? (`'It is ${player1}'s Turn!`) : `'It is ${player2}'s turn'`
if(element){
boardArray[idx].innerText = element
}})
}
renderState()
// maybe a dozen or so helper functions for tiny pieces of the interface
function addText(cell, currentPlayer){
cell.innerText = currentPlayer ? 'x' : 'o'
}
// function winCheck(){
// if (fullBoard.length = 9)
let current = currentPlayer ? 'X' : 'O'
function changeturn(){
currentPlayer = !currentPlayer
}
function onBoardClick() {
console.log('clicked', event.target)
let cellIndex = boardArray.findIndex((cell)=>{
return cell === event.target
})
addText(event.target, currentPlayer)
fullBoard[cellIndex] = currentPlayer ? 'x' : 'o'
// winCheck()
let current = currentPlayer ? 'X' : 'O'
let draw = isDraw()
let winner = winCheck()
if(draw){
winMessage.innerText ='It is a Tie!'
}
if(win){
boardArray.forEach((cell=>{
cell.removeEventListener('click', onBoardClick)
}))
winMessage.innerText= currentPlayer ? ` ${player1} Won!`:
`${player2} Won!`
}
changeturn()
console.log(winner)
renderState()
}
// update state, maybe with another dozen or so helper functions...
renderState() // show the user the new state
const gameboard = document.getElementById('board');
// gameboard.addEventListener('click', onBoardClick); //
buildInitialState()