-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
177 lines (153 loc) · 5.7 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const playerScoreContainer = document.getElementById('player-score');
const computerScoreContainer = document.getElementById('computer-score');
const saberBtn = document.getElementById('player-saber-servant');
const lancerBtn = document.getElementById('player-lancer-servant');
const archerBtn = document.getElementById('player-archer-servant');
const compSaberBtn = document.getElementById('computer-saber-servant');
const compLancerBtn = document.getElementById('computer-lancer-servant');
const compArcherBtn = document.getElementById('computer-archer-servant');
const scoreboardDiv = document.getElementById('scoreboard');
const resultText = document.getElementById('result-text');
//score updates
let playerScore=0;
let computerScore=0;
function updatePlayerScore(){
playerScore++;
playerScoreContainer.innerHTML= playerScore;
}
function updateComputerScore(){
computerScore++;
computerScoreContainer.innerHTML=computerScore;
}
//listen for player choice
saberBtn.addEventListener('click', function(e){
if (playerScore >= 5 || computerScore >= 5) return;
removeAllBorders();
saberBtn.classList.add('clicked');
return playRound('Saber',getComputerChoice());
})
archerBtn.addEventListener('click', function(e){
if (playerScore >= 5 || computerScore >= 5) return;
removeAllBorders();
archerBtn.classList.add('clicked');
return playRound('Archer',getComputerChoice());
});
lancerBtn.addEventListener('click', function(e){
if (playerScore >= 5 || computerScore >= 5) return;
removeAllBorders();
lancerBtn.classList.add('clicked');
return playRound('Lancer',getComputerChoice());
});
//computer randomly chooses between saber archer or lancer
function getComputerChoice(){
let result= Math.floor(Math.random()*3)+1;
if(result==1){
removeCompBorders();
compSaberBtn.classList.add('clicked');
return "Saber";
}
else if (result==2){
removeCompBorders();
compArcherBtn.classList.add('clicked');
return "Archer";
}
else{
removeCompBorders();
compLancerBtn.classList.add('clicked');
return "Lancer";
}
}
//function for playing one round
function playRound(playerChoice, computerChoice){
//if its a tie
if (playerChoice === computerChoice){
draw();
}
//if player is the winner
else if ((playerChoice == "Saber") && (computerChoice =="Lancer")){
win(playerChoice, computerChoice);
}
else if ((playerChoice == "Lancer") && (computerChoice =="Archer")){
win(playerChoice, computerChoice);
}
else if((playerChoice == "Archer") && (computerChoice == "Saber")){
win(playerChoice, computerChoice);
}
//if computer is the winner
else if ((computerChoice =="Saber") && (playerChoice =="Lancer")){
lose(playerChoice, computerChoice);
}
else if ((computerChoice =="Lancer") && (playerChoice =="Archer")){
lose(playerChoice, computerChoice);
}
else if((computerChoice =="Archer") && (playerChoice =="Saber")){
lose(playerChoice, computerChoice);
}
}
//helper functions
function win(playerChoice, computerChoice ){
updatePlayerScore();
const para = document.createElement('p');
para.classList = 'result-item';
para.appendChild(document.createTextNode(`You win! ${playerChoice} killed ${computerChoice}!`));
resultText.after(para);
checkWinner();
}
function lose(playerChoice, computerChoice){
updateComputerScore();
const para = document.createElement('p');
para.classList = 'result-item';
para.appendChild(document.createTextNode(`Argh.. Computer Wins! ${computerChoice} killed ${playerChoice}!`));
resultText.after(para);
checkWinner();
}
function draw(){
const para = document.createElement('p');
para.classList = 'result-item';
para.appendChild(document.createTextNode("It's a tie! They're evenly matched!"));
resultText.after(para);
}
function checkWinner(){
if (playerScore >= 5){
const para = document.createElement('p');
para.classList = 'result-item-winner';
para.appendChild(document.createTextNode("Congratulations! You obtained the Holy Grail!"));
resultText.after(para);
const playAgainBtn = document.createElement('div');
playAgainBtn.classList = 'play-again';
playAgainBtn.appendChild(document.createTextNode('PLAY AGAIN'));
scoreboardDiv.after(playAgainBtn);
playAgainBtn.addEventListener('click', reloadPage);
}
if (computerScore >= 5){
const para = document.createElement('p');
para.classList = 'result-item-loser';
para.appendChild(document.createTextNode("You died. Computer wins the Holy Grail War."));
resultText.after(para);
const playAgainBtn = document.createElement('div');
playAgainBtn.classList = 'play-again';
playAgainBtn.innerHTML= 'PLAY AGAIN';
scoreboardDiv.after(playAgainBtn);
playAgainBtn.addEventListener('click', reloadPage);
}
}
function reloadPage(){
window.location.reload(true);
}
//animations
function removeAllBorders(){
const playerChoices = document.getElementsByClassName('player-choice');
const compChoices = document.getElementsByClassName('computer-choice');
for(let i=0; i< playerChoices.length;i++){
playerChoices[i].classList.remove('clicked');
}
for(let i=0; i< compChoices.length;i++){
compChoices[i].classList.remove('clicked');
}
}
function removeCompBorders(){
const compChoices = document.getElementsByClassName('computer-choice');
for(let i=0; i< compChoices.length;i++){
compChoices[i].classList.remove('clicked');
}
}