-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuessingGame.js
203 lines (161 loc) · 5.89 KB
/
GuessingGame.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
197
198
199
200
201
202
203
/*all the javascript things*/
function generateWinningNumber(){
return 1+Math.floor(Math.random()*100);
}
function shuffle(array) {
var m = array.length, t, i;
while (m) {
i = Math.floor(Math.random() * m--);
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
}
function Game(){
this.playersGuess = null;
this.pastGuesses = [];
this.winningNumber = generateWinningNumber();
}
Game.prototype.difference = function(){
return Math.abs(this.winningNumber-this.playersGuess);
}
Game.prototype.isLower = function(){
return this.playersGuess < this.winningNumber ? true : false;
}
Game.prototype.playersGuessSubmission = function(x){
if(typeof x !== 'number'||x>100||x<1) throw('That is an invalid guess.');
console.log(this.pastGuesses)
if(this.pastGuesses === null) {
return this.checkGuess(x);
}
if(this.pastGuesses.length !== 4){
return this.checkGuess(x);
}
else {
if(this.checkGuess(x)==='You Win Nothing!') return 'You Win Nothing!';
else return 'You Lose.';
}
}
Game.prototype.checkGuess = function(y){
if(y===this.winningNumber) return 'You Win Nothing!';
this.playersGuess = y;
this.pastGuesses.push(y);
var len = this.pastGuesses.length;
for(let i = 0; i < len-1; i++){
if(y === this.pastGuesses[i]) {
return 'You guess before, why you do again? I no get.';
}
}
if(this.difference()<2) return "Ogly,Ogly,Og!";
if(this.difference()<5) return "FIRE!!!!!!!!";
if(this.difference()<9) return "YOU SO CLOSE";
if(this.difference()<16) return "A wee bit far.";
if(this.difference()<25) return "Oh nonono FAR";
if(this.difference()<35) return "REAL CRAZY FAR";
if(this.difference()<100) return "UHHH UHHHHHHH";
}
Game.prototype.provideHint = function(){
return shuffle([this.winningNumber,generateWinningNumber(),generateWinningNumber()]);
}
function newGame (){
return new Game();
}
//jQuery functionality
$(document).ready(function() {
var newGame = new Game(),
check = '',
x = $('input#number');
//pop up every time user clicks on the input box.
//there is h1 on top of image that changes based upon returned
//value of player submission
function owl (check){
$('div.hidden-body').show();
$('h1.hidden-text-1').text(check);
setTimeout(function() {
$('div.hidden-body').fadeOut();
}, 2000);
}
//every visible change on page as well as the changes done
//to the newGame object.
function setValue (){
//saving the returned string to use later.
check = newGame.playersGuessSubmission(parseInt(x.val(),10));
//generating one of the hints, 15 random numbers.
//right after, the winning number is placed randomly in one of the indexes
var i=0,arr=[];
while(i<15){
arr.push(generateWinningNumber());
i++;
}
arr[Math.floor(Math.random()*15)] = newGame.winningNumber;
//different image is displayed if user wins. Pointing animal.
if(check === 'You Win Nothing!') {
$('div.hidden-body').css({'background': 'url("images/pointing.jpg")','background-size':'1300px'});
}
//each instance of game, 3 values change.
//hippo stores previous values and says derogatory things.
//bird thing on left has answer within array of 10, values keep changing,
//except that the value is within it constantly, in diffent idx each time.
//right bird thing reminds you what the owl said.
var len = newGame.pastGuesses.length;
if(len-1===0) {
$('.hippo-talk').html(newGame.pastGuesses.join(' ')+"? you ain't got it in ya");
$('button.btn.btn-primary.btn-md.left').html(arr.join(' ') + ': One this');
$('button.btn.btn-primary.btn-md.right').html('Owl say: '+check);
}
else if(len-1===1) {
$('.hippo-talk').html(newGame.pastGuesses.join()+"??? Ha! crap shoot.");
$('button.btn.btn-primary.btn-md.right').html('Owl say: '+check);
$('button.btn.btn-primary.btn-md.left').html(arr.join(' ') + ': One this');
}
else if(len-1===2) {
$('.hippo-talk').html(newGame.pastGuesses.join()+' grubgrubgrub');
$('button.btn.btn-primary.btn-md.right').html('Owl say: '+check);
$('button.btn.btn-primary.btn-md.left').html(arr.join(' ') + ': One this');
}
else if(len-1===3) {
$('.hippo-talk').html(newGame.pastGuesses.join()+' One More!!!');
$('button.btn.btn-primary.btn-md.right').html('Owl say: '+check);
$('button.btn.btn-primary.btn-md.left').html(arr.join(' ') + ': One this');
}
else if(len-1===4) {
$('.hippo-talk').html(newGame.pastGuesses.join()+' suckaa');
$('button.btn.btn-primary.btn-md.right').html('You diarhea, real mushy kind');
$('button.btn.btn-primary.btn-md.left').html('I help, you fail, you no friend');
}
//same as resetting the game, user clicks 6 times does it.
else{
newGame = new Game();
$('.hippo-talk').html('I got something to say');
$('button.btn.btn-primary.btn-md.right').html('Me too, thing I tell');
$('button.btn.btn-primary.btn-md.left').html('I tell you thing');
}
//return value to 0
x.val() = 0;
}
//I tried to do both on a mouseclick but did not work, after struggling this worked,
//two separate. Mousedown,mouseup are irrelevant, well suppose it wouldn't work if
//user changed mind while holding down mouse. But too late.
$('button.btn.btn-success').on('mousedown', function() {
setValue();
});
$('button.btn.btn-success').on('mouseup', function() {
owl(check);
});
//Reset button brings everything back to how it was at the beginning
$('input.btn.btn-danger').on('click', function(){
newGame = new Game();
$('.hippo-talk').html('I got something to say');
$('button.btn.btn-primary.btn-md.right').html('Me too, thing I tell');
$('button.btn.btn-primary.btn-md.left').html('I tell you thing');
})
//Enter can also be used but the functionality does not work the same way at all.
//Change this later? Likely not. So click, not enter!
x.keypress(function(e) {
if(e.which == 13) {
check = newGame.playersGuessSubmission(parseInt(x.val(),10));
x.val() = 0;
}
});
});