-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
170 lines (141 loc) · 4.74 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
//Hide / Show the modal
const modalClose = document.querySelector('.modal-close');
const modal = document.querySelector('.modal_wrapper');
const rules = document.querySelector('.rules');
modalClose.addEventListener('click', function() {
modal.style.display="none";
});
rules.addEventListener('click', function() {
modal.style.display="block";
});
//Score
const gameResult = document.querySelector('.game_result');
const gameWin = document.querySelector('.game-win');
const gameEqual = document.querySelector('.game-equal')
let winStreak = 0;
const score = document.querySelector('span');
function Win() {
document.querySelector(".player_pick").id='win';
gameResult.style.display="block";
gameWin.style.display="block";
gameEqual.style.display="none";
gameLoose.style.display="none";
winStreak++;
score.innerHTML = winStreak;
}
const gameLoose = document.querySelector('.game-loose');
function Loose() {
document.querySelector(".house_pick").id='win';
gameResult.style.display="block";
gameLoose.style.display="block";
gameWin.style.display="none";
gameEqual.style.display="none";
winStreak--;
score.innerHTML = winStreak;
}
function Equal() {
gameResult.style.display="block";
gameEqual.style.display="block";
gameWin.style.display="none";
gameLoose.style.display="none";
}
//Game function
const gameOption = document.querySelector('.game-option');
//const gameIcon = document.querySelectorAll('.game-button');
const gamePick = document.querySelector('.game_pick');
//gameIcon.forEach(element => {
//element.addEventListener('click',
function Start(element) {
//Hide the Choice Picker
gameOption.style.display="None";
//Show the Game Pick panel
gamePick.style.display="Flex";
//Generate a random number
Game(element);
}
//});
function Game(yourChoice){
let human = yourChoice.id;
let bot;
bot = numberToChoice(roll());
results = decideWinner(human,bot);
setTimeout(setScore,3200,results); //setScore(results)
frontEnd(human,bot);
setTimeout(function(){
document.querySelector('.flip-card').style.display="none";
console.log("hi");
document.querySelector('.house_result').style.display="block";
}, 3000)
}
function roll(){
return Math.floor(Math.random()*3);
}
function numberToChoice(num){
return ['rock','paper','scissors'][num];
}
function decideWinner(yourChoice,botChoice){
let Data = {
'rock':{'scissors': 1,'rock':0.5, 'paper':0},
'paper':{'scissors': 0,'rock':1, 'paper':0.5},
'scissors':{'scissors': 0.5,'rock':0, 'paper':1}
}
let yourScore = Data[yourChoice][botChoice];
let computerScore = Data[botChoice][yourChoice];
return [yourScore , computerScore];
}
function setScore([yourScore,computerScore]){
if(yourScore === 1){
Win();
}
else if (yourScore === 0.5){
Equal();}
else{
Loose();
}
}
function frontEnd(humanChoice,botChoice){
//images database
let Database = {
'rock': document.querySelector('#rock > img').src,
'paper': document.querySelector('#paper > img').src,
'scissors': document.querySelector('#scissors > img').src };
let humanDiv = document.createElement('div');
let botDiv = document.createElement('div');
humanDiv.className="icon-bg";
botDiv.className="icon-bg";
const botImg = document.createElement("img");
const humanImg = document.createElement("img");
humanImg.src = Database[humanChoice];
botImg.src = Database[botChoice];
console.log(Database[humanChoice]);
document.querySelector('.player_result').id = humanChoice;
document.querySelector('.house_result').id = botChoice;
humanDiv.id="player"
document.querySelector('.player_result').appendChild(humanDiv);
document.querySelector('#player').appendChild(humanImg);
botDiv.id="house"
document.querySelector('.house_result').appendChild(botDiv);
document.querySelector('#house').appendChild(botImg);
document.querySelector('.house_result').style.display="none";
}
function Reset() {
gameOption.style.display = "Flex";
gameResult.style.display = "none";
gamePick.style.display = "none";
const player = document.querySelector('#player');
const house = document.querySelector('#house');
player.remove();
house.remove();
document.querySelector(".player_pick").removeAttribute('id');
document.querySelector(".house_pick").removeAttribute('id');
document.querySelector('.flip-card').style.display="block";
document.querySelector('.house_result').style.display="none";
}
const more = document.querySelector('.more');
if (more) {
//When the buton more exist add a event listener
more.addEventListener('click', function() {
//If clicked restart the game
Reset();
});
}