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
76 lines (69 loc) · 2.97 KB
/
script.js
File metadata and controls
76 lines (69 loc) · 2.97 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
//create required variables
var mode = "roll dice";
var playerNumber = 1;
var diceRoll1 = 0;
var diceRoll2 = 0;
var player1Dice = [];
var player2Dice = [];
var player1Output = [];
var player2Output = [];
function getRandomNumber() {
return Math.floor(Math.random() * 6) + 1;
}
var determineWinner = function () {
if (player1Output > player2Output) {
return "Player 1 won";
} else if (player1Output < player2Output) {
return "Player 2 won";
}
return "A tie";
};
var concatenate2Num = function (num1, num2) {
return Number(String(num1) + String(num2));
};
function main(input) {
if (mode == "roll dice" && playerNumber == 1 && input == "") {
diceRoll1 = getRandomNumber();
diceRoll2 = getRandomNumber();
player1Dice = [diceRoll1, diceRoll2];
mode = "enter order";
return `You are Player number 1. You rolled ${diceRoll1} and ${diceRoll2}. Now, please pick Dice 1 or 2 as the first numerical of the combined number e.g. 2`;
} else if ((mode = "roll dice" && playerNumber == 2 && input == "")) {
diceRoll1 = getRandomNumber();
diceRoll2 = getRandomNumber();
player2Dice = [diceRoll1, diceRoll2];
mode = "enter order";
return `You are Player number 2. You rolled ${diceRoll1} and ${diceRoll2}. Now, please pick Dice 1 or 2 as the first numerical of the combined number e.g. 2`;
}
if ((mode = "enter order" && playerNumber == 1 && input == 1)) {
player1Output = concatenate2Num(player1Dice[0], player1Dice[1]);
playerNumber = 2;
console.log(playerNumber);
mode = "roll dice";
console.log(mode);
return `You picked Dice 1 as the first numerical of the combined number. Your result: ${player1Output}. It's player 2's turn now. Click Submit to roll dice`;
} else if ((mode = "enter order" && playerNumber == 1 && input == 2)) {
player1Output = concatenate2Num(player1Dice[1], player1Dice[0]);
playerNumber = 2;
console.log(playerNumber);
mode = "roll dice";
console.log(mode);
return `You picked Dice 2 as the first numerical of the combined number. Your result: ${player1Output}. It's player 2's turn now. Click Submit to roll dice`;
} else if ((mode = "enter order" && playerNumber == 2 && input == 1)) {
player2Output = concatenate2Num(player2Dice[0], player2Dice[1]);
playerNumber = 1;
console.log(playerNumber);
mode = "roll dice";
console.log(mode);
var winningPlayer = determineWinner();
return `You picked Dice 1 as the first numerical of the combined number. Player 2: ${player2Output} & Player 1: ${player1Output}. ${winningPlayer} won`;
} else if ((mode = "enter order" && playerNumber == 2 && input == 2)) {
player2Output = concatenate2Num(player2Dice[1], player2Dice[0]);
playerNumber = 1;
console.log(playerNumber);
mode = "roll dice";
console.log(mode);
var winningPlayer = determineWinner();
return `You picked Dice 2 as the first numerical of the combined number. Player 2: ${player2Output} & Player 1: ${player1Output}. ${winningPlayer} won`;
}
}