2012-12-19 02:56:37 +00:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Conway's Game of Life</title>
|
|
|
|
<link type="text/css" rel="stylesheet" href="/static/normalize.css" />
|
|
|
|
<script type="text/javascript" src="/static/jquery.min.js"></script>
|
|
|
|
<script type="text/javascript">
|
|
|
|
curState = {};
|
|
|
|
curImg = "";
|
|
|
|
|
|
|
|
function populateInitialState() {
|
|
|
|
$.getJSON('/state', {}, onPopulateResponse);
|
|
|
|
}
|
|
|
|
|
2012-12-19 03:03:26 +00:00
|
|
|
function goToNextState(state, loop) {
|
|
|
|
$.post(
|
|
|
|
'/state',
|
|
|
|
JSON.stringify({s: state}, null, 2),
|
|
|
|
function(data, textStatus, jqXHR) {
|
|
|
|
var state = onPopulateResponse(data, textStatus, jqXHR);
|
|
|
|
if (loop) {
|
|
|
|
setTimeout(function() { goToNextState(state, loop); }, 500);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'json'
|
|
|
|
);
|
2012-12-19 02:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function onPopulateResponse(data, textStatus, jqXHR) {
|
|
|
|
$.extend(curState, data.s);
|
|
|
|
curImg = data.i;
|
|
|
|
var $stateImg = $('#state_img');
|
|
|
|
if ($stateImg.length < 1) {
|
|
|
|
$stateImg = $('<img id="state_img" />').appendTo($('#state_container'));
|
|
|
|
}
|
|
|
|
$stateImg.attr('src', 'data:image/png;base64,' + encodeURI(data.i));
|
2012-12-19 03:03:26 +00:00
|
|
|
return data.s;
|
2012-12-19 02:56:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$(function() {
|
|
|
|
populateInitialState();
|
|
|
|
$('#step').click(function() { goToNextState(curState); });
|
2012-12-19 03:03:26 +00:00
|
|
|
$('#play').click(function() { goToNextState(curState, true); });
|
2012-12-19 02:56:37 +00:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Conway's Game of Life</h1>
|
|
|
|
<div id="controls">
|
|
|
|
<button id="step">Step</button>
|
|
|
|
<button id="play">Play</button>
|
|
|
|
</div>
|
|
|
|
<hr />
|
|
|
|
<div id="state_container">
|
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>
|