Skip to content

Instantly share code, notes, and snippets.

@derekmc
Last active February 22, 2021 18:45
Show Gist options
  • Select an option

  • Save derekmc/716db7b05e48daa77b335363e6a75865 to your computer and use it in GitHub Desktop.

Select an option

Save derekmc/716db7b05e48daa77b335363e6a75865 to your computer and use it in GitHub Desktop.
<html>
<head>
<script src="./src/jszip.min.js"></script>
<script src="./src/FileSaver.min.js"></script>
<script>
// goto targets are capitalized
// contemporary programming make the mistake of being easy to debug, not easy to write.
// debugging is always hard, deal with it.
//
// So linguistic "decoupling" is more important, and easier, than "runtime" decoupling,
// runtime "decoupling", actually leads to linguistic "coupling", ie interdependence
// semantically between the different parts of the program.
//
// Making debugging easy is trying to control the future, which makes no sense,
// and is very hard on top.
// Runtime decoupling, is inherently impossible, so it just makes programs very hard to write.
//
window.addEventListener("load", () => goto(GUESS));
window.addEventListener("load", () => { editor.innerHTML = document.documentElement.outerHTML; });
// variables
let X,N,G,LIMIT,TRIES;
function GUESS(){
N = 100;
X = randint(N);
LIMIT = 8;
TRIES = 0;
disp(`Guess a number between 1 and ${N},`);
more(`You have ${TRIES} tries.`);
textinput.value = "";
return GUESSWAIT;
}
function GUESSWAIT(){
let handlers = [];
wait(handlers, okaybutton, 'click', null, GUESSCHECK);
wait(handlers, textinput, 'keydown', (e)=>(e.keyCode == 13), GUESSCHECK);
return null;
}
function GUESSCHECK(){
G = parseInt(textinput.value);
if(isNaN(G)){
disp(`Please enter a valid number.`);
return GUESSWAIT;
}
disp(`You guessed ${G}.`);
if(X < G){
more(`Lower! The number is lower than ${G}.`); }
else if(X > G){
more(`Higher! The number is higher than ${G}.`); }
else{
console.log('X', X);
return GUESSWIN; }
if(++TRIES == LIMIT){
return GUESSLOSE; }
more(`You have ${LIMIT - TRIES} guesses remaining.`);
return GUESSWAIT;
}
function GUESSWIN(){
more(`You won! ${X} is the correct number.`);
return GUESSRESTART;
}
function GUESSLOSE(){
more("You lost! The number was ${X}.");
return GUESSRESTART;
}
function GUESSRESTART(){
return sleep(2.5, ()=>{
more("Restarting ...");
sleep(2.5, GUESS)})
}
function goto(target){ // targets return the next function to run.
while(target){
target = target(); }
}
function wait(handlers, target, name, test, after){
if(!handlers) handlers = [];
let handler = function(evt){
if(test && !test(evt)) return;
for(let tuple of handlers){
let [other_target, other_name, other_handler] = tuple;
other_target.removeEventListener(other_name, other_handler);
}
goto(after);
}
handlers.push([target, name, handler]);
target.addEventListener(name, handler);
return null;
}
function sleep(n, after){
setTimeout(()=>goto(after), n*1000);
return null;
}
function disp(x){
document.getElementById("disptext").innerHTML = x;
}
function more(x){
document.getElementById("disptext").innerHTML += "\n" + x;
}
function randint(n){
return Math.floor(Math.random() * n);
}
function Download(){
var zip = new JSZip();
// Generate a directory within the Zip file structure
var guessgame = zip.folder("guessgame");
guessgame.file("index.html", document.documentElement.outerHTML);
// Generate the zip file asynchronously
zip.generateAsync({type:"blob"}).then(function(content){
// Force down of the Zip file
saveAs(content, "guessgame.zip");
});
}
</script>
<style>
*{ font-size: 108%; font-family: monospace; }
#editor{ height: 70%; width: 100%; overflow-x: scroll; }
</style>
</head>
<body>
<p><pre id="disptext"></pre></p>
<input id='textinput' type='text'></input>
<input id='okaybutton' type='button' value="ok"></input>
<!-- <p style="position: fixed; bottom: 0px; right:8px; text-align: right;"> -->
<p>
<!-- <h3> Source Code </h3> -->
<!--<button onclick="editor.innerHTML = document.documentElement.outerHTML; editor.style.display='block'"> View </button>-->
<button onclick="Download();"> Download Game</button>
</p>
<p>
<textarea id='editor' wrap='off' style="display:none;" readonly="true"></textarea>
</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment