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
190 lines (174 loc) Β· 6.37 KB
/
script.js
File metadata and controls
190 lines (174 loc) Β· 6.37 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
// There are 2 players and players take turns.
// When a player clicks Submit, the game rolls 2 dice and shows the dice rolls, for example 3 and 6.
// The player picks the order of the dice they want. For example, if they wanted the number 63, they would specify that the 2nd dice goes first.
// You can choose how the player specifies dice order.
// After both players have rolled and chosen dice order, the player with the higher combined number wins.
// Score
// 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.
// global variables
var playerOneDiceOne = "";
var playerOneDiceTwo = "";
var playerTwoDiceOne = "";
var playerTwoDiceTwo = "";
var playerOneNumber = "";
var playerTwoNumber = "";
var playerOneScore = [];
var playerTwoScore = [];
var gameMode = "player 1";
// calc total player 1 score indefinitely
var totalPlayerScore = function () {
var playerOneTotalScore = 0;
var playerTwoTotalScore = 0;
for (i = 0; i < playerOneScore.length; i++) {
playerOneTotalScore = playerOneScore[i];
console.log(
playerOneScore
.map(function (elt) {
// assure the value can be converted into an integer
return /^\d+$/.test(elt) ? parseInt(elt) : 0;
})
.reduce(function (a, b) {
// sum all resulting numbers
return a + b;
})
);
}
for (j = 0; j < playerTwoScore.length; j++) {
playerTwoTotalScore = playerTwoScore[j];
console.log(
playerTwoScore
.map(function (elt) {
// assure the value can be converted into an integer
return /^\d+$/.test(elt) ? parseInt(elt) : 0;
})
.reduce(function (a, b) {
// sum all resulting numbers
return a + b;
})
);
}
return `Player 1 total score is ${playerOneTotalScore} and Player 2 total score is ${playerTwoTotalScore}.`;
};
// roll dice to string
var rollDice = function () {
var randomDecimal = Math.random() * 6;
var randomInteger = Math.floor(randomDecimal);
var diceNumber = randomInteger + 1;
return diceNumber.toString();
};
// player 1 dice roll
var playerOneDiceRoll = function () {
playerOneDiceOne = rollDice();
playerOneDiceTwo = rollDice();
gameMode = "player 1 position";
return `Hello Player 1 πΆ.<br><br>You rolled ${playerOneDiceOne} for Dice π² One and ${playerOneDiceTwo} for Dice π² Two.<br><br>Now please choose the order of your Dice π² by entering 1 or 2.`;
};
// player 1 choose dice position
var playerOneChoice = function (input) {
if (input == 1 || input == 2) {
playerOneNumber = combineDiceRolls(input);
playerOneScore.push(playerOneNumber);
gameMode = "player 2";
return `Player 1 πΆ.<br><br>Your chosen number is ${playerOneNumber}.<br><br>It is now Player 2's πΉ turn to give it a go.<br><br>Click submit to roll the dice π².`;
} else {
return verifyInput(input);
}
};
// player 2 dice roll
var playerTwoDiceRoll = function () {
playerTwoDiceOne = rollDice();
playerTwoDiceTwo = rollDice();
gameMode = "player 2 position";
return `Hello Player 2 πΉ.<br><br>You rolled ${playerTwoDiceOne} for Dice π² One and ${playerTwoDiceTwo} for Dice π² Two.<br><br>Now please choose the order of your Dice π² by entering 1 or 2.`;
};
// player 2 choose dice position
var playerTwoChoice = function (input) {
if (input == 1 || input == 2) {
playerTwoNumber = combineDiceRolls(input);
playerTwoScore.push(playerTwoNumber);
gameMode = "score time";
return `Player 2 πΉ.<br><br>Your chosen number is ${playerTwoNumber}.<br><br>Let's see who's the winner?! πΎ`;
} else {
return verifyInput(input);
}
};
// verify if input is a valid number
var verifyInput = function (input) {
if (input != 1 || input != 2) {
return "π
Please enter 1 or 2 to position your dice π² roll.";
}
};
// combine the dice rolls
var combineDiceRolls = function (input) {
if (input == 1 && gameMode == "player 1 position") {
return playerOneDiceOne + playerOneDiceTwo;
} else if (input == 1 && gameMode == "player 2 position") {
return playerTwoDiceOne + playerTwoDiceTwo;
} else if (input == 2 && gameMode == "player 1 position") {
return playerOneDiceTwo + playerOneDiceOne;
} else if (input == 2 && gameMode == "player 2 position") {
return playerTwoDiceTwo + playerTwoDiceOne;
}
};
// player scoring
var finalScore = function () {
if (playerOneScore > playerTwoScore) {
return `Player 1 πΆ wins π with ${
playerOneScore[playerOneScore.length - 1]
} and Player 2 πΉ loses π with ${
playerTwoScore[playerTwoScore.length - 1]
}`;
} else if (playerOneScore < playerTwoScore) {
return `Player 1 πΆ loses π with ${
playerOneScore[playerOneScore.length - 1]
} and Player 2 πΉ wins π with ${
playerTwoScore[playerTwoScore.length - 1]
}`;
} else if (playerOneScore == playerTwoScore) {
return `It is a draw as Player 1 πΆ number is ${
playerOneScore[playerOneScore.length - 1]
} and Player 2 πΉ number is also ${
playerTwoScore[playerTwoScore.length - 1]
}`;
}
};
// continue playing
var playAgain = function () {
gameMode = "player 1";
};
var main = function (input) {
// player 1 rolls dice
if (gameMode == "player 1") {
myOutputValue = playerOneDiceRoll();
console.log(
`player 1 dice roll is ${playerOneDiceOne} and ${playerOneDiceTwo}`
);
// player 1 choose dice position
} else if (gameMode == "player 1 position") {
myOutputValue = playerOneChoice(input);
console.log(`player 1 number is ${playerOneNumber}`);
// player 2 rolls dice
} else if (gameMode == "player 2") {
myOutputValue = playerTwoDiceRoll();
console.log(
`player 1 dice roll is ${playerTwoDiceOne} and ${playerTwoDiceTwo}`
);
// player 2 choose dice position
} else if (gameMode == "player 2 position") {
myOutputValue = playerTwoChoice(input);
console.log(`player 2 number is ${playerTwoNumber}`);
// who has the bigger number
} else if (gameMode == "score time") {
myOutputValue =
finalScore() +
"<br><br>Press submit to play again!" +
"<br><br>" +
totalPlayerScore();
console.log(
`player 1 score in array is ${playerOneScore} and player 2 is ${playerTwoScore}`
);
// continue playing
playAgain();
}
return myOutputValue;
};