Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

complete number game #39

Open
wants to merge 1 commit into
base: number-guessing
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions SpecRunner.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Jasmine Spec Runner v2.0.0</title>

<link rel="shortcut icon" type="image/png" href="vendor/jasmine-2.0.0/jasmine_favicon.png">
<link rel="stylesheet" type="text/css" href="vendor/jasmine-2.0.0/jasmine.css">

<script type="text/javascript" src="vendor/jquery-2.1.0.min.js"></script>
<script type="text/javascript">//$.noConflict();</script>

<script type="text/javascript" src="vendor/jasmine-2.0.0/jasmine.js"></script>
<script type="text/javascript" src="vendor/jasmine-2.0.0/jasmine-html.js"></script>
<script type="text/javascript" src="vendor/jasmine-2.0.0/boot.js"></script>
<script type="text/javascript" src="vendor/jasmine-2.0.0/jasmine-jquery.js"></script>

<!-- include source files here... -->
<script type="text/javascript" src="js/business-logic.js"></script>
<script type="text/javascript" src="js/presentation-UI.js"></script>

<!-- include spec files here... -->
<script type="text/javascript" src="spec/spec-helper.js"></script>
<script type="text/javascript" src="spec/BusinessLogic-spec.js"></script>
<script type="text/javascript" src="spec/presentation-UI-spec.js"></script>

</head>

<body>
</body>
</html>
18 changes: 18 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
body {
background: url('../img/coolbg.jpg') no-repeat;
background-size: cover;
}

.main {
background: rgba(255, 255, 255, 0.3);
-webkit-border-radius: 10px;
border-radius: 10px;
padding: 20px;
margin-top: 20px;
}

@media all and (max-width: 64rem) {
.main {
max-width: 90%;
}
}
33 changes: 33 additions & 0 deletions guess-the-number.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Guess the Number!</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/foundation/5.2.3/css/foundation.min.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>

<div class='row main'>
<div class="large-8 large-centered columns">
<div class="panel">
<label>Pick a Number from 1-100:</label>
<label id="response"></label>
</div>
<br />
<input id="user-input" type="text" placeholder="Put your guess here!" />
<input class="button small" id="submit-button" type="button" value="Guess" />
<!-- <input class="button small" id="start-game" type="button" value="Start Game" />
<input class="button small" id="end-game" type="button" value="End Game" />
<input class="button small" id="clear-input" type="button" value="Clear Input and Response" /> -->
</div>
</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="js/business-logic.js"></script>
<script type="text/javascript" src="js/presentation-UI.js"></script>
</body>
</html>
150 changes: 150 additions & 0 deletions guess-the-number_ORG.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<html>
<head>
<title>Guess the Number!</title>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/foundation/5.2.3/css/foundation.min.css">
</head>
<body>
<label>Pick a Number from 1-100:</label>
<label id="response"></label>
<br />
<input id="user-input" type="text" />
<input id="submit-button" type="button" value="Guess" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">

var secretNumber;
var landmineNumber = [];
var guess;
var closestMine;

function genNums() {
secretNumber = parseInt(Math.random() * 100, 10) + 1;
landmineNumber[0] = parseInt(Math.random() * 100, 10) + 1;
landmineNumber[1] = parseInt(Math.random() * 100, 10) + 1;
landmineNumber[2] = parseInt(Math.random() * 100, 10) + 1;
// console.log("secret: " + secretNumber + " landmine: " + landmineNumber);
for (var i =0; i < landmineNumber.length; ++i) {
if (secretNumber === landmineNumber[i]) {
genNums() }
}
}
function numDiff() {
closestMine = undefined
// console.log("landmine numbers: " + landmineNumber);
$.each(landmineNumber, function(index, value) {
if (closestMine === undefined || Math.abs(value - guess) < Math.abs(closestMine - guess)) {
closestMine = value
}
});
closestMine = Math.abs(closestMine - guess)
// console.log(closestMine);
}

