//Jeff's Mastermind (c) Jeff Kendall 11/2000 
//distribute freely with no files ommitted from the distribution
//covered by the gnu copyleft.
//version 1.0 -- initial release           11/30/2000
//version 1.1 -- added difficulty settings 11/30/2000
//version 1.1b - minor tweaks to support
//               internet explorer         12/01/2000
//version 1.2  - minor tweaks for
//               useability, 8 colors
//               on medium, 12 colors
//               on hard                   12/01/2000
//version 1.21 - smaller code, simpler
//               image names, precache
//               images, more difficulty
//               settings 4,5...12 colors, 
//               more guesses in hard mode 12/03/2000
//version 1.22 - tweaks of the difficulty
//               selector, some images     12/04/2000
//version 1.3  - smaller html code, score  12/04/2000
//
//version 1.4  - improved layout           12/05/2000
//version 1.5  - improved url handling     1/11/2001
//version 1.51 - even better url handling  1/11/2001
//version 1.52 - better url handling in frames 2/12/2002
//
//
//
//
//*******************************************************
//chop mastermind.htm or index.htm off of top.location to
//determine the path to all the images and such...
//global variables and functions.....
//
var tlose=7;
var twin=tlose + 1;
var tmax=tlose -1;
//
var tmp1 = String(window.location);
var tmp2 = tmp1.slice(-6);
//were we called as index.htm? mastermind.htm? or
//as the default web page in a directory? assume if d.htm
//extension is present, we were called as
//http://...../mastermind.htm and if x.htm is present
//we were called as http://..../index.htm
//otherwise assume we were called as
//http://...../
var tmp3=tmp1.lastIndexOf('/');
if (tmp3 > 0) {
var base=tmp1.slice(0,tmp3 + 1);
}
else
{
var base=tmp1;
}
//declare global variables for the secret code
var pick='ffff';
var diff=4;
//declare 5 element arrays due to javascript's
//zero indexing
picks= new Array ('f', 'f', 'f', 'f', 'f');
guess= new Array ('0', '0', '0', '0', '0');
values=new Array ('0','1', '2', '3', '4', '5', '6','7','8','9','a','b','c');
preloadem();
var guesses=0;
var tries=0;
var score=0;
var losses=0;
var wins=0;
//*******************************************************

//*******************************************************
//preload most images
function preloadem() {
preimgs=new Array ( '_',
'face', 'dotq',
'easya', 'medium',
'mediuma', 'hard',
'harda', 'facewin',
'facelose', 'jeffmaila',
'guessbtna', 'guessbtn2',
'lta', 'gta',
'b0w0', 'b0w1',
'b0w2', 'b0w3',
'b0w4', 'b1w0',
'b1w1', 'b1w2',
'b1w3', 'b2w0',
'b2w1', 'b2w2',
'b3w0', 'b4w0',
'n5', 'n6',
'n7', 'n8',
'n9', 'n10',
'n11', 'n12',
'guesbtnq','guessbtnqa');

preimg1=new Array ();
preimg2= new Array();
var idx0;
for (idx0=1;idx0<13;idx0++) {
  preimg1[idx0] = new Image();
  preimg1[idx0].src='dot' + idx0 + '.gif';
  preimg2[idx0] = new Image();
  preimg2[idx0].src=preimgs[idx0] + '.gif';
  }
for (idx0=13;idx0<39;idx0++) {
  preimg2[idx0] = new Image();
  preimg2[idx0].src=preimgs[idx0] + '.gif';
  }
}
//end image preloads
//*******************************************************

//*******************************************************
//pickit picks the secret code.  it is called when the
//user begins to fill in their first guess OR when the
//user submits a blank guess (why would they ever do that?)
function pickit() {
var i;
pick='';
for (i=1;i<5;i++) {
  picks[i] = (Math.floor(Math.random() * diff) + 1);
  if (picks[i]==10) picks[i]='a';
  if (picks[i]==11) picks[i]='b';
  if (picks[i]==12) picks[i]='c';
  picks[i] = String(picks[i]);
  pick=pick+picks[i];
  }
  //pick = picks[1] + picks[2]  + picks[3]  + picks[4]
  document.images.i05.src= base + 'face.gif';
  document.images.i01.src= base + 'dotq.gif';
  document.images.i02.src= base + 'dotq.gif';
  document.images.i03.src= base + 'dotq.gif';
  document.images.i04.src= base + 'dotq.gif';

}
//*******************************************************

