-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
117 lines (101 loc) · 3.21 KB
/
main.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
const div = document.createElement("div");
const roundResults = document.querySelector(".round-results");
const rock = document.querySelector("#rock");
rock.addEventListener("click", () => {
game("rock", getComputerChoice());
});
const paper = document.querySelector("#paper");
paper.addEventListener("click", () => {
game("paper", getComputerChoice());
});
const scissors = document.querySelector("#scissors");
scissors.addEventListener("click", () => {
game("scissors", getComputerChoice());
});
const computerScore = document.querySelector(".computer-score");
const playerScore = document.querySelector(".player-score");
function getComputerChoice() {
let choices = ["rock", "paper", "scissors"];
let randomIndex = Math.floor(Math.random() * choices.length);
return choices[randomIndex];
}
function getPlayerChoice() {
let playerChoice = prompt("Enter your move choice:").toLowerCase();
return checkPlayerChoice(playerChoice) ? playerChoice : getPlayerChoice();
}
function checkPlayerChoice(playerChoice) {
if (
playerChoice == "rock" ||
playerChoice == "paper" ||
playerChoice == "scissors"
) {
return true;
} else {
return false;
}
}
function playRound(playerSelection, computerSelection) {
let roundWinner = "";
if (playerSelection == computerSelection) {
div.textContent = "It's a Draw!";
roundResults.appendChild(div);
roundWinner = "draw";
} else if (playerSelection == "rock") {
if (computerSelection == "scissors") {
div.textContent = "You Win! Rock beats Scissors.";
roundResults.appendChild(div);
roundWinner = "player";
} else {
div.textContent = "You Lose! Paper beats Rock.";
roundResults.appendChild(div);
roundWinner = "computer";
}
} else if (playerSelection == "scissors") {
if (computerSelection == "paper") {
div.textContent = "You Win! Scissors beats Paper.";
roundResults.appendChild(div);
roundWinner = "player";
} else {
div.textContent = "You Lose! Rock beats Scissors.";
roundResults.appendChild(div);
roundWinner = "computer";
}
} else if (playerSelection == "paper") {
if (computerSelection == "rock") {
div.textContent = "You Win! Paper beats Rock.";
roundResults.appendChild(div);
roundWinner = "player";
} else {
div.textContent = "You Lose! Scissors beats Paper.";
roundResults.appendChild(div);
roundWinner = "computer";
}
}
updateScore(roundWinner);
if (checkGameStatus() == 0) {
div.textContent = "The Player wins.";
return;
} else if (checkGameStatus() == 1) {
div.textContent = "The Computer wins.";
return;
}
}
function game(playerSelection, computerSelection) {
if (checkGameStatus() != 0 && checkGameStatus() != 1) {
playRound(playerSelection, computerSelection);
}
}
function updateScore(roundWinner) {
if (roundWinner == "player") {
playerScore.textContent = Number(`${playerScore.textContent}`) + 1;
} else if (roundWinner == "computer") {
computerScore.textContent = Number(`${computerScore.textContent}`) + 1;
}
}
function checkGameStatus() {
if (playerScore.textContent == 5) {
return 0;
} else if (computerScore.textContent == 5) {
return 1;
}
}