<!-- 
// Flower Garden Game
// JavaScript copyright (c) 2000-2002 by Karen M. Glatt
// This game is not freeware nor is it in the public domain.
// If you would like to use this game on your web site, 
// contact KMG Associates at www.kmgassociates.com.


// global variables
var board = new Array(25);  // board with either dirt squares or flowers
var remaining = 15;			// remaining flowers to plant
var eraseMsg = 0;			// to remove error msgs
var numFlowerImgs = 5;		// # gif files w/flowers for random display

// Preload some of the images into global variables
var dirtblank = new Image();
dirtblank.src = "dirt.gif";
var flower0 = new Image();
flower0.src = "flower0.gif";
var flower1 = new Image();
flower1.src = "flower1.gif";
var flower2 = new Image();
flower2.src = "flower2.gif";
var flower3 = new Image();
flower3.src = "flower3.gif";
var flower4 = new Image();
flower4.src = "flower4.gif";

// Preload the error msgs
var msgblank = new Image();
msgblank.src = "msgbuy.gif";	// Put up ad to license game
var msgmore = new Image();
msgmore.src = "msgmore.gif";	// More flowers to plant
var msgrow = new Image();
msgrow.src = "msgrow.gif";		// Error in rows
var msgcol = new Image();
msgcol.src = "msgcol.gif";		// Error in columns
var msgdiag = new Image();
msgdiag.src = "msgdiag.gif";	// Error in diagonals
var msgwin = new Image();
msgwin.src = "msgwin.gif";		// It worked!

var sunnormal = new Image();
sunnormal.src = "sun.gif";
var sunsmile = new Image();
sunsmile.src = "sunsmile.gif";


// Clear the board position by displaying the dirt image
function ShowDirt(pos)
{
    // Put the image on the screen
    document.images[eval('"dirt' + pos + '"')].src = dirtblank.src;
}

// Show a random flower image on the board where indicated
function ShowFlower(pos)
{
	var randnum;

	// Pick a random flower
	randnum = Math.floor(Math.random() * numFlowerImgs);
    // Put the image on the screen
    document.images[eval('"dirt' + pos + '"')].src = eval('flower' + randnum + '.src');
}

// Displays the number of flowers remaining to be planted
function DisplayRemaining()
{
	document.form1.remaining.value = remaining;
}

// Initialize the game
function InitGame()
{
    var i;

    // Reset the global variables
    remaining = 15;
	DisplayRemaining();

	// Clear the board and display dirt
    for (i = 0; i < 25; ++i)
        {
		board[i] = 0;
        ShowDirt(i);
        }

	// Erase any msg
	document.msg.src = msgblank.src;
	document.sun.src = sunnormal.src;
}

// The player has clicked on a square to select it. If there is dirt
// currently in the square, put a flower there.  If there is a flower,
// remove it.
function Selected(pos)
{
	// Do we need to erase a msg?
	if (eraseMsg)
		{
		eraseMsg = 0;
		document.msg.src = msgblank.src;
		document.sun.src = sunnormal.src;
		}


	// Check if currently dirt, if so put flower there.
	if (board[pos] == 0)
		{
		if (remaining == 0)		// any flowers left to plant?
			return;				// no, ignore it
		board[pos] = 1;			// mark as used
		ShowFlower(pos);		// display a flower
		--remaining;			// one less flower to plant
		DisplayRemaining();
		}
	else
		{
		// There is a flower currently at this spot, remove it.
		board[pos] = 0;			// mark as free
		ShowDirt(pos);			// display dirt
		++remaining;			// one additional flower to plant
		DisplayRemaining();
		}
}

// Checks to make sure there are exactly 3 flowers in each
// row, column and diagonal.  Displays an appropriate msg.
function CheckBoard()
{
	var i, j;
	var inline;		// keeps track of how many in line

	eraseMsg = 1;	// no matter what, there will be a msg

	// There must have been 15 flowers planted.
	if (remaining != 0)
		{
		document.msg.src = msgmore.src;
		return;
		}

	// Check the rows
	for (i = 0; i < 5; ++i)
		{
		inline = 0;
		for (j = 0; j < 5; ++j)
			inline+= board[(i*5) + j];
		if (inline != 3)
			{
			document.msg.src = msgrow.src;
			return;
			}
		}

	// Check the columns
	for (j = 0; j < 5; ++j)
		{
		inline = 0;
		for (i = 0; i < 5; ++i)
			inline+= board[(i*5) + j];
		if (inline != 3)
			{
			document.msg.src = msgcol.src;
			return;
			}
		}

	// Check the diagonals
	inline = board[0] + board[6] + board[12] + board[18] + board[24];
	if (inline != 3)
		{
		document.msg.src = msgdiag.src;
		return;
		}
	inline = board[4] + board[8]+ board[12] + board[16]+ board[20]
	if (inline != 3)
		{
		document.msg.src = msgdiag.src;
		return;
		}
	
	// The board is correct!
	document.msg.src = msgwin.src;
	document.sun.src = sunsmile.src;
}


// End JavaScript -->

