-
Notifications
You must be signed in to change notification settings - Fork 0
/
index(old-no UI).html
151 lines (150 loc) · 5.75 KB
/
index(old-no UI).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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
</html>
<script>
//#region Helper Functions
const playsenum =
{
Rock: 'rock',
Paper: 'paper',
Scissors: 'scissors'
}
let plays = [playsenum.Rock, playsenum.Paper, playsenum.Scissors];
function random(number){
let res = Math.floor(Math.random()*number);
return res;
}
function assert(condition, message) {
if (!condition) {
message = message || "Assertion failed";
alert(message);
if (typeof Error !== "undefined") {
throw new Error(message);
}
throw message; // Fallback
}
}
//#endregion
//#region Main Functions
//get the human choice
function getHumanPlay(){
let ans = "", promptMsg = "Rock, Paper, or scissors?";
let shouldContinue = false;
while (shouldContinue == false){
ans = prompt(promptMsg);
if (ans.match(/^\s*scissor\s*$|^\s*rock\s*$|^\s*paper\s*$/)) {
shouldContinue = true;
}
promptMsg = `${ans} is not a valid option. Type "rock", "paper", or "scissors".`;
}
return ans.trim();
}
//computer picks an option
function computerPlay () {
return plays[random(plays.length)];
}
//play one round
function playOneRound (playerSelection, computerSelection){
let msg, winningChoice, losingChoice, outcome = "";
switch (playerSelection.toLowerCase()) {
case playsenum.Rock:
if (computerSelection === playsenum.Scissors) {
msg = "You win! :)";
winningChoice = playerSelection;
losingChoice = computerSelection;
}
else if (computerSelection === playsenum.Paper) {
msg = "You lose :(!";
winningChoice = computerSelection;
losingChoice = playerSelection;
}
else if (playerSelection.toLowerCase() == computerSelection.toLowerCase()) {
msg = `Looks like a tie. You both chose ${playerSelection}`;
outcome = "tie";
}
break;
case playsenum.Paper:
if (computerSelection === playsenum.Rock) {
msg = "You win! :)";
winningChoice = playerSelection;
losingChoice = computerSelection;
}
else if (computerSelection === playsenum.Scissors) {
msg = "You lose :(!";
winningChoice = computerSelection;
losingChoice = playerSelection;
}
else if (playerSelection.toLowerCase() == computerSelection.toLowerCase()) {
msg = `Looks like a tie. You both chose ${playerSelection}`;
outcome = "tie";
}
break;
case playsenum.Scissors:
if (computerSelection === playsenum.Paper) {
msg = "You win! :)";
winningChoice = playerSelection;
losingChoice = computerSelection;
}
else if (computerSelection === playsenum.Rock) {
msg = "You lose :(!";
winningChoice = computerSelection;
losingChoice = playerSelection;
}
else if (playerSelection.toLowerCase() == computerSelection.toLowerCase()) {
msg = `Looks like a tie. You both chose ${playerSelection}`;
outcome = "tie";
}
break;
}
return (outcome != 'tie') ? `${msg}.\n${winningChoice} beats ${losingChoice}` : msg;
}
//Returns a string showing the result of game
function getGameResult(score, roundsPlayed){
//positive score means player won by score amount of games. Negative means computer and 0 means tie.
alert(`final score: ${score} and roundsPlayed: ${roundsPlayed}`);
assert(Math.abs(score) <= roundsPlayed, "The score must be less than or equal to roundsPlayed");
let gameResult, playerScore, computerScore, scoreMessage;
if (score == 0) {
scoreMessage = 'Looks like we have a tie!';
}
else if (score > 0){
scoreMessage = 'Humanity prevails THIS time';
}
else {
scoreMessage = 'Was there ever really ANY doubt about machine superiority?';
}
playerScore = (roundsPlayed + score) /2;
computerScore = roundsPlayed - playerScore
return `Result of ${roundsPlayed} Round Game:\n\nYou won ${playerScore} games.\nThe computer won ${computerScore} games.\n\n${(scoreMessage)}`
}
//The main function that plays a game.
function game(roundsPlayed) {
let score = 0;
for (let index = 0; index < parseInt(roundsPlayed); index++) {
// alert(`score: ${score}\nindex; ${index}\nroundsPlayed: ${roundsPlayed}\n index < roundsPlayed: ${index < roundsPlayed}`);
let result = playOneRound(getHumanPlay(),computerPlay());
alert(`${result}`);
if (result.match('win')) {
score += 1;
//alert('won');
}
else if (result.match('lose')) {
score -= 1;
//alert('lost');
}
else {
score += 0;
//alert('tie');
}
}
return getGameResult(score, roundsPlayed);
}
//#endregion
alert(game(5));
</script>