function guessNum() {
guess = +$('#user-input').val();
if ($.inArray(guess, landmineNumber) > -1) {
$('#response').html("Oops! You hit a landmine! <strong>BOOM!</strong>");
genNums();
} else if (guess > secretNumber) {
numDiff();
$('#response').text("Nope! The secret number is lower; try again! Be careful, there is a landmine " + closestMine + " places away from your last guess!");
} else if (guess < secretNumber) {
numDiff();
$('#response').text("Nope! The secret number is higher; try again! Be careful, there is a landmine " + closestMine + " places away from your last guess!");
} else { $('#response').text("You guessed the secret number!");
genNums();
};
}

genNums();

$('#submit-button').on('click', function (e) {
guessNum();
$('#user-input').val("");
});

// var secretNumber;
// var landmineNumber;
// var guess;
// var guessDiff;

// function genNums() {
// secretNumber = parseInt(Math.random() * 100, 10) + 1;
// landmineNumber = parseInt(Math.random() * 100, 10) + 1;
// // alert("secret: " + secretNumber + " landmine: " + landmineNumber);
// if (secretNumber === landmineNumber) {
// genNums() }
// }
// function numDiff() {
// if (guess > landmineNumber) {
// guessDiff = (guess - landmineNumber);
// } else {
// guessDiff = (landmineNumber - guess);
// }
// }
// function guessNum() {
// guess = +$('#user-input').val();
// if (guess === landmineNumber) {
// $('#response').text("Oops! You chose the landmine number! BOOM!");
// } else if (guess > secretNumber) {
// numDiff();
// $('#response').text("Nope! The secret number is lower; try again! Be careful, the landmine is " + guessDiff + " places away from your last guess!");
// $('#user-input').val("");
// //guessNum();
// } else if (guess < secretNumber) {
// numDiff();
// $('#response').text("Nope! The secret number is higher; try again! Be careful, the landmine is " + guessDiff + " places away from your last guess!");
// $('#user-input').val("");
// //guessNum();
// } else { $('#response').text("You guessed the secret number!");
// genNums();
// };
// }

// genNums();

// $('#submit-button').on('click', function (e) {
// guessNum();
// });

// var secretNumber;
// var landmineNumber;
// var guess;
// var guessDiff;

// function genNums() {
// secretNumber = parseInt(Math.random() * 100, 10) + 1;
// landmineNumber = parseInt(Math.random() * 100, 10) + 1;
// alert("secret: " + secretNumber + " landmine: " + landmineNumber)
// if (secretNumber === landmineNumber) {
// genNums() }
// };
// function numDiff() {
// if (guess > landmineNumber) {
// guessDiff = (guess - landmineNumber);
// } else {
// guessDiff = (landmineNumber - guess);
// };
// }
// function guessNum() {
// guess = prompt("Guess a number between 1 and 100");
// if (Number(guess) === landmineNumber) {
// alert("Oops! You chose the landmine number! BOOM!");
// } else if (guess > secretNumber) {
// numDiff();
// alert("Nope! The secret number is lower; try again! Be careful, the landmine is " + guessDiff + " places away from your last guess!");
// guessNum();
// } else if (guess < secretNumber) {
// numDiff();
// alert("Nope! The secret number is higher; try again! Be careful, the landmine is " + guessDiff + " places away from your last guess!");
// guessNum();
// } else { alert("You guessed the secret number!"); };
// };

// genNums();
// guessNum();

</script>

