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
153 lines (133 loc) Β· 5.34 KB
/
script.js
File metadata and controls
153 lines (133 loc) Β· 5.34 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
var AWAITING_PLAYER_ONE = "awaiting player one";
var START_PLAYER_DICE_ROLL = "start dice game for players";
var CHOOSE_COMBINATION_PLAYER = "player decides dice combination";
var currentGameMode = AWAITING_PLAYER_ONE;
/// Keep track of the current player's number, either 1 or 2.
// The game starts with Player 1.
var currentPlayer = 1;
/// Keep track of each player's dice rolls
var playerOneDice = [];
var playerTwoDice = [];
// Keep track of each player's chosen numbers
var playerOneCombination;
var playerTwoCombination;
// Create an array combinedDiceRolls with 2 independent dice roll values
// Assign combinedDiceRolls to the current player's dice array
// If currentPlayer is not 1, assume currentPlayer is 2
// Return combined dice rolls to parent function
var getDiceRolls = function () {
var combinedDiceRolls = [diceRollOne(), diceRollTwo()];
if (currentPlayer === 1) {
playerOneDice = combinedDiceRolls;
} else {
playerTwoDice = combinedDiceRolls;
}
return combinedDiceRolls;
};
var concatenateTwoNumbers = function (num1, num2) {
return Number(String(num1) + String(num2));
};
console.log("two numbers");
var getPlayerNumber = function (firstNumeralIndex) {
var diceArray;
if (currentPlayer === 1) {
diceArray = playerOneDice;
} else {
diceArray = playerTwoDice;
}
console.log("diceArray: " + diceArray);
var playerNum;
// If the chosen first numeral index is 1, create player number starting with 1st dice
// Otherwise, create player number starting with 2nd dice
if (firstNumeralIndex === 1) {
playerNum = concatenateTwoNumbers(diceArray[0], diceArray[1]);
} else {
playerNum = concatenateTwoNumbers(diceArray[1], diceArray[0]);
}
console.log("playerNum: " + playerNum);
// Store player combination in the relevant global player combination variable
// Return generated player num to parent function
if (currentPlayer === 1) {
playerOneCombination = playerNum;
} else {
playerTwoCombination = playerNum;
}
return playerNum;
};
/**
* Compute the winner between Player 1 and Player 2.
* Return either 1 or 2 to represent the winning player.
* In the event of a tie, Player 2 wins.
*/
var determineWinner = function () {
if (playerOneCombination > playerTwoCombination) {
return 1;
}
return 2;
};
var main = function (input) {
var myOutputValue = "";
if ((input == "") & (currentGameMode == AWAITING_PLAYER_ONE)) {
currentGameMode = START_PLAYER_DICE_ROLL;
myOutputValue =
"Hi Player One, welcome to the π² dice π² roll game! <br> <br> To start, please hit the submit button to roll the dice π²!";
} else if (currentGameMode == START_PLAYER_DICE_ROLL) {
var playerDiceRolls = getDiceRolls();
currentGameMode = CHOOSE_COMBINATION_PLAYER;
myOutputValue =
"Hi Player " +
currentPlayer +
" , you rolled: " +
playerDiceRolls[0] +
" and " +
playerDiceRolls[1] +
"<br> Please enter βΆ or β· as the first numeral index for your dice rolls to get the highest combined number and hit the submit button.";
} else if (currentGameMode == CHOOSE_COMBINATION_PLAYER) {
// Validate the input. If first numeral index is neither 1 nor 2, tell the user.
var firstNumeralIndex = Number(input);
if (firstNumeralIndex !== 1 && firstNumeralIndex !== 2) {
return "Please choose βΆ or β· as the first numeral index for your dice rolls";
}
// Get combined number for current player
console.log("firstNumeralIndex: " + firstNumeralIndex);
var playerCombined = getPlayerNumber(firstNumeralIndex);
console.log("playerCombined: " + playerCombined);
var playerNumResponse = `Player ${currentPlayer}, you chose π² Dice ${firstNumeralIndex} first. <br>
Your number is ${playerCombined}.`;
// If currentPlayer is Player 1, change currentPlayer to Player 2, switch mode to dice roll
if (currentPlayer === 1) {
currentPlayer = 2;
currentGameMode = START_PLAYER_DICE_ROLL;
// Return player number to Player 1, let Player 2 know it is their turn
return `${playerNumResponse} <br>
It is now Player 2's turn. Press Submit to roll Player 2's π² dice.`;
}
// Else if currentPlayer is Player 2, determine the winner and let the players know who won.
var winningPlayer = determineWinner();
// Reset the game
currentPlayer = 1;
currentGameMode = AWAITING_PLAYER_ONE;
// Return the game end response
return `${playerNumResponse} <br>
Congrats, Player ${winningPlayer} has won ππ»ππ»ππ». <br>
Player 1's number: ${playerOneCombination} | Player 2's number: ${playerTwoCombination} <br> <br>
Press Submit to play again.`;
}
return myOutputValue;
};
var diceRollOne = function () {
var randomDecimal = Math.random() * 6;
var randomInteger = Math.floor(randomDecimal);
var diceNumber = randomInteger + 1;
return diceNumber;
};
var diceRollTwo = function () {
var randomDecimal = Math.random() * 6;
var randomInteger = Math.floor(randomDecimal);
var diceNumber = randomInteger + 1;
return diceNumber;
};
// Base with some help from the ref solution
// NaN when concatenating two numbers, I tried debugging but I still don't know what is the problem
//
// Keep score for each player. The score is the running sum of all numbers that player has generated so far. This means there is no permanent winner, only a temporary leader.