-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
150 lines (122 loc) · 4.82 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
142
143
144
145
146
147
148
149
150
const root = document.querySelector("#root");
//appends buttons to root
const buttonContainer = document.createElement('div');
buttonContainer.classList.add('buttonContainer');
buttonContainer.classList.add('load');
const rock = document.createElement('img');
rock.src = "images/rock.png";
rock.alt = "Image of a rock";
const paper = document.createElement('img');
paper.src = "images/paper.png";
paper.alt = "Image of a paper";
const scissors = document.createElement('img');
scissors.src = "images/scissors.png";
scissors.alt = "Image of scissors";
const moveSelection = [rock, scissors, paper];
rock.textContent = 'Rock';
scissors.textContent = 'Scissors';
paper.textContent = 'Paper'
moveSelection.forEach((move) => {
buttonContainer.appendChild(move);
})
root.appendChild(buttonContainer);
// int representations of player scores
let playerScore = 0;
let compScore = 0;
// node that contains the scores
const scoreKeeper = document.createElement('div');
scoreKeeper.classList.add("scoreBox");
scoreKeeper.classList.add("load");
// nodes that represent player and comp scores
const trackPlayer = document.createElement('p');
const trackComp = document.createElement('p');
// shows the scores as an initial zero
trackPlayer.textContent = "Player: " + playerScore.toString();
trackComp.textContent = "Computer: " + compScore.toString();
// refresh button when either side wins
const refreshButton = document.createElement('button');
refreshButton.addEventListener('click', () => {
location.reload();
})
refreshButton.classList.add("refresh-button");
refreshButton.innerHTML = "↻"
//append the player and comp scores and refresh button as children to scoreKeeper container node
scoreKeeper.appendChild(trackPlayer);
scoreKeeper.appendChild(refreshButton);
scoreKeeper.appendChild(trackComp);
const verdictMessage = document.createElement('p');
verdictMessage.textContent = 'First to five points wins! Are you ready? Make a move to start!';
verdictMessage.id = "verdictMessage";
verdictMessage.classList.add('load');
root.insertBefore(verdictMessage, buttonContainer);
//append score container to root container
root.insertBefore(scoreKeeper, buttonContainer);
moveSelection.forEach((move) => {
move.addEventListener('click', function (e) {
const verdict = playRound(e.target.textContent, getComputerChoice());
if (verdict.charAt(4) == 'w') {
playerScore++;
trackPlayer.textContent = "Player: " + playerScore.toString();
verdictMessage.textContent = "You won this round!";
checkWinner()
} else if (verdict.charAt(4) == 'l') {
compScore++;
trackComp.textContent = "Computer: " + compScore.toString();
verdictMessage.textContent = "You lost this round!";
checkWinner();
} else {
verdictMessage.textContent = "It's a tie! Make another move!";
}
});
});
const getComputerChoice = () => {
const choices = ['Rock', 'Paper', 'Scissors'];
const randomNumber = Math.floor(Math.random() * 3);
return choices[randomNumber];
}
const playRound = (playerSelection, computerSelection) => {
//make moves case insensitive
const playerMove = playerSelection.toLowerCase();
const compMove = computerSelection.toLowerCase();
//logic for determining winnerscis
if (playerMove === compMove) {
return "Tie! Play another round!";
} else if (playerMove === "rock" && compMove === "paper") {
return "You lose! Paper beats Rock!";
} else if (playerMove === "rock" && compMove === "scissors") {
return "You win! Rock beats Scissors!";
} else if (playerMove === "paper" && compMove === "rock") {
return "You win! Paper beats Rock!";
} else if (playerMove === "rock" && compMove === "scissots") {
return "You lose! Scissors beats Paper!";
} else if (playerMove === "scissors" && compMove === "rock") {
return "You lose! Rock beats Scissors!";
} else {
return "You win! Scissors beats Paper!";
}
}
//return string for verdict message
const checkWinner = () => {
if (playerScore == 5) {
verdictMessage.textContent = "You won the game! Refresh to play another!";
disableButtons();
refreshButton.style.visibility = "visible"
} else if (compScore == 5) {
verdictMessage.textContent = "You lost the game! Refresh to play another!";
disableButtons();
refreshButton.style.visibility = "visible"
}
}
//disable buttons
const disableButtons = () => {
const buttons = document.querySelectorAll('img');
buttons.forEach((button) => {
button.setAttribute('style', 'pointer-events: none;');
})
}
const enableButtons = () => {
const buttons = document.querySelectorAll('img');
buttons.forEach((button) => {
button.setAttribute('style', 'pointer-events: auto;');
})
}