//*******************************************************
// this cycles a guess to the next color
// i is a string containing the image name
function nextcolor(i) {
  btnchk();
  var t;
  var d;
  var e;
  var j;
  var gn = String(i);
  var num = i.slice(1,-1);
  var gnum = parseInt(num);
  if (gnum == tries + 1 && tries < tlose) {
  if (pick=='ffff') pickit();
  t=document.images[i].src;
  d=0;
  if (t==base + '0.gif') {
     document.images[i].src=base + 'dot1.gif';
     d=1;
     }
  for (j=1;j<diff;j++) {
    if (t==base + 'dot' + j + '.gif' && d==0) {
      e=j+1;
      document.images[i].src=base + 'dot' + e + '.gif';
      d=1
      }
      }
  if (t==base + 'dot' + diff + '.gif' && d==0) {
     document.images[i].src=base + 'dot1.gif';
     d=1;
     }
     }
else if (tries > tmax) {
startover();
}
}
//*******************************************************

//*******************************************************
//this is the main part of the mastermind program. it
//handles receiving guessings and doling out clues
function guessit(i) {
if (i==0) i= tries + 1;
if (i == tries + 1 && tries < tlose) {
var t;
var k;
var j;
var zero=0;
var white=0;
var black=0;
taken = new Array(0,0,0,0,0);
used = new Array(0,0,0,0,0);
//catch the rare occurence wherein the numbers have not been picked yet.
if (pick=='ffff') pickit();
for (j=1;j<5;j++) {
  guess[j]=0;
  t=document.images['i'+ i + j].src;
  for (k=1;k<diff+1;k++) {
    if (t==base + 'dot' + k + '.gif') guess[j]=values[k];
    }
  if ( guess[j]==0 ) zero = 1;
  }
  if (zero == 1 && !confirm('One or more of your guesses is blank.  Do you want me to use this guess?')) return;

guesses = String(guess[1]) + String(guess[2]) + String(guess[3]) + String(guess[4]);
if (guesses==pick) {
   showit();
   score= score + (twin - tries) * 10 * diff * diff
   wins=wins+1;
   document.forms.frm1.score.value='Score: ' + score + ', W: ' + wins + ', L: ' + losses;
   document.images.i05.src= base + 'facewin.gif';
   tries=twin;
   }
tries=tries+1;
for (j=1;j<5;j++) {
  if (guess[j]==picks[j]) {
           black=black+1;
           taken[j]=1;
           used[j]=2;
           }
  }
for (j=1;j<5;j++) {
  for (k=1;k<5;k++) {
     if (guess[j]==picks[k] && taken[k]==0 && used[j]==0 && j!=k) {
          white=white+1;
          taken[k]=1;
          used[j]=1;
          }
     }
  }
document.images['i' + i + 5].src= base + 'b' + black + 'w' + white + '.gif'
if (tries == tlose ) lose();
} // if i == tries + 1
else if (tries > tmax) {
startover();
}
btnchk();

}
//*******************************************************

//*******************************************************
//called for losers
function lose() {
losses=losses+1;
document.forms.frm1.score.value='Score: ' + score + ', W: ' + wins + ', L: ' + losses;
showit();
   document.images.i05.src= base + 'facelose.gif';
}
//*******************************************************

//*******************************************************
//displays secret code in top (yellow) row
function showit() {
var j;
var k;
for (j=1;j<5;j++) {
  for (k=1;k<13;k++) {
    if (picks[j]==values[k]) {
      document.images['i0' + j].src = base + 'dot' + k + '.gif';
      }
    }
  }
}
//*******************************************************

