Created
October 29, 2015 10:39
-
-
Save bvsatyaram/085357cde7725480cff1 to your computer and use it in GitHub Desktop.
Simple Hang Man Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('Hangman', []); | |
angular.module('Hangman') | |
.controller('GameCtrl', function() { | |
var game = this; | |
game.word = "SINGLETON"; | |
game.allChars = game.word.split(""); | |
game.chars = []; | |
angular.forEach(game.allChars, function(char) { | |
game.chars.push({ | |
char: char | |
}); | |
}); | |
game.allGuesses = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""); | |
game.guesses = []; | |
angular.forEach(game.allGuesses, function(char) { | |
game.guesses.push({ | |
char: char, | |
used: false | |
}); | |
}); | |
game.guessesLeft = 6; | |
game.charUsed = function(char) { | |
var used; | |
angular.forEach(game.guesses, function(guess){ | |
if(guess.char == char) { | |
used = guess.used; | |
} | |
}); | |
return used; | |
} | |
game.charForDisplay = function(char) { | |
if(game.charUsed(char)) { | |
return char; | |
} else { | |
return "-"; | |
} | |
}; | |
game.charIncluded = function(char) { | |
var included = false; | |
angular.forEach(game.chars, function(charObj){ | |
if(charObj.char == char) { | |
included = true; | |
} | |
}); | |
return included; | |
}; | |
game.gameOver = function() { | |
var over = true; | |
angular.forEach(game.chars, function(charObj) { | |
over = over && game.charUsed(charObj.char); | |
}); | |
return over; | |
}; | |
game.guessChar = function(char) { | |
if (!game.charIncluded(char)) { | |
game.guessesLeft --; | |
} | |
if (game.guessesLeft < 1) { | |
alert("Game Over! I win!!"); | |
return | |
} | |
angular.forEach(game.guesses, function(guess){ | |
if(guess.char == char) { | |
guess.used = true; | |
} | |
}); | |
if (game.gameOver()) { | |
alert("You Win! I Giveup!!"); | |
} | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en" ng-app="Hangman"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>Bootstrap 101 Template</title> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous"> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script> | |
<script src="js/app.js"></script> | |
</head> | |
<body> | |
<div class="container" ng-controller="GameCtrl as game"> | |
<h1>Guess a word:</h1> | |
<div class="btn-group" role="group"> | |
<button type="button" class="btn btn-default" ng-repeat="charObj in game.chars">{{game.charForDisplay(charObj.char)}}</button> | |
</div> | |
<h2>Click on a char to use it</h2> | |
<div class="btn-group" role="group"> | |
<button type="button" class="btn btn-default" ng-repeat="guess in game.guesses" ng-click="game.guessChar(guess.char)" ng-disabled="game.charUsed(guess.char)">{{guess.char}}</button> | |
</div> | |
<h2>Chances Left: {{game.guessesLeft}}</h2> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment