-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameLogic.js
More file actions
80 lines (71 loc) · 3.11 KB
/
gameLogic.js
File metadata and controls
80 lines (71 loc) · 3.11 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
/*
Author: Aryan
Date: 8/1/2024
Time: 00-51-07
About: Stone Paper Scissor using Javascript Console..
*/
// Number of times game will continue..
const playTimes = 5;
let playerWin = 0, computerWin = 0, matchDraw = 0;
for(let startGameCount = 1; startGameCount <= playTimes; startGameCount++){
let invalidInput = 0;
function playRound(playerSelection, computerSelection) {
if(playerSelection.toLowerCase() != "rock" &&
playerSelection.toLowerCase() != "paper" &&
playerSelection.toLowerCase() != "scissors"){
invalidInput++;
return "Invalid Choice! Try again..";
}
if(playerSelection.toLowerCase() === "rock" && computerSelection === "scissors"
|| playerSelection.toLowerCase() === "paper" && computerSelection === "rock"
|| playerSelection.toLowerCase() === "scissors" && computerSelection === "paper"){
playerWin++;
return youWon(playerSelection, computerSelection);
} else if(playerSelection.toLowerCase() === "rock" && computerSelection === "rock"
|| playerSelection.toLowerCase() === "paper" && computerSelection === "paper"
|| playerSelection.toLowerCase() === "scissors" && computerSelection === "scissors"){
matchDraw++;
return itsTie();
} else{
computerWin++;
return youLose(playerSelection, computerSelection);
}
}
function youWon(playerSelection, computerSelection){
return "You Won! "+playerSelection+" beats "+computerSelection;
}
function youLose(playerSelection, computerSelection){
return "Oops! You Lost. "+computerSelection+" beats "+playerSelection;
}
function itsTie(){
return "It's a Tie! Both Wins...";
}
function showDetails(){
if(playerWin > computerWin){
return "You are Winner..\nMatch statistics:\nYou Won: "+playerWin+" times"+" Computer Won: "+computerWin+" times"+" Match Draw: "+matchDraw+" times";
}else if(playerWin === computerWin){
return "Match is Tie..\nMatch statistics:\nYou Won: "+playerWin+" times"+" Computer Won: "+computerWin+" times"+" Match Draw: "+matchDraw+" times";
}else{
return "You Lost..\nMatch statistics:\nYou Won: "+playerWin+" times"+" Computer Won: "+computerWin+" times"+" Match Draw: "+matchDraw+" times";
}
}
function getComputerChoice(){
// This will generate random number from 1 - 3
const computerChoice = Math.floor(Math.random() * 3) + 1;
if(computerChoice == 1){
return "rock";
}else if(computerChoice == 2){
return "paper";
}else{
return "scissors";
}
}
const playerSelection = prompt("Select Your Choice: ");
const computerSelection = getComputerChoice();
console.log(playRound(playerSelection, computerSelection));
console.log("You Won: "+playerWin+"\nComputer won: "+computerWin+"\nMatch draw: "+matchDraw);
startGameCount = startGameCount - invalidInput;
if(startGameCount == 5){
console.log(showDetails());
}
}