-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleGame.js
More file actions
97 lines (86 loc) · 3.24 KB
/
Copy pathsimpleGame.js
File metadata and controls
97 lines (86 loc) · 3.24 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
function computerChoice() {
const possibleChoices = ['rock', 'scissors', 'paper'];
return possibleChoices[Math.floor(Math.random() * possibleChoices.length)];
}
let roundCounter = 1;
function playerChoice() {
let playerDecision = prompt(`Round ${roundCounter}\nMake your choice! Type Rock, Paper or Scissors.`);
try {
return playerDecision.trim().toLowerCase();
} catch (error) {
console.log('Sorry to see you leave!');
}
}
let gamesWonByPlayer = 0;
let gamesWonByComputer = 0;
function playSingleRound(playerSelection, computerSelection) {
let roundResultMessage = '';
let round = 1;
while (round > 0) {
computerSelection = computerChoice();
playerSelection = playerChoice();
if (playerSelection === null || playerSelection === undefined) {
break;
} else if (playerSelection === computerSelection) {
roundResultMessage = 'Game-draw! Play again: Type Rock, Paper or Scissors.';
roundCounter++;
} else if (playerSelection === 'rock') {
if (computerSelection === 'paper') {
roundResultMessage = 'You Lose the Round! Paper beats Rock';
gamesWonByComputer++;
roundCounter++;
} else {
roundResultMessage = `You won the Round! ${playerSelection} beats ${computerSelection}!`;
gamesWonByPlayer++;
roundCounter++;
}
} else if (playerSelection === 'paper') {
if (computerSelection === 'scissors') {
roundResultMessage = 'You Lose the Round! Scissors beats Paper';
gamesWonByComputer++;
roundCounter++;
} else {
roundResultMessage = `You won the Round! ${playerSelection} beats ${computerSelection}!`;
gamesWonByPlayer++;
roundCounter++;
}
} else if (playerSelection === "scissors") {
if (computerSelection === "rock") {
roundResultMessage = 'You Lose the Round! Rock beats Scissors';
gamesWonByComputer++;
roundCounter++;
} else {
roundResultMessage = `You won the Round! ${playerSelection} beats ${computerSelection}!`;
gamesWonByPlayer++;
roundCounter++;
}
} else {
console.log('Invalid choice.Make another choice...');
continue;
}
round--;
}
return roundResultMessage;
}
function printResult() {
console.log('Your score: ' + gamesWonByPlayer);
console.log('Computer score: ' + gamesWonByComputer);
if (gamesWonByPlayer > gamesWonByComputer) {
console.log('Congratulations! You won the game!');
} else if (gamesWonByPlayer === gamesWonByComputer) {
console.log('No winner. Game-draw!');
} else {
console.log('Sorry you lost! Computer won the game.');
}
}
function playFiveRoundsGame() {
alert(`Welcome!\nThis game has five rounds. Click \"OK"\ to continue`);
for (let i = 1; i <= 5; i++) {
// if (playSingleRound() === '') {
// break;
// }
console.log(playSingleRound());
}
printResult();
}
playFiveRoundsGame();