//*******************************************************
//resets everything but does not pick a new code
function newgame() {
tries=0;
pick='ffff';
for (i=0;i<twin;i++) {
  for (j=1;j<6;j++) {
    document.images['i'+ i + j].src = base + '0.gif' ;
    }
  }
for (i=twin;i<11;i++) {
  for (j=1;j<6;j++) {
    document.images['i'+ i + j].src = base + '00.gif' ;
    }
  }
for (i=1;i<5;i++) {
  picks[i]='f';
  }
document.images['i15'].src='guessbtnq.gif'

}
//*******************************************************

//*******************************************************
//this function resets everything upon clicking the face
//somehow didnt get pick to work in conjunction with newgame
function startover() {
if (pick != 'ffff' && tries < tlose) {
  if (!confirm('are you sure you want to quit this game?')) return;
  losses=losses+1;
  showit();
  document.forms.frm1.score.value='Score: ' + score + ', W: ' + wins + ', L: ' + losses;
  alert('here is the solution to the previous game...(' + picks[1] + picks[2] + picks[3] + picks[4] + ')');
  }
else if (tries > tmax) {
  if (!confirm('new game?')) return;
  }
newgame();
//pick();
}
//*******************************************************

//*******************************************************
//this function displays help for image mouseovers
function mousein(i) {
if (i == tries + 1 && tries < tlose) {
  window.status='click to pick color for current guess';
  }
else if (tries == tlose) {
  window.status='game over - click face to play again';
  }
else if (tries == twin) {
  window.status='you won! - click face to play again';
  }
else  window.status='';
  
}
//*******************************************************

//*******************************************************
//this function displays help for guess link mouseovers
//also handles rollover images
function mouseguess(i) {
if (i == tries + 1 && tries < tlose) {
  window.status='click to submit your current guess';
  document.images['i' + i + '5'].src=base + 'guessbtnqa.gif';
  }
else if (i==0  && tries < tlose) {
  window.status='click to submit your current guess';
  document.images['g0'].src='guessbtna.gif';
  }
else  window.status='';
}
//*******************************************************

//*******************************************************
//this function displays help for guess link mouseovers
//also handles rollover image
function mouseguessout(i) {
if (i == tries + 1 && tries < tlose) {
  window.status='';
  document.images['i' + i + '5'].src=base + 'guessbtnq.gif';
  }
else if (i==0) {
  window.status='';
  document.images['g0'].src='guessbtn.gif';
  }
else  window.status='';
  
}
//*******************************************************


//*******************************************************
//rollover image handler for difficulty level
function diffmouse(i) {
if (diff<7  && i==1) {
document.images.diff.src= base + 'easya.gif';
}
else if (diff>6 && diff < 10 && i==1) {
document.images.diff.src= base + 'mediuma.gif';
}
else if (diff>9 && i==1) {
document.images.diff.src= base + 'harda.gif';
}
else if (diff<7  && i==0) {
document.images.diff.src= base + 'easy.gif';
}
else if (diff>6 && diff < 10 && i==0) {
document.images.diff.src= base + 'medium.gif';
}
else if (diff>9 && i==0) {
document.images.diff.src= base + 'hard.gif';
}
}
//*******************************************************




//*******************************************************
function difficulty(i) {
if (pick != 'ffff' && tries < tlose) {
   if (!confirm('are you sure you want to quit and change difficulty?')) return;
   losses=losses+1;
   showit();
   document.forms.frm1.score.value='Score: ' + score + ', W: ' + wins + ', L: ' + losses;
   alert('here is the solution to the previous game...(' + picks[1] + picks[2] + picks[3] + picks[4] + ')');
   }
else if (tries > tmax) {
   if (!confirm('change difficulty?')) return;
   }
if (i == 0) diff = diff -1;
if (i == 1) diff = diff +1;
if (diff < 4) diff = 4;
if (diff > 12) diff = 12;
if (diff < 7) document.images.diff.src= base + 'easy.gif';
else if (diff > 6 && diff < 10) document.images.diff.src= base + 'medium.gif';
else if (diff > 9) document.images.diff.src= base + 'hard.gif';
document.images.diffnum.src='n' + diff + '.gif';
if (diff < 10) tlose=7;
else tlose = 10;
twin=tlose + 1;
tmax=tlose -1;
if (diff==4) document.images.ltimg.src='lt2.gif';
else document.images.ltimg.src='lt.gif'
if (diff==12) document.images.gtimg.src='gt2.gif';
else document.images.gtimg.src='gt.gif'
newgame();
}
//*******************************************************


