guesses=[4,5];
colors=[6,8];
rounds=8;
setlevel=['basic','advanced'];
help=['','Playing Master Mind requires logical thinking. The computer sets up a code of colored pegs and hides it behind the white pegs on top of the board (duplicates are allowed, so even all pegs of the same color might be possible). The aim is to break the code in eight or less guesses. Clicking on one of the colored pegs at the bottom places a peg on the board, starting at the first row, and from left to right. Once the row is complete the game will provide feedback. A black peg on the left indicates that one of the guess pegs is correct both in color and position, a white peg indicates that one of the guess pegs is correct in color but wrong in position.'];

if(!Array.indexOf)
{
Array.prototype.indexOf=function(el)
{
for(j=0;j<this.length;j++) if (el==this[j]) return j;
return -1;
}
}

function preload()
{
count=0;
image=['bg.jpg','zero.gif','white.gif','black.gif','green.gif','red.gif','blue.gif','brown.gif','orange.gif','yellow.gif','cyan.gif','pink.gif','new.gif','undo.gif','level.gif','help.gif','about.gif','mail.gif'];
picture=new Array(image.length);
for (i=0;i<image.length;i++)
{
picture[i]=new Image();
picture[i].onload=loadcheck;
picture[i].src=image[i];
}
}

function loadcheck()
{
count++;
if (count==image.length) setup();
}

function shuffle()
{
for (i=0;i<colors[0];i++)
{
random=Math.random();
for (k=0;k<colors[0];k++) if (random<=(k+1)/colors[0]&&random>k/colors[0]) hide[i]=color[k];
}
for (i=0;i<guesses[0];i++)
{
hide[i]="<img src=\""+hide[i]+".gif\">";
hide.length=guesses[0];
}
}

function setup()
{
step=0;
mark=["?","!"];
color=['green','red','blue','brown','orange','yellow','cyan','pink'];
if (guesses[0]==4) color.length=colors[0];
hide=new Array(colors[0]);
guess=new Array(guesses[0]*rounds);
answer=new Array(guesses[0]*rounds);
calc=new Array(guesses[0]*rounds);
proof=new Array(guesses[0]);
test=new Array(guesses[0]);
solution=new Array(guesses[0]);
black="<img src=\"black.gif\">";
white="<img src=\"white.gif\">";
zero="<img src=\"zero.gif\">";
for (i=0;i<guesses[0];i++) solution[i]=white;
for (i=0;i<guesses[0]*rounds;i++)
{
guess[i]=zero;
answer[i]=zero;
}
shuffle();
message="Level: "+setlevel[0];
show();
}

function submit(a)
{
if (mark[0]=="!")
{
message="Game over.";
show();
}
else
{
guess[step]="<img src=\""+a+".gif\">";
guess.length=guesses[0]*rounds;
if ((step+1)/guesses[0]-(parseInt((step+1)/guesses[0]))==0)
{
index=step-(guesses[0]-1);
test=guess.slice((step-(guesses[0]-1)),(step+1));
for (i=0;i<guesses[0];i++) proof[i]=hide[i];
test.length=guesses[0];
proof.length=guesses[0];
for (i=(step-(guesses[0]-1));i<(step+1);i++)
{
if (hide[i-index]==test[i-index])
{
answer[i]=black;
proof[i-index]="";
test[i-index]="";
}
}
for (i=(step-(guesses[0]-1));i<(step+1);i++)
{
if (proof.indexOf(test[i-index])!=-1&&answer[i]!=black)
{
answer[i]=white;
proof[proof.indexOf(test[i-index])]="";
test[i-index]="";
}
calc[i]=answer[i];
}
order=calc.slice((step-(guesses[0]-1)),(step+1));
order.sort();
for (i=(step-(guesses[0]-1));i<(step+1);i++)
{
calc.splice(i,1,order[i-index]);
answer.splice(i,1,calc[i]);
}
}
if (
(step+1)/guesses[0]-(parseInt((step+1)/guesses[0]))==0&&
guesses[0]==4&&(
(answer[step-(guesses[0]-1)]==zero)&&
(answer[step-(guesses[0]-2)]==zero)&&
(answer[step-(guesses[0]-3)]==zero)&&
(answer[step]==zero))||
(step+1)/guesses[0]-(parseInt((step+1)/guesses[0]))==0&&
guesses[0]==5&&(
(answer[step-(guesses[0]-1)]==zero)&&
(answer[step-(guesses[0]-2)]==zero)&&
(answer[step-(guesses[0]-3)]==zero)&&
(answer[step-(guesses[0]-4)]==zero)&&
(answer[step]==zero))
)
{
if (step>=guesses[0]*rounds-1) lose();
else message="No matching peg.";
}
else if (
guesses[0]==4&&(
(answer[step-(guesses[0]-1)]==black)&&
(answer[step-(guesses[0]-2)]==black)&&
(answer[step-(guesses[0]-3)]==black)&&
(answer[step]==black))||
guesses[0]==5&&(
(answer[step-(guesses[0]-1)]==black)&&
(answer[step-(guesses[0]-2)]==black)&&
(answer[step-(guesses[0]-3)]==black)&&
(answer[step-(guesses[0]-4)]==black)&&
(answer[step]==black))
)
{
mark.reverse();
for (i=0;i<guesses[0];i++) solution[i]=hide[i];
message="Congratulations: You win!";
}
else if (step!=guesses[0]*rounds-1) message="";
else lose();
step++;
show();
}
}

