-
Notifications
You must be signed in to change notification settings - Fork 3
/
scripts.js
88 lines (61 loc) · 2.45 KB
/
scripts.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
function rpsGame(yourChoice){
//console.log(yourChoice);
let humanChoice,botChoice;
humanChoice=yourChoice.id;
botChoice=numberTochoice(randToRpsInt());
//console.log(botChoice);
results= decideWinner(humanChoice,botChoice);
console.log(results);
message=finalMessage(results);
console.log(message);
rpsFrontEnd(humanChoice,botChoice,message);
}
function randToRpsInt(){
return Math.floor(Math.random()*3)
}
function numberTochoice(number){
return ['rock','paper','scissors'][number];
}
function decideWinner(yourChoice,computerChoice){
const rpsDatabase ={
'rock':{'scissors':1,'rock':0.5,'paper':0},
'paper':{'rock':1,'paper':0.5,'scissors':0},
'scissors':{'paper':1,'scissors':0.5,'rock':0},
}
const yourScore=rpsDatabase[yourChoice][computerChoice];
const botScore=rpsDatabase[computerChoice][yourChoice];
return [yourScore,botScore] ;
}
function finalMessage([yourScore,botScore]){
if(yourScore===0){
return {'message':'You Lost!','color':'red'};
}else if (yourScore===0.5){
return {'message':'You Tied!','color':'blue'};
}else{
return {'message':'You Won!','color':'green'};
}
}
function rpsFrontEnd(humanImageChoice,botImageChoice,finalMessage){
let imageDatabase={
'rock' :document.getElementById('rock').src,
'paper' :document.getElementById('paper').src,
'scissors' :document.getElementById('scissors').src,
}
//Remove all the image
document.getElementById('rock').remove();
document.getElementById('paper').remove();
document.getElementById('scissors').remove();
//Creation result
const humanDiv=document.createElement('div');
const botDiv=document.createElement('div');
const messagediv=document.createElement('div');
humanDiv.innerHTML="<img src='"+imageDatabase[humanImageChoice]+"'height=150 width=150 style='box-shadow: 0px 10px 50px rgba(37,50,233,1);'>";
document.getElementById('flex-box-rps-div').appendChild(humanDiv);
messagediv.innerHTML="<h1 style='color:"+finalMessage['color']+"; font-size:60px; padding:30px; '>"+finalMessage['message'] +"</h1>"
document.getElementById('flex-box-rps-div').appendChild(messagediv);
botDiv.innerHTML="<img src='"+imageDatabase[botImageChoice]+"'height=150 width=150 style='box-shadow: 0px 10px 50px rgba(243,38,24,1);'>";
document.getElementById('flex-box-rps-div').appendChild(botDiv);
}
function reload(){
location.reload();
}