// JavaScript Codebreaker.
// Copyright (C) 2007 Thunor <thunorsif@hotmail.com>
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

// --- Global constants. ---// --- Can't use 'const' here as IE doesn't like it so must use 'var' :s ---

var mmdebug = false;
var mmlowestlevel = 3;
var mmhighestlevel = 7;
var mmhighestturn = 10;
// --- Global variables. ---
var mmcurrentlevel = mmlowestlevel;
var mmcurrentturn = 1;
var mmcurrentpeg = 1;
var mmuserarray = new Array(mmhighestlevel);
var mmanswerarray = new Array(mmhighestlevel);
var mmgameover = false;
var mmshowallplaces = false;
var mmuniquecolours = true;
var mmgamesplayed = 0;
var mmaccumulatedturns = 0;

// --- Load all images used within document. ---
mmblank = new Image(); mmblank.src = "images/blankx1y1.gif";

mmnew1 = new Image(); mmnew1.src = "images/new1.gif";
mmnew2 = new Image(); mmnew2.src = "images/new2.gif";
mmnew3 = new Image(); mmnew3.src = "images/new3.gif";

mmnumber0 = new Image(); mmnumber0.src = "images/0.gif";
mmnumber1 = new Image(); mmnumber1.src = "images/1.gif";
mmnumber2 = new Image(); mmnumber2.src = "images/2.gif";
mmnumber3 = new Image(); mmnumber3.src = "images/3.gif";
mmnumber4 = new Image(); mmnumber4.src = "images/4.gif";
mmnumber5 = new Image(); mmnumber5.src = "images/5.gif";
mmnumber6 = new Image(); mmnumber6.src = "images/6.gif";
mmnumber7 = new Image(); mmnumber7.src = "images/7.gif";
mmnumber8 = new Image(); mmnumber8.src = "images/8.gif";
mmnumber9 = new Image(); mmnumber9.src = "images/9.gif";

mmpeg00 = new Image(); mmpeg00.src = "images/peg00.gif";
mmpeg01 = new Image(); mmpeg01.src = "images/peg01.gif";
mmpeg02 = new Image(); mmpeg02.src = "images/peg02.gif";
mmpeg03 = new Image(); mmpeg03.src = "images/peg03.gif";
mmpeg04 = new Image(); mmpeg04.src = "images/peg04.gif";
mmpeg1 = new Image(); mmpeg1.src = "images/peg1.gif";
mmpeg2 = new Image(); mmpeg2.src = "images/peg2.gif";
mmpeg3 = new Image(); mmpeg3.src = "images/peg3.gif";
mmpeg4 = new Image(); mmpeg4.src = "images/peg4.gif";
mmpeg5 = new Image(); mmpeg5.src = "images/peg5.gif";
mmpeg6 = new Image(); mmpeg6.src = "images/peg6.gif";
mmpeg7 = new Image(); mmpeg7.src = "images/peg7.gif";

mmucno =  new Image(); mmucno.src = "images/no.gif";
mmucyes =  new Image(); mmucyes.src = "images/yes.gif";

// ------------------------------------------------------------------
// Functions
// ------------------------------------------------------------------

function mmaction() {
	var colloop = 0;
	var count = 0;
	var winflag = false;

	if (mmgameover == false) {	
		// --- Have all pegs been set? ---
		if (mmcurrentpeg > mmcurrentlevel) {
			if (mmdebug == true) {
				alert(mmuserarray);
			}	
			mmshowclues();

			// --- Compare arrays for correct match. ---
			winflag = true;
			for (count = 0; count <= mmcurrentlevel - 1; count++) {
				if (mmanswerarray[count] != mmuserarray[count]) {
					winflag = false;
				}
			}

			// --- Check for game over (no more turns OR match found). ---
			if (mmcurrentturn >= mmhighestturn || winflag == true) {
				for (count = 0; count <= mmcurrentlevel -1; count++) {
					document.images["mmpeg" + (mmhighestturn + 1) + (count + 1)].src = eval("mmpeg" + mmanswerarray[count]).src;
				}
				document.images["mmaction1"].src = mmnew1.src;
				document.images["mmaction2"].src = mmnew2.src;
				document.images["mmaction3"].src = mmnew3.src;

				mmgameover = true;
				mmgamesplayed++;
				mmaccumulatedturns = mmaccumulatedturns + mmcurrentturn;
				mmdisplaygamesplayed();
				mmdisplaywinavg();
				// Save the stats to a cookie.			
				cookiemanager("write", "cbgamesplayed", mmgamesplayed, 366);
				cookiemanager("write", "cbaccumulatedturns", mmaccumulatedturns, 366);
			}
			else {
				mmcurrentpeg = 1;
				mmcurrentturn++;

				if (!mmshowallplaces) {
					// --- Show next set of empty places for input. ---
					for (colloop = 1; colloop <= mmcurrentlevel; colloop++) {
						if (mmcurrentturn == 11) {
							document.images["mmpeg" + mmcurrentturn + colloop].src = mmpeg04.src;
						}
						else if (mmcurrentturn >= 8 && mmcurrentturn <= 10) {
							document.images["mmpeg" + mmcurrentturn + colloop].src = mmpeg03.src;
						}
						else if (mmcurrentturn >= 5 && mmcurrentturn <= 7) {
							document.images["mmpeg" + mmcurrentturn + colloop].src = mmpeg02.src;
						}
						else if (mmcurrentturn >= 3 && mmcurrentturn <= 4) {
							document.images["mmpeg" + mmcurrentturn + colloop].src = mmpeg01.src;
						}
						else if (mmcurrentturn >= 1 && mmcurrentturn <= 2) {
							document.images["mmpeg" + mmcurrentturn + colloop].src = mmpeg00.src;
						}
					}
				}
			}
		}
	}
	else {
		mmreset();
	}
}

