forked from rocketacademy/basics-scissors-paper-stone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
100 lines (88 loc) · 3.41 KB
/
script.js
File metadata and controls
100 lines (88 loc) · 3.41 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
// User inputs one of "scissors", "paper", or "stone",
// Program internally randomly chooses scissors, paper, or stone,
// Program outputs whether the user won, the program won, or it's a draw.
// Rules: scissors beats paper, paper beats stone, and stone beats scissors.
// If both parties choose the same object, it's a draw.
var currentGameMode = "Waiting for user name..";
console.log(currentGameMode);
var userName = "";
// HELPER FUNCTIONS HERE
var chooseScissorsPaperStone = function () {
var randomDecimal = Math.random() * 3; // computer random selection
var randomInteger = Math.floor(randomDecimal);
var randomScissorsPaperStone = randomInteger + 1; // Add 1 to get valid numbers 1 to 3 inclusive, which represents each action
return randomScissorsPaperStone; // return 3 random values to represent each action
};
var generateScissorsPaperStone = function () {
var scissorsPaperStone = chooseScissorsPaperStone(); // assigning values to variables here
if (scissorsPaperStone == 1) {
return "scissors";
}
if (scissorsPaperStone == 2) {
return "paper";
}
if (scissorsPaperStone == 3) {
return "stone";
}
};
var userWinCounter = 0;
var computerWinCounter = 0;
var drawCounter = 0;
var main = function (input) {
var myOutputValue =
"You can only input 'scissors', 'paper' or 'stone'. Please input valid option!";
if (currentGameMode == "Waiting for user name..") {
// set the name
userName = input;
// now that we have the name, switch the mode
currentGameMode = "Scissors paper stone game is starting!";
console.log(currentGameMode);
myOutputValue =
"Hello " +
userName +
"! Welcome to the scissors paper stone game. Please input 'scissors', 'paper' or 'stone' only to start playing!";
} else if ((currentGameMode = "Scissors paper stone game is starting!")) {
console.log(currentGameMode);
var computer = generateScissorsPaperStone();
console.log("Computer chooses", computer);
if (
(input == "scissors" && computer == "scissors") ||
(input == "paper" && computer == "paper") ||
(input == "stone" && computer == "stone")
) {
drawCounter = drawCounter + 1;
console.log(drawCounter);
console.log("draw!");
myOutputValue =
"It's a draw! You have drawn " + drawCounter + " time(s)!";
}
if (
(input == "scissors" && computer == "paper") ||
(input == "paper" && computer == "stone") ||
(input == "stone" && computer == "scissors")
) {
userWinCounter = userWinCounter + 1;
console.log(userWinCounter);
console.log("Winning actions");
myOutputValue = `${userName}, you chose ${input} and won! <br><br>
The computer chose ${computer} and lost! <br><br>
Bravo, keep it up ${userName}! You've won ${userWinCounter} turn(s) so far!`;
}
if (
(input == "scissors" && computer == "stone") ||
(input == "paper" && computer == "scissors") ||
(input == "stone" && computer == "paper")
) {
computerWinCounter = computerWinCounter + 1;
console.log(computerWinCounter);
console.log("Losing actions");
myOutputValue =
`${userName}, you chose ${input} and lost! <br><br>
The computer chose ${computer} and won ` +
computerWinCounter +
` turn(s)! <br><br>
Good try ${userName}! You've won ${userWinCounter} turn(s) so far, keep going!`;
}
}
return myOutputValue;
};