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
102 lines (84 loc) · 4.28 KB
/
script.js
File metadata and controls
102 lines (84 loc) · 4.28 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
// 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.
var currentGameMode = "Waiting for players..";
console.log(currentGameMode);
// Keep track of the current player's number, either 1 or 2.
// The game starts with Player 1.
var currPlayer = 1;
// Keep track of each player's dice rolls
var player1Dice = [];
var player2Dice = [];
// keeping track of each player's chosen numbers
var playerOneNumber;
var playerTwoNumber;
// helper functions
var diceRoll = function () {
var randomDecimal = Math.random() * 6;
var randomInteger = Math.floor(randomDecimal);
var diceNumber = randomInteger + 1; // Add 1 to get valid dice rolls of 1 through 6 inclusive.
return diceNumber;
};
var generateDiceOrder = function () {
var diceOrder = Math.ceil() * 2; // Add 1 to get valid dice order from 1 to 2 inclusive
return diceOrder;
};
var main = function (input) {
var myOutputValue = `Choose 1 or 2 for the order only!`;
if (currentGameMode == "Waiting for players..") {
// switch the mode
playerOneDiceRoll = [diceRoll(), diceRoll()];
console.log("dice number for player one", playerOneDiceRoll);
currentGameMode = "Player 1 has rolled the dice";
console.log(currentGameMode);
return `Welcome Player 1! You rolled ${playerOneDiceRoll[0]} for Dice 1 and ${playerOneDiceRoll[1]} for Dice 2. Choose the order of the dice.`;
} else if (currentGameMode == "Player 1 has rolled the dice") {
if (input == 1) {
console.log("player 1 dice order", input);
currentGameMode = "Player 1 has chosen dice order. Player 2 next";
console.log(currentGameMode);
return `You have chosen Dice 1 to go first. Your number is ${playerOneDiceRoll[0]}${playerOneDiceRoll[1]}. Now is Player 2's turn, press submit to roll!`;
}
if (input == 2) {
console.log("player 1 dice order", input);
currentGameMode = "Player 1 has chosen dice order. Player 2 next";
console.log(currentGameMode);
return `You have chosen Dice 2 to go first. Your number is ${playerOneDiceRoll[1]}${playerOneDiceRoll[0]}. Now is Player 2's turn, press submit to roll!`;
}
}
if (currentGameMode == "Player 1 has chosen dice order. Player 2 next") {
playerTwoDiceRoll = [diceRoll(), diceRoll()];
console.log("dice number for player two", playerTwoDiceRoll);
currentGameMode = "Player 2 has rolled the dice!";
console.log(currentGameMode);
return `Welcome Player 2! You rolled ${playerTwoDiceRoll[0]} for Dice 1 and ${playerTwoDiceRoll[1]} for Dice 2. Choose the order of the dice.`;
} else if (currentGameMode == "Player 2 has rolled the dice!") {
if (input == 1) {
console.log("player 2 dice order", input);
currentGameMode = "Player 2 has chosen dice order";
console.log(currentGameMode);
return `You have chosen Dice 1 to go first. Your number is ${playerTwoDiceRoll[0]}${playerTwoDiceRoll[1]}. Press submit to see who won.`;
}
if (input == 2) {
currentGameMode = "Player 2 has chosen dice order";
console.log("player 2 dice order", input);
return `You have chosen Dice 2 to go first. Your number is ${playerTwoDiceRoll[1]}${playerTwoDiceRoll[0]}. Press submit to see who won.`;
}
}
if ((currentGameMode = "Player 2 has chosen dice order")) {
var playerOneNumber = (playerOneDiceRoll[0], playerOneDiceRoll[1]);
console.log("player one number is this", playerOneNumber);
var playerTwoNumber = (playerTwoDiceRoll[0], playerTwoDiceRoll[1]);
console.log("player two number is this", playerTwoNumber);
currentGameMode = "Both players have rolled the dice and we have a winner!";
console.log(currentGameMode);
if (playerOneNumber > playerTwoNumber) {
return `Since ${playerOneNumber} is more than ${playerTwoNumber}, Player 1 wins!`;
} else playerTwoNumber > playerOneNumber;
{
return `Since ${playerTwoNumber} is more than ${playerOneNumber}, Player 2 wins!`;
}
}
return myOutputValue;
};