-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
69 lines (62 loc) · 1.92 KB
/
script.js
File metadata and controls
69 lines (62 loc) · 1.92 KB
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
var diceRoll = function(){
return Math.floor(Math.random()*6+1)
}
// score leaderboard tracker game with 4 players//
var numOfPlayers = 1000
var scoreArray = [0]
var player = 1
var numOfPlayersSpecified = false
var diceOne = 0
var diceTwo = 0
var main = function(input){
// take the user input as the number of players in the game
if (numOfPlayersSpecified==false){
// input validation
if (input <2 || input >10){
return `Greetings! Please indicate the number of players`
}
numOfPlayers = input;
numOfPlayersSpecified = true
return "Starting Game. It is Player 1's turn. Press Submit to Roll"
}
// player playing is same as number of players restart player count to 1
if (player == Number(numOfPlayers) + 1){
player = 1
}
// roll dice and determine dice number for each player
diceOne = diceRoll()
diceTwo = diceRoll()
diceNumber = determineDiceNumber(diceOne,diceTwo)
// add dice number to array
if (scoreArray.length < player){
scoreArray.push(diceNumber)
}
else{
scoreArray[player-1] += diceNumber
}
// increment the player count
player += 1
// make a copy of the scoreArray so as to acheive score board
tempArray = [...scoreArray]
scoreBoard = getScoreBoard(tempArray)
return ` Hello Player ${player-1}. <br>Your number rolled for dice one is ${diceOne} <br> Your number rolled for dice two is ${diceTwo}.<br> Your dice num for this turn is ${diceNumber}. ${scoreBoard}.`
}
var determineDiceNumber = function(diceOne,diceTwo){
number1 = diceOne * 10 + diceTwo
number2 = diceTwo * 10 + diceOne
if (number1 > number2){
return number1
}
return number2
}
var getScoreBoard = function(array){
str = ``
for (i = 0; i<array.length;i+=1){
maxIndex = array.indexOf(Math.max(...array))
console.log(maxIndex)
console.log(array[maxIndex])
str += `<br>Player ${maxIndex+1} score: ${array[maxIndex]}`
array[maxIndex] = -1
}
return str
}