//*******************************************************
//checks which guess button should be enabled
function btnchk() {
var j;
var k=tries+1;
if (k >= twin) return;
for (j=k;j<twin;j++) {
  if (j==k && tries < tlose) {
    document.images['i' + j + '5'].src=base + 'guessbtnq.gif';
    }
  else {
    document.images['i' + j + '5'].src=base + '0.gif';
    }
  }
}
//*******************************************************

//*******************************************************
function explain(img) {
var i;
var j;
var k=img.slice(1,-1);
//alert(k);
if (document.images[img].src !=base + '0.gif') {
  for (i=0;i<5;i++) {
    for (j=0;j<5-i;j++) {
      if (document.images[img].src == base + 'b' + i + 'w' + j + '.gif' )
      alert('For this row, you got: ' + i + ' exactly right, ' + j + ' right color, wrong spot.');
      }
    }
  }
if (document.images[img].src == base + 'guessbtnqa.gif') {
  //alert('calling guessit: ' + k);
  guessit(k);
  }
}

//*******************************************************

//*******************************************************
function gtmouse(i) {
if (diff < 12 && i==1) {
  window.status='click to increase difficulty';
  document.images.gtimg.src= 'gta.gif';
  }
else if (diff < 12 && i==0) {
  window.status='';
  document.images.gtimg.src= 'gt.gif';
  }
else if (diff == 12) {
  window.status='';
  document.images.gtimg.src= 'gt2.gif';
  }
}
//*******************************************************

//*******************************************************
function ltmouse(i) {
if (diff > 4 && i==1) {
  window.status='click to decrease difficulty';
  document.images.ltimg.src= 'lta.gif';
  }
else if (diff > 4 && i==0) {
  window.status='';
  document.images.ltimg.src= 'lt.gif';
  }
else if (diff == 4) {
  window.status='';
  document.images.ltimg.src= 'lt2.gif';
  }
}
//*******************************************************

//*******************************************************
//shows arbitrary text in score area
//used for diagnosis
function showtxt(txt) {
document.forms.frm1.score.value=txt;
}
//*******************************************************