function mmshowclues() {
	var count = 0;
	var count2 = 0;
	var colorclue = 0;
	var placeclue = 0;
	var tempuserarray = new Array(mmhighestlevel);
	var tempanswerarray = new Array(mmhighestlevel);

	// --- We need to use temp arrays as we will change their contents.
	// Matching pegs will be set to zero within the array as each
	// peg can only be compared once. ---
	for (count = 0; count <= mmcurrentlevel - 1; count++) {
		tempuserarray[count] = mmuserarray[count];
		tempanswerarray[count] = mmanswerarray[count];
	}

	// --- Check for exactly matched pegs (colour and place). ---
	for (count = 0; count <= mmcurrentlevel - 1; count++) {
		for (count2 = 0; count2 <= mmcurrentlevel - 1; count2++) {
			if (tempuserarray[count] == tempanswerarray[count2] && count == count2 && tempuserarray[count] != 0 && tempanswerarray[count2] != 0) {
				colorclue++;
				placeclue++;				
				tempuserarray[count] = 0;		// erase entry so no more compare.
				tempanswerarray[count2] = 0;	// erase entry so no more compare.
			}
		}
	}

	// --- Check for matched colour pegs (colour and not place). ---
	for (count = 0; count <= mmcurrentlevel - 1; count++) {
		for (count2 = 0; count2 <= mmcurrentlevel - 1; count2++) {
			if (tempuserarray[count] == tempanswerarray[count2] && count != count2 && tempuserarray[count] != 0 && tempanswerarray[count2] != 0) {
				colorclue++;
				tempuserarray[count] = 0;		// erase entry so no more compare.
				tempanswerarray[count2] = 0;	// erase entry so no more compare.
			}
		}
	}
	
	// --- Display results. ---
	document.images["mmcolorclue" + mmcurrentturn].src = eval("mmnumber" + colorclue).src
	document.images["mmplaceclue" + mmcurrentturn].src = eval("mmnumber" + placeclue).src
}

function mminitialiseanswerarray() {
	var count = 0;
	var swapidx = 0;
	var tempval = 0;
	
	if (!mmuniquecolours) {
		// any numbers will do, duplicates accepted.
		for (count = 0; count <= mmcurrentlevel - 1; count++) {
			mmanswerarray[count] = Math.round(Math.random() * 6) + 1;
		}
	}
	else {
		// first make an array of all the possible numbers.
		for (count = 0; count <= mmhighestlevel - 1; count++) {
			mmanswerarray[count] = count + 1;
		}
		// now mix them all up
		for (count = 0; count <= mmhighestlevel - 1; count++) {
			swapidx = Math.round(Math.random() * 6);
			tempval = mmanswerarray[count];
			mmanswerarray[count] = mmanswerarray[swapidx];
			mmanswerarray[swapidx] = tempval;
		}
	}	
	if (mmdebug == true) {
		alert(mmanswerarray);
	}
}

function mmback() {
	if (mmgameover == false && mmcurrentpeg > 1) {
		mmcurrentpeg--;
		if (mmcurrentturn == 11) {
			document.images["mmpeg" + mmcurrentturn + mmcurrentpeg].src = mmpeg04.src;
		}
		else if (mmcurrentturn >= 8 && mmcurrentturn <= 10) {
			document.images["mmpeg" + mmcurrentturn + mmcurrentpeg].src = mmpeg03.src;
		}
		else if (mmcurrentturn >= 5 && mmcurrentturn <= 7) {
			document.images["mmpeg" + mmcurrentturn + mmcurrentpeg].src = mmpeg02.src;
		}
		else if (mmcurrentturn >= 3 && mmcurrentturn <= 4) {
			document.images["mmpeg" + mmcurrentturn + mmcurrentpeg].src = mmpeg01.src;
		}
		else if (mmcurrentturn >= 1 && mmcurrentturn <= 2) {
			document.images["mmpeg" + mmcurrentturn + mmcurrentpeg].src = mmpeg00.src;
		}
	}
}

function mmsetpeg (pegnumber) {
	if (mmgameover == false && mmcurrentpeg <= mmcurrentlevel) {
		document.images["mmpeg" + mmcurrentturn + mmcurrentpeg].src = eval("mmpeg" + pegnumber).src
		mmuserarray[mmcurrentpeg - 1] = pegnumber;
		mmcurrentpeg++;
	}
}

