-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrps.js
148 lines (135 loc) · 5.05 KB
/
rps.js
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
let lastWin = "";
let lastLoss = "";
let lastTie = '';
let playerWins = 0;
let computerWins = 0;
let playerLoss = 0;
let computerLoss = 0;
let ties = 0;
let numberOfGames = 0;
let currentStatus = "";
const messageBox = document.querySelector('.message-box');
const weaponButton = document.querySelector('.buttons-card');
const finalMessage = document.querySelector('.final-results');
const results = document.querySelector('.rounds');
const playerWinCounter = document.querySelector('.player-wins');
const ComputerWinCounter = document.querySelector('.computer-wins');
const tieCounter = document.querySelector('.ties');
const roundCounter = document.querySelector('.round-count');
messageBox.textContent = 'Choose your weapon';
//function that randomly choses from Rock, Paper or Scissor and returns them as a string
function getComputerChoice() {
//Math.random code to get an random integer between a maximum value and minimum value Math.floor(Math.random()*(maximum - minimum + 1) + minimum)
const choiceNumber = Math.floor(Math.random() * (3 - 1 + 1) + 1);
if (choiceNumber === 3) {
return "Rock";
} else if (choiceNumber === 2) {
return "Paper";
} else {
return "Scissors";
}
}
// function for updating wins and loss
const roundStatus = (status) => {
if (status === "playerWin") {
lastWin = "Player";
lastLoss ="Computer";
lastTie ='';
computerLoss++;
playerWins++;
} else if (status === "playerLose") {
lastWin = "Computer";
lastLoss ="Player";
lastTie = '';
playerLoss++;
computerWins++;
}
}
// function to play game by taking two parameters
function playRound(playerSelection, computerSelection) {
const playerChoiceLowerCase = playerSelection.toLowerCase();
const computerChoiceLowerCase = computerSelection.toLowerCase();
// generate proper case of player selection
const playerChoiceProperCase = playerChoiceLowerCase.slice(0,1).toUpperCase() + playerChoiceLowerCase.slice(1);
// winning and losing message
const winningMessage = `You win!! ${playerChoiceProperCase} beats ${computerSelection}.`;
const losingMessage = `You lose!! ${computerSelection} beats ${playerChoiceProperCase}.`;
// switch statement checks if the player choice is a valid choice and proceed with game if it is else returns a string saying that the choice is invalid
switch (playerChoiceLowerCase) {
case "rock":
case "paper":
case "scissors":
// checks if player choice and computer choice is same or not before proceeding
if (playerChoiceLowerCase === computerChoiceLowerCase) {
ties++;
lastTie = "Tied";
lastWin = lastLoss = '-';
return "It's a tie!";
} else if (playerChoiceLowerCase === "rock") {
if (computerChoiceLowerCase === "scissors") {
roundStatus("playerWin");
return winningMessage;
} else {
roundStatus("playerLose");
return losingMessage;
}
} else if (playerChoiceLowerCase === "paper") {
if (computerChoiceLowerCase === "rock") {
roundStatus("playerWin");
return winningMessage;
} else {
roundStatus("playerLose");
return losingMessage;
}
} else if (computerChoiceLowerCase === "paper") {
roundStatus("playerWin");
return winningMessage;
} else {
roundStatus("playerLose");
return losingMessage;
}
default:
return "Wrong weapon";
}
}
//funtion to record the current game status out of all the rounds
function currentGameStatus() {
switch (true) {
case (playerWins > computerWins):
currentStatus = `Player Wins with ${playerWins} Wins, ${playerLoss} Loss and ${ties} Ties in a total of ${numberOfGames} rounds.`;
break;
case (playerWins < computerWins):
currentStatus = `Computer Wins with ${computerWins} Wins, ${computerLoss} Loss and ${ties} Ties in a total of ${numberOfGames} rounds.`;
break;
case (playerWins === computerWins):
currentStatus = `Player is tied with the computer with ${playerWins} Wins, ${playerLoss} Loss and ${ties} Ties out of ${numberOfGames} Rounds.`;
break;
default:
currentStatus = "";
}
}
function addRoundsList() {
roundCounter.textContent = `Number of Rounds: ${numberOfGames}`;
ComputerWinCounter.textContent = `Computer Wins: ${computerWins}`;
playerWinCounter.textContent = `Player Wins: ${playerWins}`;
tieCounter.textContent = `Number of Ties: ${ties}`;
}
weaponButton.addEventListener('click', (event)=> {
let target = event.target;
if ((target.id === 'rock'
|| target.id === 'paper'
|| target.id === 'scissors')
&& (playerWins < 5
&& computerWins < 5)) {
let roundResult = playRound(target.id, getComputerChoice());
messageBox.textContent = roundResult;
numberOfGames++;
addRoundsList();
}
if ((playerWins >= 5
|| computerWins >= 5)) {
messageBox.textContent = 'Reset the page to restart the game';
currentGameStatus();
finalMessage.textContent = currentStatus;
}
});