-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
150 lines (117 loc) · 3.59 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
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
148
149
150
const values = ["rock", "paper", "scissors"];
let playerCounter = 0;
let computerCounter = 0;
const playerElection = document.getElementById('player-choice');
const computerElection = document.getElementById('computer-choice');
const playerScore = document.getElementById('player-score');
const computerScore = document.getElementById('computer-score');
const player = document.getElementById('player');
const computer = document.getElementById('computer');
const roundResult = document.getElementById('round-result');
const winnerSection = document.getElementsByClassName('winner-section')
const finalWinner = document.getElementById('final-winner');
const body = document.getElementById('body')
const buttons = [...document.querySelectorAll('.button')];
const playButton = document.getElementById('play');
const restartButton = document.getElementById('restart')
choices = [playerElection, computerElection]
function computerPlay() {
let computerSelection = values[Math.floor(Math.random() * values.length)];
computer.textContent = computerSelection
return computerSelection;
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection) {
return "Tie round";
}
if (playerSelection === "paper" && computerSelection === "rock") {
playerCounter++;
return "Player wins! Paper beats Rock";
}
if (playerSelection == "scissors" && computerSelection === "paper") {
playerCounter++;
return "Player wins! Scissors beats Paper";
}
if (playerSelection === "rock" && computerSelection === 'scissors') {
playerCounter++;
return "Player wins! Rock beats Scissors";
}
computerCounter++;
return "Computer wins!"
}
function displayScore() {
playerScore.textContent = playerCounter;
computerScore.textContent = computerCounter;
}
function changingDisplay(display, args) {
let elements = Array.from(args).flat();
elements.forEach((element) => {
element.style.display = display;
});
}
function hideElement(...args) {
changingDisplay('none', args);
}
function showElement(...args) {
changingDisplay('block', args);
}
function displayRound(playerChoice, roundResults) {
showElement(choices);
player.textContent = playerChoice;
roundResult.textContent = roundResults;
displayScore()
}
function displayWinner(winner) {
hideElement(choices, body, buttons);
showElement(finalWinner, restartButton);
finalWinner.textContent = winner + ' won the game!';
}
function startGame() {
return () => {
clearCounters()
clearDisplay()
hideElement(choices, finalWinner, restartButton, playButton);
showElement(buttons, body);
};
}
function makeMovement(button) {
return () => {
playerChoice = button.id;
computerChoice = computerPlay();
roundResults = playRound(playerChoice, computerChoice);
displayRound(playerChoice, roundResults)
checkCounter();
};
}
function checkCounter() {
if (playerCounter == 3) {
endGame('You');
}
if (computerCounter == 3) {
endGame('Computer');
}
}
function endGame(winner) {
displayWinner(winner)
}
function restartGame() {
startGame();
}
function clearCounters() {
playerCounter = 0;
computerCounter = 0;
displayScore()
}
function clearDisplay() {
player.textContent = ''
computer.textContent = ''
roundResult.textContent = ''
}
hideElement(body, restartButton);
playButton.addEventListener('click', startGame());
restartButton.addEventListener('click', startGame());
buttons.forEach((button) => {
button.style.display = 'none'
button.addEventListener('click', makeMovement(button));
});
// module.exports = computerPlay, playRound;