Skip to content

Instantly share code, notes, and snippets.

@dsomel21
Last active June 17, 2016 04:45
Show Gist options
  • Save dsomel21/511a0e6e07c59a73778324a7ccb830d6 to your computer and use it in GitHub Desktop.
Save dsomel21/511a0e6e07c59a73778324a7ccb830d6 to your computer and use it in GitHub Desktop.
Single-player betting game that runs in the browser. The player starts off with a bankroll of $100 and bets money to guess a number randomly chosen by the game. If the player loses all their money, the game ends. Refactored code is in the final edition game.js!
// 'use strict';
// console.log('You got $' + cash);
// var amount = function bet(){
// amount = prompt("Place a bet between $5-$10: ");
// while (amount < 5 || amount > 10){
// alert("That's not right!");
// amount = prompt("Bet must be between $5-$10. Try Again: ");
// }
// alert("OKAY! $" + amount + " IT IS!");
// }
// var guess = function pick() {
// guess = prompt("Pick your number between 1-10: ");
// while (guess < 1 || guess > 10){
// alert("That's not right!");
// guess = prompt("Number must be between 1-10. Try Again: ");
// }
// };
// function main(){
// amount();
// random();
// // guess();
// // test();
// // }
main();
$(document).ready(function(){
var cash = 100;
var user_bet = null;
var user_guess = null;
var error = null;
$('#cash_amount').text(cash);
var random = function () {
return Math.floor((Math.random() * 10 + 1));
}
function validate_bet(bet){
error = 'bet';
return (bet > 10 || bet < 5);
}
function validate_guess(guess){
error = 'guess';
return (guess < 1 || guess > 10)
}
function errors(){
switch(error){
case 'bet':
$("#info").text("You cannot bet more money than What you have!");
break;
case 'guess':
$('#info').text("Must choose a number between 1-10");
break;
}
}
$('#form').submit(function(event) {
event.preventDefault();
user_bet = $('#bet').val();
user_guess = $('#guess').val();
if (validate_bet(user_bet) || validate_guess(user_guess)) {
errors();
}
else {
test();
$('#bet_amount').text(user_bet);
$('#guess_amount').text(user_guess);
$('#cash_amount').text(cash);
}
});
function test(){
if (user_guess == random()){
console.log(true);
cash += parseInt(user_bet);
}
else {
console.log(false);
cash -= parseInt(user_bet);
}
}
});
<!DOCTYPE html>
<html>
<head>
<title>Javascript | Gambling Games</title>
</head>
<body>
<form id="form">
Bet:<br>
<input id="bet" type="number" name="bet">
<br>
Strategic Guess:<br>
<input id="guess" type="number" name="guess">
<br><br>
<input type="submit" value="Submit">
</form>
<h1>Score: <span id="my-score">0</span></h1>
<button class='score-increaser'>+++++++++</button>
<div>
<h2 id="info_header">Results</h2>
<p>Total Cash: $<span id="cash_amount"></span></p>
<h2>Bet: <span id="bet_amount"></span></h2>
<h2>Guess: <span id="guess_amount"></span></h2>
<h4 id="info"></h4>
</div>
<script src="jquery-2.2.4.min.js"></script>
<script src="game.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment