-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
165 lines (152 loc) · 4.85 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
console.log("welcome to the deathmatch!");
let playerScore = 0;
let computerScore = 0;
let playerScoreboard = document.querySelector(".playerScoreboard");
let npcScoreboard = document.querySelector(".computerScoreboard");
let roundResult = document.querySelector(".roundResult");
const rock = document.querySelector(".rock");
const paper = document.querySelector(".paper");
const scissor = document.querySelector(".scissor");
const restart = document.querySelector(".reset");
/*
** playerChoice() will get the users selection and return warrior, mage, or archer
**
** arg(s): none
** return(s): string type fighter; ex. 'rock'
*/
function playerChoice(e) {
let choice = e.target.value;
return choice;
}
/*
** computerChoice() will randomly generate a number to pick and return 1 of the 3 options
**
** arg(s): none
** return(s): string type figher; ex. 'paper'
*/
function computerChoice() {
let fighters = ["rock", "paper", "scissor"];
let computerChoice = Math.floor(Math.random() * 3);
//console.log(fighters[computerChoice]);
return fighters[computerChoice];
}
/*
** playRound() will call the helper function battle() returns the winner.
**
**
** arg(s): none
** return(s): none
*/
function playRound(e) {
if (playerScore >= 5 || computerScore >= 5) {
alert("Play a new game by pressing the Start Over button.");
} else {
let fighter1 = playerChoice(e);
let fighter2 = computerChoice();
document
.querySelector(".fighter1")
.setAttribute(
"style",
`background-image: url("./images/${fighter1}.jpg")`
);
document
.querySelector(".fighter2")
.setAttribute(
"style",
`background-image: url("./images/${fighter2}.jpg")`
);
let winner = battle(fighter1, fighter2);
if (winner === 1) {
playerScore++;
playerScoreboard.textContent = playerScore;
}
if (winner === 2) {
computerScore++;
npcScoreboard.textContent = computerScore;
}
console.log(`player:${playerScore}; computer:${computerScore}`);
checkWinner(winner);
}
}
/*
** battle(fighter1, fighter2) take the computer and players selection as
** arguments and determines the winner of the round.
** Warrior > Archer
** Archer > Mage
** Mage > Warrior
**
** arg(s): strings type 'fighter1' & 'fighter2'
** return(s): number type 'winner'; number 1 correlate to the player while
** number 2 will represent the computer
*/
function battle(fighter1, fighter2) {
let winner = 0;
if (fighter1 === fighter2) {
roundResult.textContent = "tie";
} else if (fighter1 === "rock" && fighter2 === "scissor") {
roundResult.textContent = "player 1's rock smashes the flimsy scisssor";
winner = 1;
} else if (fighter1 === "scissor" && fighter2 === "paper") {
roundResult.textContent = "player 1's scissor rips the paper to shreds";
winner = 1;
} else if (fighter1 === "paper" && fighter2 === "rock") {
roundResult.textContent =
"player 1's paper wraps the rock and crushes it to pebbles";
winner = 1;
} else if (fighter2 === "rock" && fighter1 === "scissor") {
roundResult.textContent = "computer's rock smashes the flimsy scisssor";
winner = 2;
} else if (fighter2 === "scissor" && fighter1 === "paper") {
roundResult.textContent = "computer's scissor rips the paper to shreds";
winner = 2;
} else if (fighter2 === "paper" && fighter1 === "rock") {
roundResult.textContent =
"computer's paper wraps the rock and crushes it to pebbles";
winner = 2;
}
return winner;
}
/*
** checkWinner() will see if someone is first to 5 wins
**
** arg(s): number type 'winner'; 0 is a tie, 1 will be the user, 2 will be the computer
** return(s): none
*/
//this function will check if game is over with the first one to 5 wins
function checkWinner(winner) {
if (playerScore === 5 || computerScore === 5) {
roundResult.textContent = `The winner is ${
winner == 1 ? "Human" : "Computer"
}`;
}
}
/*
** checkWinner() will see if someone is first to 5 wins
**
** arg(s): number type 'winner'; 0 is a tie, 1 will be the user, 2 will be the computer
** return(s): none
*/
//this function will check if game is over with the first one to 5 wins
function reset() {
playerScore = 0;
computerScore = 0;
playerScoreboard.textContent = playerScore;
npcScoreboard.textContent = computerScore;
document
.querySelector(".fighter1")
.removeAttribute(
"style",
`background-image: none)`
);
document
.querySelector(".fighter2")
.removeAttribute(
"style",
`background-image: none)`
);
roundResult.textContent = "Another war has been declared!";
}
scissor.addEventListener("click", playRound);
paper.addEventListener("click", playRound);
rock.addEventListener("click", playRound);
restart.addEventListener("click", reset);