forked from rocketacademy/basics-beat-that
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
204 lines (187 loc) · 6.23 KB
/
script.js
File metadata and controls
204 lines (187 loc) · 6.23 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Track number of players in the game
var numPlayers = 0;
// Track number of dice to use
var numDice = 0;
// Track game version
var gameVersion = "";
// Track game mode - allows the game to move into the different player stages eg. rolling dice, getting sequence etc
var gameMode = 1;
// Variable to store a player's dice rolls
var playerRoll = [];
// Variable to store all player's combined numbers
var playerCombinedNums = [];
// Variable to track running scores of each player.
var runningScore = [];
// Function to get user inout for game version.
var getVersion = function (input) {
gameVersion = input;
return `In this mode, the ${gameVersion} number will win the game. <br><br> Please input the number of players.`;
};
// Function to generate dice roll
var diceRoll = function () {
return Math.floor(Math.random() * 6) + 1;
};
//Request for player input to get number of players in game.
var getPlayers = function (input) {
numPlayers = input;
return `Welcome! There are ${numPlayers} players in this game. <br><br> Please select how many dice you want to play with. `;
};
// Get player input to number of dice used in game.
var getNumDice = function (input) {
numDice = input;
return `You will be playing with ${numDice} dice in this game. <br><br> Player 1, please click "submit" to roll the dice.`;
};
// Get player to roll dice.
var getPlayerRoll = function () {
var index = 0;
var playerDiceRoll = [];
while (index < numDice) {
playerDiceRoll.push(diceRoll());
index += 1;
}
return playerDiceRoll;
};
// Get player to select the sequence of the dice. Function takes in the sequence input, and the array of dice rolls.
var getSequence = function (input, diceArray) {
var combinedNum = 0;
var autoCombinedNum = 0;
var indexArray = input.split("").map(Number);
// This loop takes in the user input sequence and generate the combined number.
for (var i = 0; i < indexArray.length; i += 1) {
combinedNum = combinedNum * 10 + diceArray[indexArray[i]];
}
// This loop auto generates the highest/lowest number depending on the game version.
var index = 0;
var maxIndex = 0;
var maxNum = 0;
var tempArray = diceArray;
while (autoCombinedNum.toString().length <= diceArray.length + 1) {
if (gameVersion == "highest") {
while (index < tempArray.length) {
if (tempArray[index] > maxNum) {
maxNum = tempArray[index];
maxIndex = index;
index += 1;
} else {
index += 1;
}
}
autoCombinedNum = autoCombinedNum * 10 + maxNum;
tempArray.splice(maxIndex, 1);
maxNum = 0;
index = 0;
} else {
maxNum = 7;
while (index < tempArray.length) {
if (tempArray[index] < maxNum) {
maxNum = tempArray[index];
maxIndex = index;
index += 1;
} else {
index += 1;
}
}
autoCombinedNum = autoCombinedNum * 10 + maxNum;
tempArray.splice(maxIndex, 1);
maxNum = 7;
index = 0;
}
}
return [combinedNum, autoCombinedNum];
};
// Compare the numbers of the players and declare the winning player. Function takes in the array of combined players numbers. Returns the highest number and the winning player.
var compareNumbers = function (input) {
var maxNum = input[0];
var maxIndex = 0;
var index = 0;
if (gameVersion == "highest") {
while (index < input.length) {
if (input[index] > maxNum) {
maxNum = input[index];
maxIndex = index;
index += 1;
} else {
index += 1;
}
}
playerCount = 0;
} else {
while (index < input.length) {
if (input[index] < maxNum) {
maxNum = input[index];
maxIndex = index;
index += 1;
} else {
index += 1;
}
}
playerCount = 0;
}
return [maxNum, maxIndex + 1];
};
// Show leaderboard. Function takes in array of running scores.
var showLeaderboard = function (input) {
var output = "";
for (var i = 0; i < input.length; i += 1) {
output += `Player ${i + 1}: ${input[i]} <br>`;
}
return output;
};
// Variable to initialise while loop. Left out of main fuction so it does not reset to 0 everytime "submit" is clicked.
var playerCount = 0;
var main = function (input) {
if (gameVersion == "") {
return getVersion(input);
} else if (numPlayers == 0) {
return getPlayers(input);
} else if (numPlayers != 0 && numDice == 0) {
return getNumDice(input);
} else {
while (playerCount < numPlayers) {
if (gameMode == 1) {
playerRoll = getPlayerRoll();
gameMode = 2;
return `Hello Player ${
playerCount + 1
}! <br> You have rolled ${playerRoll}. <br> Please select the sequeuce of your dice. <br> Please enter the index of the dice roll (use zero indexing).`;
} else if (gameMode == 2) {
gameMode = 1;
var thisPlayerNum = getSequence(input, playerRoll);
if (runningScore.length < playerCount + 1) {
runningScore.push(thisPlayerNum[0]);
} else {
runningScore[playerCount] += thisPlayerNum[0];
}
playerCombinedNums.push(thisPlayerNum[0]);
if (playerCount == numPlayers - 1) {
var outputValue = `Player ${
playerCount + 1
} <br> You have formed the number ${
thisPlayerNum[0]
} <br> The auto generated ${gameVersion} number is ${
thisPlayerNum[1]
}. <br><br> Click "submit" to see the results`;
playerCount += 1;
} else {
var outputValue = `Player ${
playerCount + 1
} <br> You have formed the number ${
thisPlayerNum[0]
} <br> The auto generated ${gameVersion} number is ${
thisPlayerNum[1]
}. <br><br> Click "submit" for the next player to roll the dice.`;
playerCount += 1;
}
return outputValue;
}
}
var winningItems = compareNumbers(playerCombinedNums);
// Assign empty array again to restart game.
playerCombinedNums = [];
return `Player ${winningItems[1]} won with the ${gameVersion} number of ${
winningItems[0]
}!<br><br> ========= LEADERBOARD ========= <br> ${showLeaderboard(
runningScore
)} <br><br> Click "submit" to play another round!`;
}
};