topical media & game development
game-javascript-math-ucla-Moore.htm / htm
<html><head><!--Moore's Nim --><title>Moore's Nim</title>
<script language="JavaScript">
<!-- begin hiding of script
var w=0;
var x=0;
var y=0;
var z=0;
var bit = new Array(10);
var blo = new Array(10);
bit[0]=0;
blo[0]=0;
mess1="There are four piles of chips. You and I alternate\r"
+"making moves. You start. A move consists of removing \r"
+"as many chips as desired from any two piles; however,\r"
+"at least one chip must be taken from some pile. \r"
+"The player who removes the last chip wins. \r\r"
+"You can win from the initial position, but don't \r"
+"make any mistakes!";
function init() {
var x=1;
for (var i=1;i<10;i++) {
bit[i]=x;
blo[i]=blo[i-1]+x;
x=2*x;
}
}
function newgame(form) {
while (equ12(w,x,y,z)==0) {
w=Math.floor(Math.random()*21)+3;
x=Math.floor(Math.random()*21)+3;
y=Math.floor(Math.random()*21)+3;
z=Math.floor(Math.random()*21)+3;
}
form.req1.value=0;
form.req2.value=0;
form.req3.value=0;
form.req4.value=0;
form.space.value=mess1;
display(form)
form.req1.focus();
}
function display(form) {
form.out1.value=w;
form.out2.value=x;
form.out3.value=y;
form.out4.value=z;
}
function equ12(w,x,y,z) {
var a1= (w&x&(y^z))|((w^x)&y&z); //exactly 3 1's
var a3= ~(w|x|y|z); //exactly 0 1's
return ~(a1|a3);
}
function equ1(x,y,z) {
var a1= ((x^y)&(~z));
var a2= (~x&~y&z);
return a1|a2;
}
function lm1(u) {
var x=1;
for (var n=0;n<10;n++) {
if (u<x) break;
x=2*x;
}
return n
}
function inputCheck(form) {
var w1=parseInt(form.req1.value);
var x1=parseInt(form.req2.value);
var y1=parseInt(form.req3.value);
var z1=parseInt(form.req4.value);
var ans = true;
if (isNaN(w1)||isNaN(x1)||isNaN(y1)||isNaN(z1)) {
alert("Fill in all the entries.");
var ans=false
} else if ((w1<0)||(x1<0)||(y1<0)||(z1<0)) {
alert("You cannot remove a negative number of chips from a pile.");
ans=false
} else if ((w1>w)||(x1>x)||(y1>y)||(z1>z)) {
alert(w1+" "+w+" "+x1+" "+x+" "+y1+" "+y+" "+z1+" "+z);
alert("You cannot remove more chips than a pile contains.");
ans=false
} else if (w1+x1+y1+z1==0) {
alert("You must remove at least one chip")
ans = false
} else if ((w1>0)+(x1>0)+(y1>0)+(z1>0)>2) {
alert("You may not remove chips from more than 2 piles.");
ans=false;
} else if ((w1>0)&&(w1<=w)) {
w=w-w1;
if ((x1>0)&&(x1<=x)) {
x=x-x1
} else if (y1>0) {
y=y-y1
} else if (z1>0) {
z=z-z1
}
} else if (x1>0) {
x=x-x1;
if (y1>0) {
y=y-y1
} else if (z1>0) {
z=z-z1
}
} else if (y1>0) {
y=y-y1;
if (z1>0) {
z=z-z1
}
} else if (z1>0) {
z=z-z1
}
return ans
}
function makeanymove(form) {
with (Math) {
if (w>x) {
w=w-floor(random()*ceil(w/2)+1)
} else {
x=x-floor(random()*ceil(x/2)+1)
}
if (y>z) {
y=y-floor(random()*ceil(y/2))
} else {
z=z-floor(random()*ceil(z/2))
}
}
announce(form);
}
function announce(form) {
display(form)
junk1="From the piles "+fmt(wsav)+fmt(xsav)+fmt(ysav)+fmt(zsav)+"\r\r";
junk2=" I removed "+fmt(wsav-w)+fmt(xsav-x)+fmt(ysav-y)+fmt(zsav-z)+"\r\r"
if (w+x+y+z>0) {
junk3="It's your move."
} else {
junk3="I win."
}
form.space.value=junk1+junk2+junk3;
form.req1.focus();
}
function fmt(x) {
var str
if (x>9) {
str=" "+x
} else {
str=" "+x
}
return str
}
function main(form) {
if (inputCheck(form)) {
display(form)
xsav=x;
ysav=y;
zsav=z;
wsav=w;
if (w+x+y+z==0) {
alert("You win!")
} else {
alert("Your move produced this:\r"+w+" "+x+" "+y+" "+z+
"\r Now it is my move.");
s=equ12(w,x,y,z);
if (s==0) {
makeanymove(form)
} else {
n=lm1(s);
if (w&bit[n]) {
s=equ1(x,y,z);
m=lm1(s);
if (m==0) {
w=(x|y|z)&(~(x&y&z));
} else if (x&bit[m]) {
x=(x&(~blo[m]))|((y^z)&blo[m]);
w=(x|y|z)&(~(x&y&z));
} else if (y&bit[m]) {
y=(y&(~blo[m]))|((x^z)&blo[m]);
w=(x|y|z)&(~(x&y&z));
} else {
z=(z&(~blo[m]))|((x^y)&blo[m]);
w=(x|y|z)&(~(x&y&z));
}
} else if (x&bit[n]){
s=equ1(w,y,z);
m=lm1(s);
if (m==0) {
x=(w|y|z)&(~(w&y&z));
} else if (w&bit[m]) {
w=(w&(~blo[m]))|((y^z)&blo[m]);
x=(w|y|z)&(~(w&y&z));
} else if (y&bit[m]) {
y=(y&(~blo[m]))|((w^z)&blo[m]);
x=(w|y|z)&(~(w&y&z));
} else {
z=(z&(~blo[m]))|((w^y)&blo[m]);
x=(w|y|z)&(~(w&y&z));
}
} else if (y&bit[n]){
s=equ1(w,x,z);
m=lm1(s);
if (m==0) {
y=(w|x|z)&(~(w&x&z));
} else if (w&bit[m]) {
w=(w&(~blo[m]))|((x^z)&blo[m]);
y=(w|x|z)&(~(w&x&z));
} else if (x&bit[m]) {
x=(x&(~blo[m]))|((w^z)&blo[m]);
y=(w|x|z)&(~(w&x&z));
} else {
z=(z&(~blo[m]))|((w^x)&blo[m]);
y=(w|x|z)&(~(w&x&z));
}
} else {
s=equ1(w,x,y);
m=lm1(s);
if (m==0) {
z=(w|x|y)&(~(w&x&y));
} else if (w&bit[m]) {
w=(w&(~blo[m]))|((x^y)&blo[m]);
z=(w|x|y)&(~(w&x&y));
} else if (x&bit[m]) {
x=(x&(~blo[m]))|((w^y)&blo[m]);
z=(w|x|y)&(~(w&x&y));
} else {
y=(y&(~blo[m]))|((w^x)&blo[m]);
z=(w|x|y)&(~(w&x&y));
}
}
}
}
announce(form);
}
}
// end script hiding -->
</script><script charset="utf-8" id="injection_graph_func" src="Moore_files/injection_graph_func.js"></script></head><body onload="init()">
<center>
<hr size="5">
<h1>Moore's Nim</h1>
<i>JavaScript</i>
<hr size="5">
<form method="post" name="board">
<input value="newgame" onclick="newgame(this.form)" type="button"><p>
<textarea name="space" rows="8" cols="54">There are four piles of chips. You and I
alternate making moves. You start.
A move consists of removing as many chips
as desired from any two piles; however, at
least one chip must be taken from some pile.
The player who removes the last chip wins.
To start, click the newgame button.
</textarea>
</p><p>
The four piles are of sizes :
<input name="out1" value="" size="6" type="text">
<input name="out2" value="" size="6" type="text">
<input name="out3" value="" size="6" type="text">
<input name="out4" value="" size="6" type="text">
</p><p>
How many do you remove?
<input name="req1" onfocus="this.select()" size="6" type="text">
<input name="req2" onfocus="this.select()" size="6" type="text">
<input name="req3" onfocus="this.select()" size="6" type="text">
<input name="req4" onfocus="this.select()" size="6" type="text">
</p><p>
<input value="Remove" onclick="main(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.