-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameView.js
51 lines (40 loc) · 1.6 KB
/
GameView.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
function GameView(){
this.displayBoard = displayBoard
function displayBoard(){
let boardElement = document.querySelector("#board")
boardElement.innerHTML = ""
game.board.forEach((tile) => {
let tileElement = document.createElement("div")
tileElement.classList.add("tile")
tileElement.setAttribute("horizontal", tile.horizontalPosition)
tileElement.setAttribute("vertical", tile.verticalPosition)
if(tile.flag === ""){
let buttonElement = document.createElement("button")
buttonElement.classList.add("action-button")
buttonElement.innerHTML = `Add ${game.flagTurn} flag to ${tile.verticalPosition} ${tile.horizontalPosition}`
buttonElement.addEventListener("click", () => {
tileClickEvent(tile)
})
tileElement.appendChild(buttonElement)
}
let flagElement = document.createElement("div")
flagElement.classList.add("tile-flag")
flagElement.innerHTML = tile.flag
tileElement.addEventListener("click", () => {
tileClickEvent(tile)
})
tileElement.appendChild(flagElement)
boardElement.appendChild(tileElement)
})
}
function tileClickEvent(tile){
game.playNextRound({
horizontal: tile.horizontalPosition,
vertical: tile.verticalPosition
})
displayBoard()
if(game.gameHasEnded()){
alert(`Game over!`)
}
}
}