-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrockpaperscissors.js
106 lines (88 loc) · 3.38 KB
/
rockpaperscissors.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
let userScore = 0
let computerScore = 0
let gameRound = 0
const listOutcome = document.querySelector("ol")
function getComputerChoice() {
let randomChoice = Math.floor(Math.random() * 3)
if (randomChoice === 0) {
return "rock"
} else if (randomChoice === 1) {
return "paper"
} else {
return "scissors"
}
}
function playRound(humanChoice) {
const choiceArray = ["rock", "paper", "scissors"]
let computerChoice = getComputerChoice()
if (choiceArray.indexOf(humanChoice) === -1) {
console.log("Invalid Choice, Please Try Again")
} else {
let humanChoiceIndex = choiceArray.indexOf(humanChoice)
let computerChoiceIndex = choiceArray.indexOf(computerChoice)
if (humanChoiceIndex == computerChoiceIndex) {
gameRound += 1
return `It's a tie!, both you and computer picked ${humanChoice}`
} else if ((humanChoiceIndex + 1) % choiceArray.length == computerChoiceIndex) {
computerScore += 1
gameRound += 1
return `You lost! you picked ${humanChoice}, while computer picked ${computerChoice}`
} else if ((computerChoiceIndex + 1) % choiceArray.length == humanChoiceIndex) {
userScore += 1
gameRound += 1
return `You won! you picked ${humanChoice}, while computer picked ${computerChoice}`
}
}
}
const results = document.querySelector("#results")
const buttons = document.querySelectorAll("#buttons button");
const [btnRock, btnPaper, btnScissors] = buttons;
btnRock.addEventListener("click", () => {
const roundOutcome = document.createElement("li");
roundOutcome.textContent = playRound("rock");
listOutcome.appendChild(roundOutcome);
updateScores();
})
btnPaper.addEventListener("click", () => {
const roundOutcome = document.createElement("li");
roundOutcome.textContent = playRound("paper");
listOutcome.appendChild(roundOutcome);
updateScores();
})
btnScissors.addEventListener("click", () => {
const roundOutcome = document.createElement("li");
roundOutcome.textContent = playRound("scissors");
listOutcome.appendChild(roundOutcome);
updateScores();
})
const btnStart = document.querySelector("#btnStart");
const overallResults = document.querySelector("#overallResults");
const updateScores = () => {
if (gameRound % 5 == 0 && gameRound > 0) {
if (userScore > computerScore) {
document.querySelector("#overallResults").textContent = "You Win!"
} else if (userScore < computerScore) {
document.querySelector("#overallResults").textContent = "You Lose!"
} else {
document.querySelector("#overallResults").textContent = "It's a tie!"
}
btnStart.style.display = "block"
btnRock.disabled = true;
btnPaper.disabled = true;
btnScissors.disabled = true;
}
let resultString = `User Score : ${userScore}\tComputer Score : ${computerScore}`
results.textContent = resultString
}
btnStart.addEventListener("click", () => {
btnStart.style.display = "none"
btnRock.disabled = false;
btnPaper.disabled = false;
btnScissors.disabled = false;
gameRound = 0;
userScore = 0;
computerScore = 0;
results.textContent = "Game started! Choose your move.";
overallResults.textContent = "";
listOutcome.textContent = "";
})