Last active
September 6, 2015 03:52
-
-
Save kevincolten/8c8adbbe5f00b5f51f90 to your computer and use it in GitHub Desktop.
Towers of Hanoi (GUI)
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"> | |
<head> | |
<meta charset="utf-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title></title> | |
<link rel="stylesheet" href="style.css" /> | |
</head> | |
<body> | |
<div data-stack="1"> | |
<div data-block="100"></div> | |
<div data-block="75"></div> | |
<div data-block="50"></div> | |
<div data-block="25"></div> | |
</div> | |
<div data-stack="2"> | |
</div> | |
<div data-stack="3"> | |
</div> | |
<div id="announce-game-won"></div> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js"></script> | |
<script type="text/javascript" src="./TowersOfHanoi.js"></script> | |
</body> | |
</html> |
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
[data-stack] { | |
display: flex; | |
justify-content: left; | |
align-items: center; | |
-webkit-align-items: center; | |
display: -webkit-flex; | |
height: 101px; | |
background-color: aliceblue; | |
margin: 25px; | |
} | |
[data-block] { | |
width: 25px; | |
float: left; | |
} | |
[data-block="25"] { | |
height: 25px; | |
background-color: blue; | |
} | |
[data-block="50"] { | |
height: 50px; | |
background-color: green; | |
} | |
[data-block="75"] { | |
height: 75px; | |
background-color: red; | |
} | |
[data-block="100"] { | |
height: 100px; | |
background-color: yellow; | |
} | |
#announce-game-won { | |
font-size: 50px; | |
text-align: center; | |
} |
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
$(document).ready(function() { | |
var block; | |
function checkForWin() { | |
var gameWon = false; | |
$('[data-stack]').each(function () { | |
if ($(this).data('stack') !== 1 && $(this).children().length === 4) { | |
gameWon = true; | |
} | |
}); | |
return gameWon; | |
} | |
$('[data-stack]').on('click', function() { | |
if (!block) { | |
block = $(this).children().last().detach(); | |
} else { | |
if ( !$(this).children().last().data('block') || $(this).children().last().data('block') > $(block).data('block') ) { | |
$(this).append(block); | |
block = null; | |
} | |
if (checkForWin()) { | |
$('#announce-game-won').text('You Won!'); | |
} | |
} | |
}); | |
FastClick.attach(document.body); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment