-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreasurehunt.js
110 lines (78 loc) · 2.42 KB
/
treasurehunt.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
// var boardSize = 7
var boardWidth = 12
var boardHeight = 7
var treasureRow
var treasureCell
var bombRow
var bombCell
var clickCounter
var triesAvailable = 30
var minutesPassed
var secondsPassed
var gameOver
var spacing = ""
reset()
function reset(){
treasureRow = Math.ceil(Math.random() * boardHeight)
treasureCell = Math.ceil(Math.random() * boardWidth)
bombRow = Math.ceil(Math.random() * boardHeight)
bombCell = Math.ceil(Math.random() * boardWidth)
if (treasureRow === bombRow && treasureCell === bombCell)
{reset()}
else{
clickCounter = -1
gameOver = false
minutesPassed = 0
secondsPassed = -1
updateTries()
updateTimeElapsed()
console.log("Treasure: " + treasureRow + " " + treasureCell)
console.log("Bomb: " + bombRow + " " + bombCell)
startTimer()
}
}
function clicked(row , cell){
var selection = document.getElementById(row).cells.namedItem(cell)
if( (clickCounter !== triesAvailable) && !gameOver){
if (row === ("row" + treasureRow) && cell === ("cell" + treasureCell)){
selection.innerHTML = "🍆"
setTimeout(gameOverEvent,50,0)
}else if (row === ("row" + bombRow) && cell === ("cell" + bombCell) ){
selection.innerHTML = "💣"
setTimeout(gameOverEvent,50,1)
}else {
if (selection.innerHTML === "🌴") { clickCounter-- }
else { selection.innerHTML = "🌴" }
}
updateTries()
}else
{ alert("The Game is Over, Silly!") }
}
function updateTries(){
var newTriesLeft = "Tries Left: " + (triesAvailable - ++clickCounter)
document.getElementById("triesLeft").innerHTML = newTriesLeft
if (clickCounter === triesAvailable)
{ setTimeout(gameOverEvent,50,2) }
}
function updateTimeElapsed(){
var newTimePassed = `Time: ${ minutesPassed }:${ secondsPassed }`
document.getElementById("elapsedTime").innerHTML = newTimePassed
if (++secondsPassed === 60){
minutesPassed++
secondsPassed = 0
}
}
function startTimer(){
updateTimeElapsed()
if(!gameOver)
{setTimeout(startTimer,1000)}
}
function gameOverEvent(endState){
gameOver = true
if (endState === 0)
{ alert("You found DA BOOTY!") }
else if( endState == 1)
{ alert("You got the bomb, loser. 🤡") }
else{ alert("Ran out of attempts. Try again!") }
}
// end of file