Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
162 lines (130 loc) · 4.36 KB
/
Copy pathapp.js
File metadata and controls
162 lines (130 loc) · 4.36 KB
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
// state
// score
// player pick
// ai pick
const playerWinsLSKey = "playerWins";
const AIWinsLSKey = "AIWins";
const winningResultsMap = {
paper: ["rock"],
rock: ["scissors"],
scissors: ["paper"],
};
let state = {
playerWins: Number(localStorage.getItem(playerWinsLSKey)) || 0,
AIWins: Number(localStorage.getItem(AIWinsLSKey)) || 0,
playerPick: null,
AIPick: null,
};
const renderScore = () => {
const pointsElement = document.querySelector(".points");
pointsElement.innerText = state.playerWins - state.AIWins;
};
const bindPickEvents = () => {
document.querySelectorAll(".options button").forEach((button) => {
button.addEventListener("click", pick);
});
document.querySelector(".result__button").addEventListener("click", reset);
};
const pick = (e) => {
pickByPlayer(e.currentTarget.dataset.pick);
pickByAI();
hideOptions();
showFight();
};
const pickByPlayer = (pickedOption) => {
state = {
...state,
playerPick: pickedOption,
};
};
const pickByAI = () => {
const options = ["rock", "paper", "scissors"];
const AIPick = options[Math.floor(Math.random() * options.length)];
state = {
...state,
AIPick,
};
};
const hideOptions = () => {
const optionsElement = document.querySelector(".options");
optionsElement.classList.add("slide-left");
};
const showFight = () => {
const fightElement = document.querySelector(".fight");
fightElement.classList.add("slide-left");
createElementPickedByPlayer();
createElementPickedByAI();
document.querySelectorAll(".options button").forEach((button) => {
button.setAttribute("tabindex", -1);
});
document.querySelector(".result__button").setAttribute("tabindex", 0);
showResult();
};
const showResult = () => {
const resultTextElement = document.querySelector(".result__text");
if (state.AIPick === state.playerPick) {
resultTextElement.innerText = "DRAW";
} else if (winningResultsMap[state.playerPick].includes(state.AIPick)) {
resultTextElement.innerText = "YOU WIN";
localStorage.setItem(playerWinsLSKey, state.playerWins + 1);
state = {
...state,
playerWins: state.playerWins + 1,
};
} else {
resultTextElement.innerText = "YOU LOSE";
localStorage.setItem(playerWinsLSKey, state.AIWins + 1);
state = {
...state,
AIWins: state.AIWins + 1,
};
}
setTimeout(renderResult, 1000);
renderScore();
};
const renderResult = () => {
document.querySelector(".result").classList.add("shown");
document.querySelector(".pick--player").classList.add("moved");
document.querySelector(".pick--ai").classList.add("moved");
};
const createElementPickedByPlayer = () => {
const playerPick = state.playerPick;
const pickContainerElement = document.querySelector(
".pick__container--player"
);
pickContainerElement.innerHTML = "";
pickContainerElement.appendChild(createPickElement(playerPick));
};
const createElementPickedByAI = () => {
const AIPick = state.AIPick;
const pickContainerElement = document.querySelector(".pick__container--ai");
pickContainerElement.innerHTML = "";
pickContainerElement.appendChild(createPickElement(AIPick));
};
const createPickElement = (option) => {
const pickElement = document.createElement("div");
pickElement.classList.add("button", `button--${option}`);
const imageConatinerElement = document.createElement("div");
imageConatinerElement.classList.add("button__image-container");
const imgElement = document.createElement("img");
imgElement.src = `./images/icon-${option}.svg`;
imgElement.alt = option;
imageConatinerElement.appendChild(imgElement);
pickElement.appendChild(imageConatinerElement);
return pickElement;
};
const reset = () => {
const fightElement = document.querySelector(".fight");
fightElement.classList.remove("slide-left");
const optionsElement = document.querySelector(".options");
optionsElement.classList.remove("slide-left");
document.querySelectorAll(".options button").forEach((button) => {
button.setAttribute("tabindex", 0);
});
document.querySelector(".result__button").setAttribute("tabindex", -1);
};
const init = () => {
renderScore();
bindPickEvents();
};
init();