I’m creating a javascript battleships game. The game works fine. I have created the game so that the user has a maximum of 25 misses – if they reach 25 then it’s game over.
I now want to create a function that at the end of the game, the function checks the number of misses, and alerts the user their score and rank name.
I calculated the score by ((25/misses)*100).
So these are the possible scores with misses.
Misses | Score
(1 |2500)
(2 | 1250)
(3 | 833)
(4 | 625)
(5 | 500)
(6 | 417)
(7 | 357)
(8 | 313)
(9 | 278)
(10 | 250)
(11 | 227)
(12 | 208)
(13 | 192)
(14 | 179)
(15 | 167)
(16 | 156)
(17 | 147)
(18 | 139)
(19 | 132)
(20 | 125)
(21 | 119)
(22 | 114)
(23 | 109)
(24 | 104)
(25 | 0)
The rank names I have are
1) Admiral of the Fleet
2) Admiral
3) Vice-Admiral
4) Rear-Admiral
5) Commodore
6) Killed In Action
These are how i’ve assigned the misses with the rank names.
Misses | Rank Name
(0-12 | Admiral of the Fleet)
(13-15 | Admiral)
(16-19 | Vice-Admiral)
(20-24 |Commodore)
(25 | Kill In Action)
I have created a variable named ‘misses’. Each time you miss, obviously the variable increments by one.
So let’s say you just played and finished the game with 14 misses. I want the function to check the variable ‘misses’ and then assign it with the appropriate score and rank.
I reckon I could probably use an array for the misses with score. Any ideas what the exact code is? please help!
Simplest solution:
You could also do something like this, which is more complicated and uses more memory, but would result in a faster
getRank()– and if you won’t have over 25 misses, the memory usage won’t be a problem.