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
165 lines (153 loc) · 5.05 KB
/
Copy pathscript.js
File metadata and controls
165 lines (153 loc) · 5.05 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
//Requirements
//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.
//1) a) create a dice game with 2 dices for player 1, b) let player 1 to choose which dice they want to be the first one.
//2) include player 2 and let them do same things as player 1
//3)compare the 2 dice rolls and announce the winner
//Global variables:
var GAME_STATE_ROLL_DICE = "GAME_STATE_ROLL_DICE ";
var GAME_STATE_CHOOSE_DICE_ORDER = "GAME_STATE_CHOOSE_DICE_ORDER";
var GAME_STATE_COMPARE_SCORES = "GAME_STATE_COMPARE_SCORES";
var GAME_STATE_PLAY_AGAIN = "GAME_STATE_PLAY_AGAIN";
var gameState = GAME_STATE_ROLL_DICE;
var currentPlayer = 1;
var currentPlayerRolls = []; //this array will store the currrent player rolls
var allPlayersScore = [];
var playerFinalValue = 0;
var errorMessage = "";
//Helper function "Roll the dice"
var rollDice = function () {
var randomDecimal = Math.random() * 6;
var randomInteger = Math.floor(randomDecimal);
var diceRoll = randomInteger + 1;
return diceRoll;
};
//Helper function "Roll the dice x2"
var rollDiceForPlayer = function () {
var counter = 0;
while (counter < 2) {
currentPlayerRolls.push(rollDice()); // adding dice rolls to the player rolls array on line 16
counter = counter + 1;
}
return (
"Welcome Player " +
currentPlayer +
". <br> <br> You rolled " +
currentPlayerRolls[0] +
" and " +
currentPlayerRolls[1] +
".<br> <br> Now please key in 1 or 2 to choose the corresponding dice as the first digit of your final value."
);
};
//helper function to get the player final value
var getPlayerFinalValue = function (input) {
//input validation:
if (input != 1 && input != 2) {
errorMessage =
"Error! Please only key in 1 or 2 to choose which dice to use as the first digit. Your current dice rolls are " +
currentPlayerRolls[0] +
" as dice 1 and " +
currentPlayerRolls[1] +
" as dice 2.";
return errorMessage;
} else {
//condition 1 "if input == 1"
if (input == 1) {
playerFinalValue = Number(
`${currentPlayerRolls[0]}` + `${currentPlayerRolls[1]}`
);
}
//condition 2 "if input == 2"
if (input == 2) {
playerFinalValue = Number(
`${currentPlayerRolls[1]}` + `${currentPlayerRolls[0]}`
);
}
allPlayersScore.push(playerFinalValue); // add the final values to the corresponding array on line 19
currentPlayerRolls = []; //clear the current player rolls array
return (
"Player " +
currentPlayer +
", your final value is " +
playerFinalValue +
"."
);
}
};
var comparePlayersScore = function () {
//player 1 wins:
if (allPlayersScore[0] > allPlayersScore[1]) {
return (
"Player's 1 final value is " +
allPlayersScore[0] +
". <br> <br> Player's 2 final value is " +
allPlayersScore[1] +
". <br> <br> " +
"Player 1 wins."
);
//player 2 wins
} else if (allPlayersScore[1] > allPlayersScore[0]) {
return (
"Player's 1 final value is " +
allPlayersScore[0] +
". <br> <br> Player's 2 final value is " +
allPlayersScore[1] +
". <br> <br> " +
"Player 2 wins."
);
// a tie:
} else {
return (
"Player's 1 final value is " +
allPlayersScore[0] +
". <br> <br> Player's 2 final value is " +
allPlayersScore[1] +
". <br> <br> " +
"It's a tie."
);
}
};
//main function
var main = function (input) {
var myOutputMessage = "";
if (gameState == GAME_STATE_ROLL_DICE) {
myOutputMessage = rollDiceForPlayer(); // call the roll dice x2 function
//cnange the game state
gameState = GAME_STATE_CHOOSE_DICE_ORDER;
return myOutputMessage;
}
if (gameState == GAME_STATE_CHOOSE_DICE_ORDER) {
myOutputMessage = getPlayerFinalValue(input); //output the player value
//if the input is incorrect, call the function again:
if (myOutputMessage == errorMessage) {
myOutputMessage = getPlayerFinalValue(input);
return myOutputMessage;
}
if (currentPlayer == 1) {
currentPlayer = 2;
gameState = GAME_STATE_ROLL_DICE;
return myOutputMessage + "<br> <br> It is now player's 2 turn.";
}
if (currentPlayer == 2) {
gameState = GAME_STATE_COMPARE_SCORES;
return (
myOutputMessage +
"<br> <br> Please click 'submit' to see the final score."
);
}
}
if (gameState == GAME_STATE_COMPARE_SCORES) {
myOutputMessage = comparePlayersScore();
allPlayersScore = [];
gameState = GAME_STATE_PLAY_AGAIN;
return myOutputMessage;
}
if (gameState == GAME_STATE_PLAY_AGAIN) {
gameState = GAME_STATE_ROLL_DICE;
currentPlayer = 1;
myOutputMessage = main();
}
return myOutputMessage;
};