-
Notifications
You must be signed in to change notification settings - Fork 0
/
rock-paper-scissors.js
158 lines (122 loc) · 4.27 KB
/
rock-paper-scissors.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
151
152
153
154
155
//gameplay variables
let compScore = 0;
let playerScore = 0;
let roundCount = 0;
const weapons = ['scissors', 'paper', 'rock'];
const scissors = document.querySelector('#scissors');
const rock = document.querySelector('#rock');
const paper = document.querySelector('#paper');
//html manipulation variables
const playerScoreCard = document.querySelector('#playerScore');
const compScoreCard = document.querySelector('#compScore');
const commentaryContainer = document.querySelector('.commentary-container');
const gameResult = document.createElement('div');
gameResult.style.cssText = 'font-size: 48px;';
// generate random choice for computer
function getComputerChoice() {
let computerChoice = weapons[Math.floor(Math.random() * 3)];
return computerChoice;
}
// reset game
function resetGame() {
roundCount = 0;
playerScore = 0;
compScore = 0;
playerScoreCard.textContent = playerScore;
compScoreCard.textContent = compScore;
while (commentaryContainer.hasChildNodes) {
commentaryContainer.removeChild(commentaryContainer.lastChild);
}
}
//generate game mechanics
function play(player, computer) {
roundCount += 1;
if (player == 'scissors') {
if (computer == 'scissors') {
return 'Tie! Your scissors are unable to vanquish each other!';
}
else if (computer == 'paper') {
playerScore++;
return 'Win! Your scissors have sliced n diced their paper to shreds!'
}
else {
compScore++;
return 'Loss! Your scissors have been crushed by their rock'
}
}
else if (player == 'paper') {
if (computer == 'paper') {
return 'Tie! Your papers are unable to vanquish each other!';
}
else if (computer == 'rock') {
playerScore++;
return 'Win! Your paper has smothered their rock to fatality!'
}
else {
compScore++;
return 'Loss! Your paper was carved up by their scissors!'
}
}
else if (player == 'rock'){
if (computer == 'rock') {
return 'Tie! Your rocks are unable to vanquish each other!';
}
else if (computer == 'scissors') {
playerScore++;
return 'Win! Your rock has crushed their scissors to fatality!'
}
else {
compScore++;
return 'Loss! Your rock has been smothered by their paper!'
}
}
else {
return 'Your choices are: "rock" or "paper" or "scissors"! Pick one!'
}
}
//print game winner
function printGameWinner(playerScore, compScore) {
if (playerScore == compScore) {
return 'Damn ya\'ll tied! Run it again!!';
}
else if (playerScore > compScore) {
return 'You came! You conquered! Noice!'
}
else {
return 'Damn you really gon let a computer do you like this? :$';
}
}
function printGameWinner(playerScore, compScore) {
if (playerScore == compScore) {
return 'Damn ya\'ll tied! Run it again!!';
}
else if (playerScore > compScore) {
return 'You came! You conquered! Noice!'
}
else {
return 'Damn you really gon let a computer do you like this? :$';
}
}
//main game of 5 rounds
function game() {
if (roundCount >= 5) {
resetGame();
}
const computerSelection = getComputerChoice();
// printing game commentary placed here so it would create a running list instead of replacing old commentary.
const commentary = document.createElement('div');
commentary.classList.add('commentary');
commentary.textContent = `Round ${roundCount +1}: Computer chose ${computerSelection}!\n ${play(this.id, computerSelection)}`;
commentaryContainer.appendChild(commentary);
playerScoreCard.textContent = playerScore;
compScoreCard.textContent = compScore;
if (roundCount >= 5) {
gameResult.textContent = `Thats game! Your score is ${playerScore}! The computer's is ${compScore}! ${printGameWinner(playerScore, compScore)}`;
commentaryContainer.appendChild(gameResult);
}
// console.log('Computer chose ' + computerSelection);
// console.log(string);
}
scissors.addEventListener('click', game);
rock.addEventListener('click', game);
paper.addEventListener('click', game);