function mmreset() {
	var colloop = 1;
	var rowloop = 1;
	var count = 0;

	for (rowloop = 1; rowloop <= mmhighestturn + 1; rowloop++) {
		for (colloop = 1; colloop <= mmhighestlevel; colloop++) {
			if ((colloop <= mmcurrentlevel && mmshowallplaces) || (colloop <= mmcurrentlevel && !mmshowallplaces && (rowloop == 1 || rowloop == mmhighestturn + 1))) {
				if (rowloop == 11) {
					document.images["mmpeg" + rowloop + colloop].src = mmpeg04.src;
				}
				else if (rowloop >= 8 && rowloop <= 10) {
					document.images["mmpeg" + rowloop + colloop].src = mmpeg03.src;
				}
				else if (rowloop >= 5 && rowloop <= 7) {
					document.images["mmpeg" + rowloop + colloop].src = mmpeg02.src;
				}
				else if (rowloop >= 3 && rowloop <= 4) {
					document.images["mmpeg" + rowloop + colloop].src = mmpeg01.src;
				}
				else if (rowloop >= 1 && rowloop <= 2) {
					document.images["mmpeg" + rowloop + colloop].src = mmpeg00.src;
				}
			}
			else {
				document.images["mmpeg" + rowloop + colloop].src = mmblank.src;
			}
		}
	}
	for (count = 1; count <= mmhighestturn; count++) {
		document.images["mmcolorclue" + count].src = mmblank.src
		document.images["mmplaceclue" + count].src = mmblank.src
	}
	document.images["mmlevel"].src = eval("mmnumber" + mmcurrentlevel).src;
	document.images["mmaction1"].src = mmblank.src;
	document.images["mmaction2"].src = mmblank.src;
	document.images["mmaction3"].src = mmblank.src;
	mmdisplaygamesplayed();
	mmdisplaywinavg();
	
	mmcurrentturn = 1;
	mmcurrentpeg = 1;
	for (count = 0; count <= mmhighestlevel - 1; count++) {
		mmuserarray[count] = 0;
		mmanswerarray[count] = 0;
	}
	mminitialiseanswerarray();
	mmgameover = false;
}

function mmlevelup() {
	if (mmcurrentlevel < mmhighestlevel) {
		mmcurrentlevel++;
		mmreset();
	}
}

function mmleveldn() {
	if (mmcurrentlevel > mmlowestlevel) {
		mmcurrentlevel--;
		mmreset();
	}
}

function mmdisplaygamesplayed() {
	var units100s = 0;
	var units10s = 0;
	var units1s = 0;

	units100s = Math.floor(mmgamesplayed % 1000 / 100);	// 'mod'ed to isolate the units.
	units10s =  Math.floor(mmgamesplayed % 100 / 10);
	units1s = Math.floor(mmgamesplayed % 10);
	document.images["mmgp100"].src = eval("mmnumber" + units100s + ".src");
	document.images["mmgp10"].src = eval("mmnumber" + units10s + ".src");
	document.images["mmgp1"].src = eval("mmnumber" + units1s + ".src");
}

function mmdisplaywinavg() {
	var winavg = 0;
	var units10s = 0;
	var units1s = 0;
	var units10ths = 0;
	var units100ths = 0;
	var units1000ths = 0;

	if (mmgamesplayed) {	// prevent divide-by-zero.
		winavg = mmaccumulatedturns / mmgamesplayed;
	}
	units10s =  Math.floor(winavg % 100 / 10);	// 'mod'ed to isolate the units.
	units1s = Math.floor(winavg % 10);
	units10ths =  Math.floor(winavg * 10 % 10);
	units100ths =  Math.floor(winavg * 100 % 10);
	units1000ths =  Math.floor(winavg * 1000 % 10);
	document.images["mmat10"].src = eval("mmnumber" + units10s + ".src");
	document.images["mmat1"].src = eval("mmnumber" + units1s + ".src");
	document.images["mmat10th"].src = eval("mmnumber" + units10ths + ".src");
	document.images["mmat100th"].src = eval("mmnumber" + units100ths + ".src");
	document.images["mmat1000th"].src = eval("mmnumber" + units1000ths + ".src");
}

function mmsetuq(state) {
	if (!((!state && !mmuniquecolours) || (state && mmuniquecolours))) {
		if (!state) {
			mmuniquecolours = state;
			document.images["mmucno"].src = mmucyes.src;
			document.images["mmucyes"].src = mmucno.src;
		}
		else {
			mmuniquecolours = state;
			document.images["mmucno"].src = mmucno.src;
			document.images["mmucyes"].src = mmucyes.src;
		}		
		mmreset();
	}
}

function mmresetcookie() {
	if (confirm("Are you sure you want to reset the cookie?")) {
		cookiemanager('write', 'cbgamesplayed', 0, 0);
		cookiemanager('write', 'cbaccumulatedturns', 0, 0);
	}
}

