topical media & game development
game-javascript-math-ucla-fibonim.htm / htm
<html><head><title>fibonim.html</title>
<script language="JavaScript">
<!-- hide this script tag's contents from old browsers
function init() {
x=26;
lim=x-1;
}
function newgame(form) {
x=28+5*Math.floor(Math.random()*5);
lim=x-1;
junk1 = "There are "+x+" counters in the pile.\r";
junk2="You may remove up to "+lim+" counters.\r";
form.space.value=junk1+junk2;
form.result.focus();
}
function fibo(x) { //Largest Fibonacci number <= x.
var a=0;
var b=1;
var temp;
while (a+b<=x) {
temp=b;
b=a+b;
a=temp;
}
return b
}
function remove(x,lim) {
var n,remo;
if (x<=lim) {
remo=x
} else {
var y=x;
while (true) {
n=y-fibo(y);
if (n==0) {
if (y<=lim) {
remo=y
} else {
remo=0
}
break
}
if ((n<=lim)&&(3*n<y)) {
remo=n;
break
}
y=n;
}
}
if (remo==0) {
if (x<5) {
remo=1
} else {
m=Math.min(lim,Math.floor((x-1)/3));
remo=m
}
}
return remo
}
function compute(form) {
if (x==0) {
form.space.value="You win!"
} else if (x==-1) {
form.space.value="I win!"
} else {
Z=parseInt(form.result.value);
if (isNaN(Z)) {
alert("Type in an integer between 1 and "+lim)
} else if ((Z<1)||(Z>lim)) {
alert("You must remove between 1 and "+lim+" counters.")
} else {
x=x-Z;
if (x==0) {
form.space.value="You win!"
} else {
lim=2*Z;
junk1="You removed "+Z+", leaving "+x+((x==1)?" counter":" counters")+" in the pile.\r"
junk2="I may remove up to "+lim+" counters.\r";
lim=remove(x,lim);
junk3="I remove "+lim+((lim==1)?" counter.\r\r":" counters.\r\r")
x=x-lim;
if (x==0) {
x=-1;
form.space.value=junk1+junk2+junk3+"I win!"
} else {
lim=2*lim;
junk4="Now there are "+x+" counters in the pile.\r"
junk5="You may remove up to "+lim+" counters.";
form.space.value = junk1+junk2+junk3+junk4+junk5;
form.result.value="";
form.result.focus();
}
}
}
}
}
// done hiding from old browsers -->
</script><script charset="utf-8" id="injection_graph_func" src="fibonim_files/injection_graph_func.js"></script></head><body onload="init()" )="">
<center>
<hr size="5">
<h1>Fibonacci Nim</h1>
<i>JavaScript</i>
<hr size="5">
There is one pile of counters. The first player may remove<br>
any positive number of counters, but not the whole pile.<br>
Thereafter, each player may remove at most twice the number<br>
of counters his opponent took on the previous move.<br>
The player who removes the last counter wins.
<p>
You move first.<br>
You can win from the initial position, but don't make any mistakes!</p><p>
</p><form method="post" name="board">
<textarea name="space" rows="7" cols="54" value="stuff">There are 26 counters in the pile.
You may remove up to 25 counters.
</textarea>
<p>
How many do you remove?
<input name="result" size="10" type="text">
<input value="Remove" onclick="compute(this.form)" type="button">
</p><p>
<input value="newgame" onclick="newgame(this.form)" type="button">
</p></form></center>
</body></html>
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.