-
Notifications
You must be signed in to change notification settings - Fork 1
/
game-of-shells.js
196 lines (160 loc) · 6.65 KB
/
game-of-shells.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
'use strict';
var GameOfShells = function() {
this._dom = {
'ball': document.getElementById('ball'),
'shell-1': document.getElementById('shell-1'),
'shell-2': document.getElementById('shell-2'),
'shell-3': document.getElementById('shell-3')
}
this._settings = {
'difficulty': {
'easy': {
'moves': 5,
'transition': 400
},
'medium': {
'moves': 10,
'transition': 300
},
'hard': {
'moves': 15,
'transition': 200
},
'extreme': {
'moves': 30,
'transition': 100
}
},
'selected_difficulty': 'medium'
}
this._vars = {
'ball_placement': 0,
'is_moving': false,
'is_first_guess': false,
'wins': 0,
'games': 0
}
this._randomise = function(type) {
if (type === 'ball') {
return Math.round(Math.random() * (3 - 1)) + 1;
} else if (type === 'moves') {
var moves_array = [];
for (var i = 0; i < this._settings.difficulty[this._settings.selected_difficulty].moves; i++) {
var rand = Math.round(Math.random() * (2 - 1)) + 1;
moves_array.push(rand === 1 ? 'left' : 'right');
}
return moves_array;
}
}
this.init = function() {
this.setup();
// set all event listeners
this._dom['shell-1'].addEventListener('click', function() { this.peak(1) }.bind(this));
this._dom['shell-2'].addEventListener('click', function() { this.peak(2) }.bind(this));
this._dom['shell-3'].addEventListener('click', function() { this.peak(3) }.bind(this));
document.getElementById('reset').addEventListener('click', function() {
this.setup();
}.bind(this));
document.getElementById('shake').addEventListener('click', function() {
if (!this._vars.is_moving) {
this.shake();
}
}.bind(this));
document.getElementById('difficulty').addEventListener('change', function() {
this.set_difficulty(this.value);
}.bind(this));
}
this.setup = function() {
this._vars.ball_placement = this._randomise('ball');
for (var i = 1; i < 4; i++) {
this._dom['shell-' + i].classList.remove('pos-1', 'pos-2', 'pos-3');
this._dom['shell-' + i].classList.add('pos-' + i);
}
// place the ball
this._dom.ball.classList.remove('pos-1', 'pos-2', 'pos-3');
this._dom.ball.classList.add('pos-' + this._vars.ball_placement);
this.peak();
}
this.set_difficulty = function(difficulty) {
if (difficulty === 'easy' ||
difficulty === 'medium' ||
difficulty === 'hard' ||
difficulty === 'extreme') {
this._settings.selected_difficulty = difficulty;
}
}
this.peak = function(force) {
var is_forced = !isNaN(force) && force > 0 && force < 4,
shell = 'shell-' + (is_forced ? force : this._vars.ball_placement);
setTimeout(function() {
this._dom[shell].classList.add('lift');
}.bind(this), is_forced ? 0 : 300);
if (this._vars.is_first_guess) {
if (force === this._vars.ball_placement) {
this._vars.wins++;
}
this._vars.games++;
this._vars.is_first_guess = false;
document.getElementById('wins').innerHTML = this._vars.wins + ' wins';
document.getElementById('games').innerHTML = this._vars.games + ' games';
}
setTimeout(function() {
this._dom[shell].classList.remove('lift');
}.bind(this), 800);
}
this.shake = function() {
// set speed, depending on selected difficulty
for (var i = 1; i < 4; i++) {
this._dom['shell-' + i].style.transition = 'all ' + this._settings.difficulty[this._settings.selected_difficulty].transition / 1000 + 's ease';
}
this._dom.ball.classList.add('is-hidden');
var moves = this._randomise('moves'),
iter = 0;
this.shake.do_move = function() {
// make sure this has time to finish before next shake
this._vars.is_moving = true;
if (iter === moves.length) {
// place the ball in the right place
var shell_class_list = document.getElementById('shell-' + this._vars.ball_placement).classList;
for (var i = 0; i < shell_class_list.length; i++) {
if (!shell_class_list[i].indexOf('pos-')) {
this._dom.ball.classList.remove('pos-1', 'pos-2', 'pos-3', 'is-hidden');
this._dom.ball.classList.add('pos-' + shell_class_list[i].split('-')[1]);
}
}
// reset transition to standard value (from stylesheet)
for (var i = 1; i < 4; i++) {
if (this._dom['shell-' + i].style.removeProperty) {
this._dom['shell-' + i].style.removeProperty('transition');
} else {
this._dom['shell-' + i].style.removeAttribute('transition');
}
}
this._vars.is_moving = false;
// give one shot at guessing
this._vars.is_first_guess = true;
clearInterval(window.timer);
return;
}
if (moves[iter] === 'left') {
// swap first and second cups
var shell_out = document.getElementsByClassName('shell pos-1')[0],
shell_in = document.getElementsByClassName('shell pos-2')[0];
shell_in.classList.remove('pos-2');
shell_out.classList.remove('pos-1');
shell_in.classList.add('pos-1');
shell_out.classList.add('pos-2');
} else {
// swap second and third cups
var shell_out = document.getElementsByClassName('shell pos-3')[0],
shell_in = document.getElementsByClassName('shell pos-2')[0];
shell_in.classList.remove('pos-2');
shell_out.classList.remove('pos-3');
shell_in.classList.add('pos-3');
shell_out.classList.add('pos-2');
}
iter++;
}.bind(this)
window.timer = setInterval(this.shake.do_move, this._settings.difficulty[this._settings.selected_difficulty].transition * 1.5);
}
}