</body>
</html>
Binary file added img/coolbg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 55 additions & 0 deletions js/business-logic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
(function() {
var secretNumber;
var landmineNumber = [];
var closestMine;

function genNums() {
secretNumber = parseInt(Math.random() * 100, 10) + 1;
landmineNumber[0] = parseInt(Math.random() * 100, 10) + 1;
landmineNumber[1] = parseInt(Math.random() * 100, 10) + 1;
landmineNumber[2] = parseInt(Math.random() * 100, 10) + 1;
console.log("secret: " + secretNumber + " landmine: " + landmineNumber);
for (var i =0; i < landmineNumber.length; ++i) {
if (secretNumber === landmineNumber[i]) {
genNums() }
}
}
function numDiff() {
closestMine = undefined
// console.log("landmine numbers: " + landmineNumber);
$.each(landmineNumber, function(index, value) {
if (closestMine === undefined || Math.abs(value - guess) < Math.abs(closestMine - guess)) {
closestMine = value
}
});
closestMine = Math.abs(closestMine - guess)
return closestMine
// console.log(closestMine);
}

function guessNum() {

if ($.inArray(guess, landmineNumber) > -1) {
// $('#response').html("Oops! You hit a landmine! <strong>BOOM!</strong>");
genNums();
return "youBoom";
} else if (guess > secretNumber) {
numDiff();
return "lowerMessage";
// $('#response').text("Nope! The secret number is lower; try again! Be careful, there is a landmine " + closestMine + " places away from your last guess!");
} else if (guess < secretNumber) {
numDiff();
return "higherMessage";
// $('#response').text("Nope! The secret number is higher; try again! Be careful, there is a landmine " + closestMine + " places away from your last guess!");
} else {
// $('#response').text("You guessed the secret number! You win!");
genNums();
};
}

genNums();

window.playGame = guessNum
window.minesNum = numDiff

})();
26 changes: 26 additions & 0 deletions js/presentation-UI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(function(){
window.guess;

$('#submit-button').on('click', function (e) {
guess = +$('#user-input').val();
// playGame();
$('#user-input').val("");

var response = playGame();
var mines = minesNum();

if (response === "youBoom") {
$('#response').html("Oops! You hit a landmine! <strong>BOOM!</strong>");
} else if (response === "lowerMessage") {
$('#response').text("Nope! The secret number is lower; try again! Be careful, there is a landmine " + mines + " places away from your last guess!");
} else if (response === "higherMessage") {
$('#response').text("Nope! The secret number is higher; try again! Be careful, there is a landmine " + mines + " places away from your last guess!");
} else {
$('#response').text("You guessed the secret number! You win!");
}


});


})();
56 changes: 56 additions & 0 deletions spec/BusinessLogic-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
describe("BusinessLogic ....................", function () {

describe("validateInputForConstraints", function () {
it('returns true if the input is a number between 1 and 100', function() {
var guessNum = 58;
var result = bl.validateInputForConstraints(guessNum);
expect(result).toEqual(true);
});
it('returns false if the input is not a number', function() {
var guessNum = 'clay';
var result = bl.validateInputForConstraints(guessNum);
expect(result).toEqual(false);
});
it('returns false if the number is 0', function() {
var guessNum = 0;
var result = bl.validateInputForConstraints(guessNum);
expect(result).toEqual(false);
});
it('returns false if the number is a negavitive number', function() {
var guessNum = -10;
var result = bl.validateInputForConstraints(guessNum);
expect(result).toEqual(false);
});
it('returns false if the number is a number over 100', function() {
var guessNum = 158;
var result = bl.validateInputForConstraints(guessNum);
expect(result).toEqual(false);
});
});

describe("numberEvaluator", function () {
bl.secretNumber = 43;
it('returns "message1" if the guess is greater than the secretNumber', function() {
var guessNum = 61;
var result = bl.numberEvaluator(guessNum);
expect(result).toEqual("message1");
});
it('returns "message2" if the guess is less than the secretNumber', function() {
var guessNum = 22;
var result = bl.numberEvaluator(guessNum);
expect(result).toEqual("message2");
});
it('returns "true" if the guess equals the secretNumber', function() {
var guessNum = 43;
var result = bl.numberEvaluator(guessNum);
expect(result).toEqual(true);
});
it('returns "error1" if the number fails the validateInputForConstraints', function() {
var guessNum = "stewart";
var result = bl.numberEvaluator(guessNum);
expect(result).toEqual("error1");
});
});


});
Loading