-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrps_new.js
More file actions
57 lines (47 loc) · 1.5 KB
/
rps_new.js
File metadata and controls
57 lines (47 loc) · 1.5 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
hands = ['rock', 'paper', 'scissors'];
//returns a random value from hands array
function getHand() {
return hands[parseInt(Math.random()*10)%3];
}
playerOne = {
name: "Emily",
hand: getHand,
wins: 0
}
playerTwo = {
name: "Edwin",
hand: getHand,
wins: 0
}
function playRound(playerOne, playerTwo) {
// Gets hands from each
// Determines the winner
var playerOneHand = playerOne.hand();
var playerTwoHand = playerTwo.hand();
var winner;
if (playerOneHand === "rock" && playerTwoHand === "paper" || playerOneHand === "paper" && playerTwoHand === "scissors" || playerOneHand === "scissors" && playerTwoHand === "rock"){
//playerTwo wins
winner = playerTwo.name;
playerTwo.wins++;
} else if (playerOneHand !== playerTwoHand) {
//playerOne wins
winner = playerOne.name;
playerOne.wins++;
} else if (playerOneHand === playerTwoHand) {
//tie
// If its a tie, log the hands played and "it's a tie".
console.log("It's a tie!");
}
// Logs the hands played and name of the winner.
console.log("Emily: " + playerOneHand + ", Edwin: " + playerTwoHand + " --> WINNER: " + winner);
console.log("SCORE -- Emily:" + playerOne.wins + ", Edwin:" + playerTwo.wins);
// Returns the winner object (null if no winner)
return winner;
}
playRound(playerOne, playerTwo);
// function playGame(player1, player2, playUntil) {
// while (player1.wins && player2.wins < playUntil) {
// playRound(playerOne, playerTwo);
// }
// }
// playGame(playerOne, playerTwo, 5);