-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
113 lines (82 loc) · 3.7 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
let playerScore = 0;
let computerScore = 0;
// Button Animations and Events
const buttons = document.querySelectorAll('.button');
buttons.forEach(button => button.addEventListener('click', choose));
function choose(e) {
this.classList.add('choosen');
console.log(e)
}
buttons.forEach(button => button.addEventListener('transitionend', removeTransition));
function removeTransition(e) {
if (e.propertyName !== 'transform') return;
this.classList.remove('choosen');
}
const rock = document.getElementById('rock');
rock.addEventListener('click', rockPlay);
function rockPlay() {
playRound('rock', computerPlay())
}
const paper = document.getElementById('paper');
paper.addEventListener('click', paperPlay);
function paperPlay() {
playRound('paper', computerPlay())
}
const scissors = document.getElementById('scissors');
scissors.addEventListener('click', scissorsPlay);
function scissorsPlay() {
playRound('scissors', computerPlay())
}
// Restart Event
const restart = document.querySelectorAll('.restart');
for (i = 0; i < restart.length; i++) {
restart[i].addEventListener('click', function () {
window.location.reload()
})
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Computer Choice Generator
function computerPlay() {
function getRandomArbitrary(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
let number = getRandomArbitrary(0, 3)
if (number == 0) {
return 'ROCK'
} else if (number == 1) {
return 'PAPER'
} else {
return 'SCISSORS'
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Single Round Main, Interface Transition
function playRound(playerSelection, computerSelection) {
playerSelection = playerSelection.toUpperCase()
if (computerSelection == playerSelection) {
document.querySelector('.results').innerText = 'DEUCE\n\n' + 'PLAYER SCORE: ' + playerScore + '\n' + 'COMPUTER SCORE: ' + computerScore;
document.querySelector('.computersChoiceBox').innerText = computerSelection;
} else if ((playerSelection == 'ROCK' && computerSelection == 'SCISSORS') || (playerSelection == 'PAPER' &&
computerSelection == 'ROCK') || (playerSelection == 'SCISSORS' && computerSelection == 'PAPER')) {
playerScore++
document.querySelector('.results').innerText = 'You Win! ' + playerSelection + ' beats ' + computerSelection +
'\n\n' + 'PLAYER SCORE: ' + playerScore + '\n' + 'COMPUTER SCORE: ' + computerScore
computerScore;
document.querySelector('.computersChoiceBox').innerText = computerSelection;
if (playerScore == 5) {
document.getElementById('game-interface').style.display = 'none';
document.getElementById('end-interface-win').style.display = 'block';
}
} else if ((playerSelection == 'SCISSORS' && computerSelection == 'ROCK') || (playerSelection == 'ROCK' &&
computerSelection == 'PAPER') || (playerSelection == 'PAPER' && computerSelection == 'SCISSORS')) {
computerScore++
document.querySelector('.results').innerText = 'You Lose! ' + computerSelection + ' beats ' + playerSelection +
'\n\n' + 'PLAYER SCORE: ' + playerScore + '\n' + 'COMPUTER SCORE: ' + computerScore
computerScore;
document.querySelector('.computersChoiceBox').innerText = computerSelection;
if (computerScore == 5) {
document.getElementById('game-interface').style.display = 'none';
document.getElementById('end-interface-lose').style.display = 'block';
}
}
}