//*******************************************************
//makes html table to play game on, takes less space than
//repetitive html
function makepage() {
var i;
var j;
document.forms.frm1.score.value='building game board...';
document.write('<table border=1 align=\"center\">');
document.write('<tr align=\"center\" class=\"hd\">');
document.write('<td colspan=5 align=\"center\"  class=\"hd\">');
document.write('<!--Jeff\'s Mastermind ver 1.52 -->\n<img src=\"jeffmast.gif\">');
document.write('</td>\n</tr><tr align=\"center\" >');
  for (j=1;j<5;j++) {
    document.write('<td bgcolor=\"cfcf7f\"><img src=\"0.gif\" width=32 height=32 \n');
    document.write('name=\"i0' + j + '\" border=0></td>\n');
    }
document.write('<td bgcolor=\"cfcf7f\">\n<a href=\"javascript:startover();\" \n');
document.write('onmouseover=\"window.status=\'click to start over\';return true;\"\n');
document.write('onmouseout=\"window.status=\'\';\"\n>\n<img src=\"0.gif\" width=32 height=32 \n');
document.write('name=\"i05\" \nalt=\"Jeff\'s Mastermind - v. 1.52 - (c) J Kendall 11/2000\" \n');
document.write('border=0>\n</a>\n</td>\n</tr>');
var tmp;
var tmp2;
for (i=1;i<11;i++) {
    document.write('<tr align=\"center\" >\n');
    if (i>7) {
       tmp='00.gif';
       tmp2='00.gif';
             }
    else if (i==1) {
       tmp='0.gif';
       tmp2='guessbtnq.gif';
       }
    else {
       tmp='0.gif';
       tmp2='0.gif';
       }
  for (j=1;j<5;j++) {
    document.write('<td><a href=\"javascript:nextcolor(\'i' + i + j + '\')\" \n');
    document.write('onmouseover=\"javascript:mousein(' + i + ');return true;\"\n');
    document.write('onmouseout=\"window.status=\'\';\"\n>\n<img src=\"'+ tmp +'\" width=32 height=32\n');
    document.write('name=\"i' + i + j + '\"\nborder=0></a>\n</td>\n');
    }
    document.write('<td bgcolor=\"cfcf7f\"><a href=\"javascript:explain(\'i' + i + '5\');\"\n');
    document.write('onmouseover=\"javascript:mouseguess(' + i + ');return true;\"\n');
    document.write('onmouseout=\"javascript:mouseguessout(' + i + ');\"\n>\n');
    document.write('<img src=\"' + tmp2 + '\" width=32 height=32\nname=\"i' + i + '5\"\nborder=0></a></td>');
    document.write('</tr>');
  }
document.write('<tr align=\"center\"  class=\"ft\">\n<td bgcolor=\"cfcf7f\" align=\"center\" colspan=5 class=\"ft\">\n');
document.write('<a href=\"mailto:jeff@jeffkendall.org?subject=mastermind_game\"\n');
document.write('onmouseover=\"document.images.jeffmail.src=\'jeffmaila.gif\';window.status=\'click to send mail to the author\';return true;\"\n');
document.write('onmouseout=\"document.images.jeffmail.src=\'jeffmail.gif\';window.status=\'\';\"\n');
document.write('><img name=\"jeffmail\" src=\"jeffmail.gif\" align=\"middle\"  alt=\"send email to the author\" border=0></a>\n');
document.write('<!--jeff@jeffkendall.org-->\n<!--/td-->');
document.write('<!--td-->\n<a href=\"javascript:guessit(\'' + 0 + '\');\"\n');
document.write('onmouseover=\"mouseguess(' + 0 + ');return true;\"\n');
document.write('onmouseout=\"mouseguessout(' + 0 +');return true;\"\n');
document.write('><img name=\"g' + 0 + '\" align=\"middle\" border=0 src=\"guessbtn.gif\" height=32 width=64></a>\n</td>');
document.write('</tr><tr align=\"center\"  class=\"ft\">');
document.write('<td bgcolor=\"cfcf7f\" class=\"ft\"  align=\"center\"  colspan=5>');
document.write('<a href=\"javascript:difficulty(0);\"\nonmouseover=\"ltmouse(1);return true;\"\n');
document.write('onmouseout=\"ltmouse(0);\"\n');
document.write('><img name=\"ltimg\" border=0 src=\"lt2.gif\" align=\"middle\" alt=\"decrease difficulty and restart\"></a>\n');
document.write('<a href=\"javascript:startover();\"');
document.write('onmouseover=\"diffmouse(1);window.status=\'click to reset game\';return true;\"');
document.write('onmouseout=\"diffmouse(0);window.status=\'\';\"\n>\n');
document.write('<img name=\"diff\" border=0 src=\"easy.gif\" align=\"middle\"  alt=\"restart at current difficulty setting\"><img name=\"diffnum\" src=\"n4.gif\" align=\"middle\" border=0>\n');
document.write('</a>\n<a href=\"javascript:difficulty(1);\"\nonmouseover=\"gtmouse(1);return true;\"\n');
document.write('onmouseout=\"gtmouse(0);\"\n');
document.write('><img name=\"gtimg\" border=0 src=\"gt.gif\" align=\"middle\"  alt=\"increase difficulty and restart\"></a>\n<!--/td-->');
document.write('<a href=\"mmhelp.htm\" target=\"_help\" \n');
document.write('onmouseover=\"window.status=\'click for help\';document.images.helpimg.src=\'helpbtna.gif\';return true;\" \n');
document.write('onmouseout=\"window.status=\'\';document.images.helpimg.src=\'helpbtn.gif\'\" \n>');
document.write('<img name=\"helpimg\"  align=\"middle\" alt=\"game rules, how to play...\" src=\"helpbtn.gif\" border=0></a>\n</td>\n');
document.write('</tr>\n');
document.write('</table>');
document.write('<a href=\"javascript:showtxt(\'' + base + '\');\">&nbsp;</a>');
document.forms.frm1.score.value='ready';
}
//*******************************************************
