-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
141 lines (125 loc) · 4.65 KB
/
script.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
let playerScore = 0;
let computerScore = 0;
let roundPlayer = '';
function computerPlay() {
const items = ['Rock', 'Paper', 'Scissors'];
return items[Math.floor(Math.random() * items.length)];
}
function playRound(playerSelection, computerSelection) {
if (
playerSelection === 'Rock' && computerSelection === 'Scissors' ||
playerSelection === 'Paper' && computerSelection === 'Rock' ||
playerSelection === 'Scissors' && computerSelection === 'Paper'
) {
playerScore++;
roundPlayer = 'player';
return `${playerSelection} beats ${computerSelection}.`;
} else if (
computerSelection === 'Rock' && playerSelection === 'Scissors' ||
computerSelection === 'Paper' && playerSelection === 'Rock' ||
computerSelection === 'Scissors' && playerSelection === 'Paper'
) {
computerScore++;
roundPlayer = 'computer';
return `${playerSelection} is beaten by ${computerSelection}.`;
} else if (
playerSelection === computerSelection
) {
roundPlayer = 'tie';
return `You and computer chose ${computerSelection}.`;
}
}
// Connections to HTML page
const scoreInfo = document.querySelector('.scoreinfo');
const scoreMessage = document.querySelector('.scoremessage');
const pcChoice = document.querySelector('.pcsign');
const playerChoice = document.querySelector('.playersign');
const pcScore = document.querySelector('.pcscore');
const userScore = document.querySelector('.playerscore');
const btnRock = document.querySelector('.rock');
const btnPaper = document.querySelector('.paper');
const btnScissors = document.querySelector('.scissors');
const overlay = document.getElementById('overlay');
const modal = document.querySelector('.modal');
const btnRestart = document.querySelector('#restart');
const winnerPara = document.querySelector('.winnerpara');
function setWinner() {
if (roundPlayer === 'player') {
return 'You won!';
} else if (roundPlayer === 'computer'){
return 'Computer won!';
} else {
return 'It\'s a tie';
}
}
function changeChosenSigns(playerSelection, computerSelection) {
switch (playerSelection) {
case "Rock":
playerChoice.setAttribute('src', './img/rock-hand.png');
break;
case "Paper":
playerChoice.setAttribute('src', './img/paper-hand.png');
break;
case "Scissors":
playerChoice.setAttribute('src', './img/scissors-hand.png');
break;
}
switch (computerSelection) {
case "Rock":
pcChoice.setAttribute('src', './img/rock-hand.png');
break;
case "Paper":
pcChoice.setAttribute('src', './img/paper-hand.png');
break;
case "Scissors":
pcChoice.setAttribute('src', './img/scissors-hand.png');
break;
}
}
function decideWhoWon() {
if(playerScore > computerScore) {
return 'You Won'
} else {
return 'You Lost'
}
}
function removeWinnerWindow() {
overlay.style.display = 'none';
modal.classList.remove('active');
}
function resetGame() {
scoreInfo.textContent = 'Choose your weapon';
scoreMessage.textContent = 'First to score 5 points wins the game';
playerChoice.setAttribute('src', 'img/question-mark.png');
pcChoice.setAttribute('src', 'img/question-mark.png');
userScore.textContent = 'Player: 0';
pcScore.textContent = 'Computer: 0';
playerScore = 0;
computerScore = 0;
removeWinnerWindow();
}
// Opens a window who won and asks to restart the game
function stateAbsoluteWinner() {
overlay.style.display = 'block';
modal.classList.add('active')
winnerPara.textContent = decideWhoWon();
btnRestart.addEventListener('click', resetGame);
overlay.addEventListener('click', removeWinnerWindow)
}
// Runs main logic of this app
function play(playerSelection) {
if (playerScore < 5 && computerScore < 5) {
let computerSelection = computerPlay();
scoreMessage.textContent = playRound(playerSelection, computerSelection);
scoreInfo.textContent = setWinner();
changeChosenSigns(playerSelection, computerSelection);
pcScore.textContent = `Computer: ${computerScore}`;
userScore.textContent = `Player: ${playerScore}`;
}
if (playerScore >= 5 || computerScore >= 5) {
stateAbsoluteWinner();
}
}
btnRock.addEventListener('click', () => {play('Rock')});
btnPaper.addEventListener('click', () => {play('Paper')});
btnScissors.addEventListener('click', () => {play('Scissors')});