-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
207 lines (191 loc) · 7.19 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
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
// Variables
var currentGame;
var currentGameType;
var playerCurrentWins;
var opponentCurrentWins;
var savedPlayers = [];
var choiceButtons = [];
var simpleButton;
var advancedButton;
var clearBoard;
var storedPlayer;
var storedOpponent;
var currentPlayers = {
player1: null,
player2: null,
};
var rpsThrow = {
playerChoice: null,
opponentChoice: null
}
var matchupArray = [
// 0 = draw, 1 = win, 2 = loss
[0, 2, 1, 1, 2, 0], [1, 0, 2, 0, 1, 2], [2, 1, 0, 2, 0, 1], [2, 0, 1, 0, 1, 2], [1, 2, 0, 2, 0, 1], [0, 1, 2, 1, 2, 0]
]
var randomChoiceHelperArray = ['rock', 'paper', 'scissors', 'lizard', 'alien', 'ufo'];
var randomChoiceHelperArraySimple = ['rock', 'paper', 'scissors'];
var startPage = document.querySelector('.start-page');
var gamePage = document.querySelector('.game-page');
var playerSide = document.querySelector('.player-area');
var opponentSide = document.querySelector('.opponent-area');
var playerChoiceImage = document.querySelector('.image-box1');
var opponentChoiceImage = document.querySelector('.image-box2');
var announcementText = document.querySelector('.announcement');
var imageArea = document.querySelector('.boxes-wrap');
var buttonsArea = document.querySelector('.button-hide-container');
var playerInnerWrap = document.querySelector('.player-inner-wrap');
var opponentInnerWrap = document.querySelector('.opponent-inner-wrap');
// Functions
function getRandomIndex(array) {
return Math.floor(Math.random() * array.length);
}
function onLoad() {
for (var j = 0; j < 6; j++) {
choiceButtons[j] = document.querySelector(`.rps-button-${j}`);
choiceButtons[j].isLocked = false;
choiceButtons[j].addEventListener('click', setPlayerChoice);
}
simpleButton = document.querySelector('.classic-rps');
simpleButton.gameTypeChosen = 'simple';
simpleButton.addEventListener('click', chooseGameType);
advancedButton = document.querySelector('.advanced-rps');
advancedButton.gameTypeChosen = 'advanced';
advancedButton.addEventListener('click', chooseGameType);
}
onLoad();
function createPlayer(playerName = 'human', playerToken = '👩🏻', playerWins = 0) {
var player =
{
name: playerName,
token: playerToken,
wins: playerWins
}
return player;
}
function chooseGameType() {
if (this.gameTypeChosen === 'advanced') {
currentPlayers.player1 = createPlayer('You', '🐶', 0);
currentPlayers.player2 = createPlayer('Computer', '🤖', 0);
currentGame = createGame(currentPlayers.player1, currentPlayers.player2);
}
else {
currentPlayers.player1 = createPlayer('You', '🐶', 0);
currentPlayers.player2 = createPlayer('Computer', '🤖', 0);
currentGame = createGame(currentPlayers.player1, currentPlayers.player2, 'simple');
for (var i = 3; i < 6; i++) {
choiceButtons[i].classList.add('hidden');
}
}
playerInnerWrap.innerHTML = `<h3>${currentGame.player1.name}</h3><h3>${currentGame.player1.token} </h3>
<div class="player-area">
<div>Wins:${currentGame.player1.wins}</div>
</div>`;
opponentInnerWrap.innerHTML = `<h3>${currentGame.player2.name}</h3><h3> ${currentGame.player2.token}</h3>
<div class="opponent-area">
<div>Wins:${currentGame.player2.wins}</div>
</div>`;
playerSide = document.querySelector('.player-area');
opponentSide = document.querySelector('.opponent-area');
startPage.classList.add('hidden');
gamePage.classList.remove('hidden');
}
function createGame(p1, p2, gameType = 'advanced') {
gameObject =
{
player1: p1,
player2: p2,
rpsType: gameType,
currentThrows: rpsThrow,
}
if (typeof (Storage)) {
if (sessionStorage.playerData) {
storedPlayer = JSON.parse(sessionStorage.getItem('playerData'));
gameObject.player1 = storedPlayer;
} else { }
if (sessionStorage.opponentData) {
storedOpponent = JSON.parse(sessionStorage.getItem('opponentData'));
gameObject.player2 = storedOpponent;
}
else { }
}
return gameObject;
}
function makeOpponentChoice() {
if (currentGame.rpsType === 'advanced') {
var computerThrow = randomChoiceHelperArray[getRandomIndex(randomChoiceHelperArray)];
rpsThrow.opponentChoice = computerThrow;
}
else {
var computerThrow = randomChoiceHelperArraySimple[getRandomIndex(randomChoiceHelperArraySimple)];
rpsThrow.opponentChoice = computerThrow;
}
return computerThrow;
}
function setPlayerChoice() {
rpsThrow.playerChoice = this.title;
clearTimeout(clearBoard);
makeOpponentChoice();
resolveThrow();
}
function sideBarDisplay() {
playerSide.innerHTML = `<div>Wins:${currentGame.player1.wins}</div>`
opponentSide.innerHTML = `<div>Wins:${currentGame.player2.wins}</div>`
}
function displayThrow(p1Choice, p2Choice) {
var thrownImageArray = ['', ''];
var tempArray = [p1Choice, p2Choice];
for (var i = 0; i < tempArray.length; i++) {
if (tempArray[i] === 'rock') {
thrownImageArray[i] = 'assets/happy-rocks.png';
}
else if (tempArray[i] === 'paper') {
thrownImageArray[i] = 'assets/happy-paper.png';
}
else if (tempArray[i] === 'scissors') {
thrownImageArray[i] = 'assets/happy-scissors.png';
}
else if (tempArray[i] === 'lizard') {
thrownImageArray[i] = 'assets/flat-lizard.png';
}
else if (tempArray[i] === 'alien') {
thrownImageArray[i] = 'assets/happy-alien.png';
}
else {
thrownImageArray[i] = 'assets/ufo.png';
}
}
playerChoiceImage.innerHTML = `<img src=${thrownImageArray[0]} alt="player 1 image">`
opponentChoiceImage.innerHTML = `<img src=${thrownImageArray[1]} alt="player 2 image">`
}
function resolveThrow() {
buttonsArea.classList.add('hidden');
imageArea.classList.remove('hidden');
var playerMatchup = matchupArray[randomChoiceHelperArray.indexOf(rpsThrow.playerChoice)];
var resolutionVal = playerMatchup[randomChoiceHelperArray.indexOf(rpsThrow.opponentChoice)];
displayThrow(rpsThrow.playerChoice, rpsThrow.opponentChoice);
if (!resolutionVal) {
announcementText.innerText = "Draw!";
}
else if (resolutionVal === 1) {
announcementText.innerText = "YOU WIN!";
currentGame.player1.wins += 1;
sideBarDisplay();
}
else {
announcementText.innerText = "You lose...";
currentGame.player2.wins += 1;
sideBarDisplay();
}
if (typeof (Storage)) {
sessionStorage.setItem('playerData', JSON.stringify(currentGame.player1));
sessionStorage.setItem('opponentData', JSON.stringify(currentGame.player2));
}
clearBoard = setTimeout(clearGameDisplay, 2000);
}
function clearGameDisplay() {
playerChoiceImage.innerHTML = ``;
opponentChoiceImage.innerHTML = ``;
announcementText.innerText = `Make your choice!`;
imageArea.classList.add('hidden');
buttonsArea.classList.remove('hidden');
}