-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
216 lines (209 loc) · 6.98 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const rulesBtn = document.querySelector('.buttons__rules');
const nextRoundBtn = document.querySelector('.buttons__next');
const rulesContainer = document.querySelector('.rules');
const optionBtns = document.querySelectorAll('.option-container__btn');
const optionContainer = document.querySelector('.option-container');
const human = document.querySelector('.human');
const cpu = document.querySelector('.cpu');
const weaponChoice = document.querySelector('.weapon-choice');
const gameField = document.querySelector('.game-field');
const scoreCounter = document.querySelector('.score-panel__score');
const playersWeapon = [
`<div class="paper shadow player-one weapon" data-weapon="paper">
<button class="paper__btn button"></button>
</div>`,
`<div class="scissors shadow player-one weapon" data-weapon="scissors">
<button class="scissors__btn button"></button>
</div>`,
`<div class="rock shadow player-one weapon" data-weapon="rock">
<button class="rock__btn button"></button>
</div>`,
];
let playerOneScore = 0;
let playerTwoScore = 0;
const sendNames = () => {
const container = document.querySelector('.container');
const inputsField = document.createElement('div');
const inputPlayerOne = document.createElement('input');
const inputPlayerTwo = document.createElement('input');
const enterBtn = document.createElement('button');
inputsField.classList.add('inputs__field');
inputPlayerOne.classList.add('inputs__name');
inputPlayerOne.classList.add('name-one');
inputPlayerOne.placeholder = 'Name...';
inputPlayerTwo.classList.add('inputs__name');
inputPlayerTwo.classList.add('name-two');
inputPlayerTwo.placeholder = 'Name...';
enterBtn.classList.add('buttons__enter');
enterBtn.textContent = 'start';
container.append(inputsField);
inputsField.append(inputPlayerOne, inputPlayerTwo, enterBtn);
const hideNames = () => {
inputsField.classList.add('hide');
};
enterBtn.addEventListener('click', hideNames);
};
const chooseEnemy = (e) => {
optionContainer.classList.add('started');
createGameField();
e.target.classList.add('choosen-option');
if (e.target.matches('span')) {
e.target.parentElement.classList.add('choosen-option');
}
if (human.classList.contains('choosen-option')) {
human.classList.add('right');
cpu.classList.add('none');
sendNames();
} else if (cpu.classList.contains('choosen-option')) {
cpu.classList.add('left');
human.classList.add('none-left');
sendNames();
const inputPlayerTwo = document.querySelector('.name-two');
inputPlayerTwo.style.display = 'none';
inputPlayerTwo.value = 'cpu';
}
};
const createGameField = () => {
gameField.innerHTML = `<div class="paper" data-weapon="paper">
<button class="paper__btn button"></button>
</div>
<div class="scissors" data-weapon="scissors">
<button class="scissors__btn button"></button>
</div>
<div class="rock pt25" data-weapon="rock">
<button class="rock__btn button"></button>
</div>`;
const gameButtons = document.querySelectorAll('.button');
const enemiesRound = () => {
if (cpu.classList.contains('choosen-option')) {
vsCpu();
} else if (
human.classList.contains('choosen-option') &&
cpu.classList.contains('none')
) {
vsHuman();
}
};
const weapon = (e) => {
weaponChoice.classList.add('small');
if (e.target === gameButtons[0]) {
weaponChoice.innerHTML = playersWeapon[0];
} else if (e.target === gameButtons[1]) {
weaponChoice.innerHTML = playersWeapon[1];
} else if (e.target === gameButtons[2]) {
weaponChoice.innerHTML = playersWeapon[2];
}
};
const vsCpu = () => {
cpuArr = [
`<div class="paper shadow player-two" data-weapon="paper">
<button class="paper__btn button"></button>
</div>`,
`<div class="scissors shadow player-two" data-weapon="scissors">
<button class="scissors__btn button"></button>
</div>`,
`<div class="rock shadow player-two" data-weapon="rock">
<button class="rock__btn button"></button>
</div>`,
];
if (weaponChoice.innerHTML !== '') {
gameField.innerHTML = `<div class="cpu__container">
<button class="cpu__btn"></button></div>`;
const stopDraw = () => {
let index = Math.floor(Math.random() * 3);
gameField.innerHTML = '';
weaponChoice.innerHTML += cpuArr[index];
showResult();
};
setTimeout(stopDraw, 1500);
}
};
const vsHuman = () => {
weaponsArr = [];
weaponsArr.push(weaponChoice.innerHTML);
gameButtons.forEach((el) => {
el.addEventListener('click', function (e) {
weapon(e);
weaponChoice.innerHTML = weaponsArr[0] + weaponChoice.innerHTML;
const weapons = document.querySelectorAll('.weapon');
if (weapons[1].classList.contains('player-one')) {
weapons[1].classList.remove('player-one');
weapons[1].classList.add('player-two');
}
setTimeout(500, showResult());
});
});
};
const delay = () => {
setTimeout(enemiesRound, 800);
};
gameButtons.forEach((el) => {
el.addEventListener('click', weapon);
});
gameButtons.forEach((el) => {
el.addEventListener('click', delay);
});
};
const showResult = () => {
const playerOneName = document.querySelector('.name-one').value;
let playerTwoName = document.querySelector('.name-two').value;
const playerOne = document.querySelector('.player-one');
const playerTwo = document.querySelector('.player-two');
playerOne.classList.add('no-before');
if (playerOne.dataset.weapon === playerTwo.dataset.weapon) {
gameField.innerHTML = `<p class="result">draw</p>`;
} else if (
playerOne.dataset.weapon === 'paper' &&
playerTwo.dataset.weapon === 'scissors'
) {
gameField.innerHTML = `<p class="result">${playerTwoName} win</p>`;
playerTwoScore++;
} else if (
playerOne.dataset.weapon === 'paper' &&
playerTwo.dataset.weapon === 'rock'
) {
gameField.innerHTML = `<p class="result">${playerOneName} win</p>`;
playerOneScore++;
} else if (
playerOne.dataset.weapon === 'scissors' &&
playerTwo.dataset.weapon === 'paper'
) {
gameField.innerHTML = `<p class="result">${playerOneName} win</p>`;
playerOneScore++;
} else if (
playerOne.dataset.weapon === 'scissors' &&
playerTwo.dataset.weapon === 'rock'
) {
gameField.innerHTML = `<p class="result">${playerTwoName} win</p>`;
playerTwoScore++;
} else if (
playerOne.dataset.weapon === 'rock' &&
playerTwo.dataset.weapon === 'paper'
) {
gameField.innerHTML = `<p class="result">${playerTwoName} win</p>`;
playerTwoScore++;
} else if (
playerOne.dataset.weapon === 'rock' &&
playerTwo.dataset.weapon === 'scissors'
) {
gameField.innerHTML = `<p class="result">${playerOneName} win</p>`;
playerOneScore++;
}
scoreCounter.innerHTML = `${playerOneScore}:${playerTwoScore}`;
nextRoundBtn.addEventListener('click', nextRound);
};
const nextRound = () => {
weaponChoice.innerHTML = '';
createGameField();
};
const showRules = () => {
const closeBtn = document.querySelector('.rules__close-btn');
rulesContainer.classList.add('active');
closeBtn.addEventListener('click', function () {
rulesContainer.classList.remove('active');
});
};
rulesBtn.addEventListener('click', showRules);
optionBtns.forEach((el) => {
el.addEventListener('click', chooseEnemy);
});