forked from rocketacademy/basics-beat-that
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
124 lines (107 loc) · 3.87 KB
/
script.js
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
// Beat That Project
// Global Variable
var [player1Ready, player2Ready, player1Dice, player2Dice] = Array(6).fill(false);
var [player1WonRounds, player2WonRounds, totalRounds] = Array(3).fill(0);
var dice1, dice2, player1Number, player2Number;
// Function rollDice
// Purpose: Use RNG to give to a number between 1 to 6
// Output: Returns a number between 1 to 6
var rollDice = function () {
return Math.floor(Math.random() * 6) + 1;
};
// Function checkUserInput
// Purpose: Ensure that the user only enter "1" or "2"
// Input: User input
// Output: Returns False if correct Input or True if incorrect Input
var checkUserInput = function (userInput) {
if (userInput === "1" || userInput === "2") {
return false;
}
return true;
};
// Function pickOrder
// Purpose: Concat both dice into 1 String
// Input: dice1, dice2 and user choice in which order
// Output: String after combining
var pickOrder = function (dice1, dice2, choice) {
if (choice === "1") {
return "" + dice1 + dice2;
} else {
return "" + dice2 + dice1;
}
};
// Function resetGame
// Purpose: Reset the game to the original state
var resetGame = function () {
player1Ready = false;
player2Ready = false;
player1Dice = false;
player2Dice = false;
};
// Function createTable
// Purpose: Create a Scoreboard to be printed at the end of the round
var createTable = function() {
var output = "<table><tr><th>Name</th><th>Score</th></tr><tr><td>";
if (player1WonRounds > player2WonRounds || player1WonRounds === player2WonRounds) {
output += `Player 1</td><td>${player1WonRounds}</td></tr><tr><td>Player 2</td><td>${player2WonRounds}</td>`;
} else {
output += `Player 2</td><td>${player2WonRounds}</td></tr><tr><td>Player 1</td><td>${player1WonRounds}</td>`;
}
output += "</tr></table>"
return output;
};
var main = function (input) {
let myOutputValue = '';
if (player1Ready === false) {
dice1 = rollDice();
dice2 = rollDice();
myOutputValue += "Welcome Player 1.<br><br>";
myOutputValue += `You rolled ${dice1} for Dice 1 and ${dice2} for Dice 2.<br><br>Choose the order of the dice.`;
player1Ready = true; // Changes the program state where player 1 has rolled the dice
return myOutputValue;
}
if (player1Dice === false) {
if (checkUserInput(input)) {
return "Please return a valid input (1 or 2).";
}
player1Number = pickOrder(dice1, dice2, input);
player1Number = parseInt(player1Number);
player1Dice = true; // Changes the program state where player 1 has picked the order
myOutputValue = "Player 2, please press the submit button to roll dice.";
return myOutputValue;
}
if(player2Ready === false) {
dice1 = rollDice();
dice2 = rollDice();
myOutputValue += "Welcome Player 2.<br><br>";
myOutputValue += `You rolled ${dice1} for Dice 1 and ${dice2} for Dice 2.<br><br>Choose the order of the dice.`;
player2Ready = true; // Changes the program state where player 2 has rolled the dice
return myOutputValue;
}
if (player2Dice === false) {
if (checkUserInput(input)) {
return "Please return a valid input (1 or 2).";
}
player2Number = pickOrder(dice1, dice2, input);
player2Number = parseInt(player2Number);
player2Dice = true;
}
// Win Condition
if (player1Number > player2Number) {
myOutputValue += "Player 1 won.<br><br>";
player1WonRounds += 1;
} else if (player2Number > player1Number) {
myOutputValue += "Player 2 won.<br><br>";
player2WonRounds += 1;
} else {
myOutputValue += "Draw.<br><br>";
}
totalRounds += 1;
console.log("Player 1 Number:", player1Number);
console.log("Player 2 Number:", player2Number);
myOutputValue += `Player 1 has ${player1Number} and Player 2 has ${player2Number}<br><br>Press submit to start new game<br><br>`
myOutputValue += createTable();
// Resets the round to the beginning state
resetGame();
return myOutputValue;
};