-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
143 lines (128 loc) · 3.47 KB
/
app.js
File metadata and controls
143 lines (128 loc) · 3.47 KB
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
//Guessing Game app.js
function Game () { //master function
this.goal = 50; //default
this.guessCount = -1;
this.active = true;
this.state = 'frigid';
this.guesses = [];
this.newGame = function() {
this.goal = Math.floor(Math.random() * 100)+1;
this.guessCount = 0;
this.active = true;
}
this.diff = function (guess){
if (Math.abs(guess - this.goal) > 40) {
this.state = "frigid";
}
else if (Math.abs(guess - this.goal) > 25) {
this.state = "cold";
}
else if (Math.abs(guess - this.goal) >= 1){
this.state = "warm";
}
else if (Math.abs(guess - this.goal) == 0){
this.state = "fire"
}
}
this.reccommend = function (guess) {
var progress = "";
var rec = ", guess higher";
if(this.guesses.length >1 && this.guessCount > 1){
if(Math.abs(guess - this.goal) < Math.abs(this.guesses[this.guesses.length - 2] - this.goal)) {
progress = ", and getting warmer";
}
else {
progress = ", and getting colder";
}
}
if(guess > this.goal) {
rec = ", guess lower ";
}
return "You are "+this.state+progress+rec;
}
this.guess = function(guess) {
if (this.guessCount === -1){
this.newGame();
}
if (guess === this.goal) {
myGame.active = false;
this.guesses.push(guess);
this.diff(guess);
}
else {
this.guessCount++;
this.guesses.push(guess);
this.diff(guess);
}
}
}
var maxGuesses = 3;
var myGame = new Game();
$(document).ready(function(){
$('.makeAGuess').click(function(event) {
var hisGuess = +$('.guess').val();
if (myGame.guessCount == -1 ) {
myGame.newGame();
}
if (myGame.active == true) {
myGame.guess(hisGuess);
if (myGame.active == false || myGame.guessCount >= maxGuesses) {
if(myGame.state == "fire") {
$('body').css('background', 'url(volcano.jpg)');
$('body').css('background-size', '100%');
$('ul').append("<li>You Have WON!!!!</li>");
}
else {
$('ul').append("<li>You Have Failed, Try Again...</li>");
}
$('.makeAGuess').prop('disabled', true);
$('.hint').prop('disabled', true);
}
else if(myGame.state == "frigid") {
$('body').css('background', 'url(ice.jpg)');
$('body').css('background-size', '100%');
$("ul").append(function(){
return "<li>"+ myGame.reccommend(hisGuess)+"</li>";
});
}
else if(myGame.state == "cold") {
$('body').css('background', 'url(close.jpg)');
$('body').css('background-size', '100%');
$("ul").append(function(){
return "<li>"+ myGame.reccommend(hisGuess)+"</li>";
});
}
else if(myGame.state == "warm") {
$('body').css('background', 'url(normal.jpg)');
$('body').css('background-size', '100%');
$("ul").append(function(){
return "<li>"+ myGame.reccommend(hisGuess)+"</li>";
});
}
}
});
});
$(document).ready(function(){
$('.guess').keyup(function(){
this.value = this.value.replace(/[^0-9\.]/g,'');
});
});
$(document).ready(function(){
$('.reset').click(function(event){
myGame.newGame();
$('.makeAGuess').prop('disabled', false);
$('.hint').prop('disabled', false);
$('body').css('background', 'url(ice.jpg)');
$('body').css('background-size', '100%');
alert("Game is Restarting!")
//unlock options
});
});
$(document).ready(function(){
$('.hint').click(function(event){
var range = (myGame.goal+ Math.ceil(Math.random()*10)) +" and " +(myGame.goal- Math.ceil(Math.random()*10));
$("ul").append(function(){
return "<li>"+"I'd Guess between "+range +"</li>";
});
});
});