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
103 lines (95 loc) · 2.98 KB
/
script.js
File metadata and controls
103 lines (95 loc) · 2.98 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
// 2 players(Game Mode)
// 2 dices (randomiser function)
// Pick the order assign order for result
// Higher number wins (myOutputValue)
var currentGameMode = "waiting for user name";
var player1Result = 0;
var player2Result = 0;
var player1Name = "";
var player2Name = "";
var randomDiceRoll = 0;
var randomDiceRoll2 = 0;
var nameCounter = 0;
var rollDice = function () {
var randomDecimal = Math.random() * 6;
var randomInteger = Math.floor(randomDecimal);
var diceNumber = randomInteger + 1;
return diceNumber;
};
var main = function (input) {
var myOutputValue = "Hello ";
if (currentGameMode == "waiting for user name") {
if (nameCounter == 1) {
player2Name = input;
myOutputValue = myOutputValue + player2Name;
nameCounter = nameCounter - 1;
currentGameMode = "playerRollDice";
return myOutputValue;
}
// set the name
player1Name = input;
nameCounter = nameCounter + 1;
currentGameMode = "playerRollDice";
// now that we have the name, switch the mode
currentGameMode = "playerRollDice";
myOutputValue = myOutputValue + player1Name;
return myOutputValue;
} else if (currentGameMode == "playerRollDice") {
if (input == 1) {
var result = randomDiceRoll + "" + randomDiceRoll2;
myOutputValue = player1Name + " Your results is " + result;
if (player2Name != "") {
if (result > player1Result) {
myOutputValue = player2Name + "<br>You won with " + result;
}
if (player1Result > result) {
myOutputValue = player1Name + "<br>You won with " + player1Result;
}
currentGameMode = "waiting for user name";
return myOutputValue;
}
currentGameMode = "waiting for user name";
player1Result = result;
return myOutputValue;
}
if (input == 2) {
var result = randomDiceRoll2 + "" + randomDiceRoll;
myOutputValue = player1Name + " Your results is " + result;
if (player2Name != "") {
if (result > player1Result) {
myOutputValue = player2Name + "<br>You won with " + result;
}
if (player1Result > result) {
myOutputValue = player1Name + "<br>You won with " + player1Result;
}
currentGameMode = "waiting for user name";
return myOutputValue;
}
currentGameMode = "waiting for user name";
player1Result = result;
return myOutputValue;
}
myOutputValue = playDiceGame(player1Name);
//now that we have result from Player 1/ Player 2's turn starts
//currentGameMode = "player2"
return myOutputValue;
}
};
var playDiceGame = function () {
var message = "";
// dice game logic
randomDiceRoll = rollDice();
randomDiceRoll2 = rollDice();
message =
"Welcome" +
"<br>You rolled " +
randomDiceRoll +
" for Dice 1 and " +
randomDiceRoll2 +
" for Dice 2 <br>" +
"Choose order of the dice";
console.log(randomDiceRoll);
console.log(randomDiceRoll2);
console.log(message);
return message;
};