-
Notifications
You must be signed in to change notification settings - Fork 0
/
old-index.html
126 lines (109 loc) · 4.31 KB
/
old-index.html
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
125
126
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rock, paper, scissors game</title>
</head>
<body>
<script>
//Storing throws for both opponents
let playerSelection;
let computerSelection;
//Storing game score
let playerScore = 0;
let computerScore = 0;
//Hint for starting the game
alert("Please open your browser console.\nHit [Ctrl+Shift+J] or [Cmd+Shift+J] on Mac\nThen type 'game()' without quotes to start.");
//Computer random throw function
function computerPlay() {
let randomComputerThrow = Math.random();
if (randomComputerThrow <= 0.33) {
computerSelection = "Rock";
} else if (randomComputerThrow > 0.33 && randomComputerThrow <= 0.66) {
computerSelection = "Paper";
} else {
computerSelection = "Scissors";
}
return (computerSelection);
}
//Player throw function
function userPlay() {
let inputThrow = prompt("What you wish to throw?\nEnter: rock, paper or scissors")
inputThrow = inputThrow.toLowerCase();
if (inputThrow === "rock") {
playerSelection = "Rock";
} else if (inputThrow === "paper") {
playerSelection = "Paper";
} else if (inputThrow === "scissors") {
playerSelection = "Scissors";
} else {
alert("Something was misspeled.");
playerSelection = "Misspelling"
}
return (playerSelection);
}
//Single play round function
function playSingleRound(playerSelection, computerSelection) {
if (playerSelection === "Misspelling") {
alert("Your opponnent graciously agrees for tie round.");
} else if (playerSelection === "Rock") {
if (computerSelection === "Rock") {
alert(`${playerSelection} vs ${computerSelection}\n\nIt's a tie ...`);
} else if (computerSelection === "Paper") {
alert(`You chose ${playerSelection}. Opponent chose ${computerSelection}.\n\nYou lost a round =(`);
computerScore++;
} else if (computerSelection === "Scissors") {
alert(`You chose ${playerSelection}. Opponent chose ${computerSelection}.\n\nYou won a round!`);
playerScore++;
} else {
alert("You found yourself a bug! Please contact me.");
}
} else if (playerSelection === "Paper") {
if (computerSelection === "Paper") {
alert(`${playerSelection} vs ${computerSelection}\n\nIt's a tie ...`);
} else if (computerSelection === "Scissors") {
alert(`You chose ${playerSelection}. Opponent chose ${computerSelection}.\n\nYou lost a round =(`);
computerScore++;
} else if (computerSelection === "Rock") {
alert(`You chose ${playerSelection}. Opponent chose ${computerSelection}.\n\nYou won a round!`);
playerScore++;
} else {
alert("You found yourself a bug! Please contact me.");
}
} else if (playerSelection === "Scissors") {
if (computerSelection === "Scissors") {
alert(`${playerSelection} vs ${computerSelection}\n\nIt's a tie ...`);
} else if (computerSelection === "Rock") {
alert(`You chose ${playerSelection}. Opponent chose ${computerSelection}.\n\nYou lost a round =(`);
computerScore++;
} else if (computerSelection === "Paper") {
alert(`You chose ${playerSelection}. Opponent chose ${computerSelection}.\n\nYou won a round!`);
playerScore++;
} else {
alert("You found yourself a bug! Please contact me.");
}
}
}
//Main function
function game() {
//Clearing the score for new game
playerScore = 0;
computerScore = 0;
for (let roundCounter = 1; roundCounter <= 5; roundCounter++) {
computerPlay();
userPlay();
playSingleRound(playerSelection, computerSelection);
alert(`Round: ${roundCounter} \nYour score: ${playerScore} \nOpponent score: ${computerScore} \n${5 - roundCounter} rounds left`)
}
if (computerScore > playerScore) {
alert("Opponent won the game =(");
} else if (playerScore > computerScore) {
alert("You won the game!");
} else {
alert("It's a tie! \nPeace ^-^");
}
}
</script>
</body>
</html>