-
Notifications
You must be signed in to change notification settings - Fork 0
/
rps.js
101 lines (83 loc) · 2.44 KB
/
rps.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
RPS = {
_:{
results:{
rock:'rock',
paper:'paper',
scissors:'scissors'
}
},
active: false,
last_result: null,
sneaky_mode: false,
display: function(leapmotion_frame) {
if (!RPS.active) return;
var result = null;
if (leapmotion_frame.hands !== undefined){
if (leapmotion_frame.hands.length == 1
&& (leapmotion_frame.pointables.length == 0 ||leapmotion_frame.pointables.length == 1)) {
result = RPS._.results.rock;
} else {
switch (leapmotion_frame.pointables.length) {
case 5:
case 4:
result = RPS._.results.paper;
break;
case 3:
case 2:
result = RPS._.results.scissors;
break;
default:
result = null;
}
}
if (RPS.last_result == null && result != null){
RPS.last_result = result;
setTimeout(function (){
if (RPS.last_result !== null){
console.log('result', RPS.last_result);
RPS.active = false;
if(!RPS.sneaky_mode){
bot_result = RPS.bot_result();
} else {
bot_result = RPS.make_bot_beat_player(RPS.last_result);
RPS.sneaky_mode = false;
};
winner = RPS.winner(RPS.last_result, bot_result);
$('#winner').html(winner);
$('#bot').addClass(bot_result);
$('#player').addClass(RPS.last_result);
RPS.last_result = null;
}
}, 500);
}
if (result !== null){
RPS.last_result = result;
}
$('#hands').html(leapmotion_frame.hands.length);
$('#fingers').html(leapmotion_frame.pointables.length);
}
},
bot_result: function (){
switch (Math.floor(Math.random() * 3 + 1)){
case 1:
return RPS._.results.rock;
case 2:
return RPS._.results.paper;
case 3:
return RPS._.results.scissors;
}
},
winner: function (player_result, bot_result){
if (player_result == bot_result) return 'nobody';
if ((player_result == RPS._.results.rock && bot_result == RPS._.results.scissors) ||
(player_result == RPS._.results.scissors && bot_result == RPS._.results.paper) ||
(player_result == RPS._.results.paper && bot_result == RPS._.results.rock))
return 'player';
return 'bot';
},
make_bot_beat_player: function(player_result) {
if(player_result == RPS._.results.rock) return RPS._.results.paper;
if(player_result == RPS._.results.scissors) return RPS._.results.rock;
if(player_result == RPS._.results.paper) return RPS._.results.scissors;
}
}