function lose()
{
for (i=0;i<guesses[0];i++) solution[i]=hide[i];
message="Sorry: You lose!";
}

function show()
{
if (guesses[0]==5) pegs="<img src=\"cyan.gif\" onclick=\"javascript:submit('cyan')\" id=\"button\">&nbsp;<img src=\"pink.gif\" onclick=\"javascript:submit('pink')\" id=\"button\">&nbsp;";
else pegs="";
menu="<img src=\"green.gif\" onclick=\"javascript:submit('green')\" id=\"button\">&nbsp;<img src=\"red.gif\"  onclick=\"javascript:submit('red')\" id=\"button\">&nbsp;<img src=\"blue.gif\" onclick=\"javascript:submit('blue')\" id=\"button\">&nbsp;<img src=\"brown.gif\" onclick=\"javascript:submit('brown')\" id=\"button\">&nbsp;<img src=\"orange.gif\" onclick=\"javascript:submit('orange')\" id=\"button\">&nbsp;<img src=\"yellow.gif\" onclick=\"javascript:submit('yellow')\" id=\"button\">&nbsp;"+pegs+"<br><img src=\"new.gif\" onclick=\"javascript:setup()\" id=\"button\" alt=\"Set up new game\" title=\"Set up new game\">&nbsp;<img src=\"undo.gif\" onclick=\"javascript:undo()\" id=\"button\" alt=\"Undo last move\" title=\"Undo last move\">&nbsp;<img src=\"level.gif\" onclick=\"javascript:level()\" id=\"button\" alt=\"Toggle level basic/advanced\" title=\"Toggle level basic/advanced\">&nbsp;<img src=\"help.gif\" onclick=\"javascript:showhelp()\" id=\"button\" alt=\"Toggle help on/off\" title=\"Toggle help on/off\">&nbsp;<img src=\"about.gif\" onclick=\"javascript:about()\" id=\"button\" alt=\"About this game\" title=\"About this game\">&nbsp;<a href=\"mailto:thomas.weibel@bluewin.ch\" title=\"E-mail to the author\"><img src=\"mail.gif\" id=\"button\" alt=\"E-mail to the author\" border=\"0\"><\/a>";
if (step>=guesses[0]*rounds&&mark[0]=="?") mark.reverse();
if (guesses[0]==5) table="<tr><td id=\"wide\"><\/td><td id=\"guess\">"+mark[0]+"<\/td><td id=\"guess2\">"+solution[0]+"<\/td><td id=\"guess2\">"+solution[1]+"<\/td><td id=\"guess2\">"+solution[2]+"<\/td><td id=\"guess2\">"+solution[3]+"<\/td><td id=\"guess2\">"+solution[4]+"<\/td><td id=\"space\" rowspan=\"9\"><\/td><td id=\"help\"rowspan=\"9\"><span>"+help[0]+"<\/span><\/td><\/tr><tr><td id=\"wide\">"+answer[35]+"&nbsp;"+answer[36]+"&nbsp;"+answer[37]+"&nbsp;"+answer[38]+"&nbsp;"+answer[39]+"<\/td><td>8<\/td><td>"+guess[35]+"<\/td><td>"+guess[36]+"<\/td><td>"+guess[37]+"<\/td><td>"+guess[38]+"<\/td><td>"+guess[39]+"<\/td><\/tr><tr><td id=\"wide\">"+answer[30]+"&nbsp;"+answer[31]+"&nbsp;"+answer[32]+"&nbsp;"+answer[33]+"&nbsp;"+answer[34]+"<\/td><td>7<\/td><td>"+guess[30]+"<\/td><td>"+guess[31]+"<\/td><td>"+guess[32]+"<\/td><td>"+guess[33]+"<\/td><td>"+guess[34]+"<\/td><\/tr><tr><td id=\"wide\">"+answer[25]+"&nbsp;"+answer[26]+"&nbsp;"+answer[27]+"&nbsp;"+answer[28]+"&nbsp;"+answer[29]+"<\/td><td>6<\/td><td>"+guess[25]+"<\/td><td>"+guess[26]+"<\/td><td>"+guess[27]+"<\/td><td>"+guess[28]+"<\/td><td>"+guess[29]+"<\/td><\/tr><tr><td id=\"wide\">"+answer[20]+"&nbsp;"+answer[21]+"&nbsp;"+answer[22]+"&nbsp;"+answer[23]+"&nbsp;"+answer[24]+"<\/td><td>5<\/td><td>"+guess[20]+"<\/td><td>"+guess[21]+"<\/td><td>"+guess[22]+"<\/td><td>"+guess[23]+"<\/td><td>"+guess[24]+"<\/td><\/tr><tr><td id=\"wide\">"+answer[15]+"&nbsp;"+answer[16]+"&nbsp;"+answer[17]+"&nbsp;"+answer[18]+"&nbsp;"+answer[19]+"<\/td><td>4<\/td><td>"+guess[15]+"<\/td><td>"+guess[16]+"<\/td><td>"+guess[17]+"<\/td><td>"+guess[18]+"<\/td><td>"+guess[19]+"<\/td><\/tr><tr><td id=\"wide\">"+answer[10]+"&nbsp;"+answer[11]+"&nbsp;"+answer[12]+"&nbsp;"+answer[13]+"&nbsp;"+answer[14]+"<\/td><td>3<\/td><td>"+guess[10]+"<\/td><td>"+guess[11]+"<\/td><td>"+guess[12]+"<\/td><td>"+guess[13]+"<\/td><td>"+guess[14]+"<\/td><\/tr><tr><td id=\"wide\">"+answer[5]+"&nbsp;"+answer[6]+"&nbsp;"+answer[7]+"&nbsp;"+answer[8]+"&nbsp;"+answer[9]+"<\/td><td>2<\/td><td>"+guess[5]+"<\/td><td>"+guess[6]+"<\/td><td>"+guess[7]+"<\/td><td>"+guess[8]+"<\/td><td>"+guess[9]+"<\/td><\/tr><tr><td id=\"wide\">"+answer[0]+"&nbsp;"+answer[1]+"&nbsp;"+answer[2]+"&nbsp;"+answer[3]+"&nbsp;"+answer[4]+"<\/td><td>1<\/td><td>"+guess[0]+"<\/td><td>"+guess[1]+"<\/td><td>"+guess[2]+"<\/td><td>"+guess[3]+"<\/td><td>"+guess[4]+"<\/td><\/tr>";
else table="<tr><td id=\"wide\"><\/td><td id=\"guess\">"+mark[0]+"<\/td><td id=\"guess\">"+solution[0]+"<\/td><td id=\"guess\">"+solution[1]+"<\/td><td id=\"guess\">"+solution[2]+"<\/td><td id=\"guess\">"+solution[3]+"<\/td><td id=\"space\" rowspan=\"9\"><\/td><td id=\"help\"rowspan=\"9\"><span>"+help[0]+"<\/span><\/td><\/tr><tr><td id=\"wide\">"+answer[28]+"&nbsp;"+answer[29]+"&nbsp;"+answer[30]+"&nbsp;"+answer[31]+"<\/td><td>8<\/td><td>"+guess[28]+"<\/td><td>"+guess[29]+"<\/td><td>"+guess[30]+"<\/td><td>"+guess[31]+"<\/td><\/tr><tr><td id=\"wide\">"+answer[24]+"&nbsp;"+answer[25]+"&nbsp;"+answer[26]+"&nbsp;"+answer[27]+"<\/td><td>7<\/td><td>"+guess[24]+"<\/td><td>"+guess[25]+"<\/td><td>"+guess[26]+"<\/td><td>"+guess[27]+"<\/td><\/tr><tr><td id=\"wide\">"+answer[20]+"&nbsp;"+answer[21]+"&nbsp;"+answer[22]+"&nbsp;"+answer[23]+"<\/td><td>6<\/td><td>"+guess[20]+"<\/td><td>"+guess[21]+"<\/td><td>"+guess[22]+"<\/td><td>"+guess[23]+"<\/td><\/tr><tr><td id=\"wide\">"+answer[16]+"&nbsp;"+answer[17]+"&nbsp;"+answer[18]+"&nbsp;"+answer[19]+"<\/td><td>5<\/td><td>"+guess[16]+"<\/td><td>"+guess[17]+"<\/td><td>"+guess[18]+"<\/td><td>"+guess[19]+"<\/td><\/tr><tr><td id=\"wide\">"+answer[12]+"&nbsp;"+answer[13]+"&nbsp;"+answer[14]+"&nbsp;"+answer[15]+"<\/td><td>4<\/td><td>"+guess[12]+"<\/td><td>"+guess[13]+"<\/td><td>"+guess[14]+"<\/td><td>"+guess[15]+"<\/td><\/tr><tr><td id=\"wide\">"+answer[8]+"&nbsp;"+answer[9]+"&nbsp;"+answer[10]+"&nbsp;"+answer[11]+"<\/td><td>3<\/td><td>"+guess[8]+"<\/td><td>"+guess[9]+"<\/td><td>"+guess[10]+"<\/td><td>"+guess[11]+"<\/td><\/tr><tr><td id=\"wide\">"+answer[4]+"&nbsp;"+answer[5]+"&nbsp;"+answer[6]+"&nbsp;"+answer[7]+"<\/td><td>2<\/td><td>"+guess[4]+"<\/td><td>"+guess[5]+"<\/td><td>"+guess[6]+"<\/td><td>"+guess[7]+"<\/td><\/tr><tr><td id=\"wide\">"+answer[0]+"&nbsp;"+answer[1]+"&nbsp;"+answer[2]+"&nbsp;"+answer[3]+"<\/td><td>1<\/td><td>"+guess[0]+"<\/td><td>"+guess[1]+"<\/td><td>"+guess[2]+"<\/td><td>"+guess[3]+"<\/td><\/tr>";
display="<p id=\"title\">Master Mind<\/p><table>"+table+"<\/table><p id=\"copyright\">&copy; 2009 twb<\/p><p>"+message+"<\/p><br><br><br><p id=\"menu\">"+menu+"<\/p>";
document.getElementById('screen').innerHTML=display;
}

function undo()
{
if (step!=0)
{
guess[step-1]=zero;
step--;
message="Move undone.";
if ((step+1)/guesses[0]-(parseInt((step+1)/guesses[0]))==0) for (i=(step-(guesses[0]-1));i<(step+1);i++) answer[i]=zero;
if (mark[0]=="!"||step>=guesses[0]*rounds-1)
{
for (i=0;i<guesses[0];i++) solution[i]=white;
mark=["?","!"];
}
}
else message="Undo not possible.";
show();
}

function level()
{
guesses.reverse();
colors.reverse();
setlevel.reverse();
setup();
}

function showhelp()
{
help.reverse();
show();
}

function about()
{
message="<p>Master Mind 1.4 by www.thomasweibel.ch<\/p>";
show();
}
