-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrock-paper-scissors.html
102 lines (81 loc) · 3.05 KB
/
rock-paper-scissors.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Rock Paper Scissors</title>
</head>
<body>
<script type="text/javascript">
function computerPlay() {
let computerArsenal = ['Rock', 'Paper', 'Scissors'];
let computerChoice = computerArsenal[Math.floor(Math.random()*computerArsenal.length)];
return(computerChoice);
}
function playerPlay() {
let isLegalWeapon = false;
let playerChoice;
do {
playerChoice = prompt("Choose your weapon: Rock, Paper, or Scissors?");
if (playerChoice === null) {
alert ('You forfeit. Computer wins!');
break;
} else if (playerChoice.toLowerCase() === 'rock' ||
playerChoice.toLocaleLowerCase() === 'paper' ||
playerChoice.toLocaleLowerCase() === 'scissors') {
isLegalWeapon = true;
} else {
alert('Illegal weapon! You must choose Rock, Paper, or Scissors!');
}
} while (isLegalWeapon === false);
return(playerChoice);
}
function capitalize(string) {
let firstLetter = string.slice(0, 1);
let stringLength = string.length;
let remainingLetters = string.slice(1, stringLength)
let lower = string.toLowerCase();
return(firstLetter.toUpperCase() + remainingLetters.toLowerCase());
}
function playRound(playerSelection, computerSelection) {
let player = playerSelection.toLowerCase();
let computer = computerSelection.toLowerCase();
console.log('Your weapon: ' + capitalize(player));
console.log('Computer\'s weapon: ' + computerSelection);
if (player === computer) {
return('It\'s a draw!');
} else if (
(player === 'rock' && computer === 'paper') ||
(player === 'paper' && computer === 'scissors') ||
(player === 'scissors' && computer === 'rock')) {
return ('You lose! ' + computerSelection + ' beats ' + player + '.');
} else {
return ('You win! ' + capitalize(player) + ' beats ' + computer + '.');
}
}
function game() {
let computerScore = 0;
let playerScore = 0;
for (roundCount = 0; roundCount < 5; roundCount++) {
console.log('Round: ' + (roundCount+1));
console.log('Choose your weapon!')
let roundResult = playRound(playerPlay(), computerPlay());
console.log(roundResult);
if (roundResult.indexOf('win') > 0) {
++playerScore;
} else if (roundResult.indexOf('lose') > 0) {
++computerScore;
}
console.log('Score: Player ' + playerScore + ' | ' + computerScore + ' Computer');
}
if (playerScore > computerScore) {
console.log('**You are the winner!**');
} else if (computerScore > playerScore) {
console.log('**You lost to the computer!**');
} else {
console.log('**It\'s a tie!**');
}
}
game();
</script>
</body>
</html>