<?xml version="1.0" encoding="UTF-8" ?> 
<Module>
  <ModulePrefs title="Tic Tac Toe Challenger">
  </ModulePrefs>
  <Content type="html">
     <![CDATA[ 
	<div id="content_div"></div>
	<script type="text/javascript">

	////////////////////////////////////////////////////////////
	//These are helper functions.  They can be used to 
	// mark pieces of the tic tac toe board, and to tell where
	// the other pieces are placed.
	// use them in the x_player_move and o_player_move functions
	////////////////////////////////////////////////////////////

	// tells us if this spot on the board is marked
	// with an X.  returns 1 if there is an X, 0 otherwise
	function is_x(board, row, column) {
		if (board[row][column] == "X") {
			return 1;
		}
		return 0;
	}

	// tells us if this spot on the board is marked
	// with an O.  returns 1 if there is an O, 0 otherwise
	function is_o(board, row, column) {
		if (board[row][column] == "O") {
			return 1;
		}
		return 0;
	}

	// Tells us if a spot on the board is empty
	// returns 1 if it is empty, 0 otherwise
	function is_empty(board, row, column) {
		if ((is_x(board,row,column) == 1) || 
		    (is_o(board,row,column) == 1)) {
			return 0;
		}
		return 1;
	}

	// Places and X on the board at the specified location
	// returns 1 if successful, 0 if there was already a 
	// mark there
	function set_x(board,row,column) {
		if (is_empty(board,row,column) == 1) {
			board[row][column] = "X";
			return 1;
		}
		return 0;
	}

	// Places and O on the board at the specified location
	// returns 1 if successful, 0 if there was already a 
	// mark there
	function set_o(board,row,column) {
		if (is_empty(board,row,column) == 1) {
			board[row][column] = "O";
			return 1;
		}
		return 0;
	}


	function dummy(board, chr) {

		var i,j;
		for (i=0;i<3;i++) {
			for (j=0;j<3;j++) {
				if (board[i][j] == "") {
					board[i][j] = chr;
					return;
				}
			}
		}
	}


	/////////////////////////////////////////////////////////////
	//THIS IS WHERE YOU COME IN
	//THESE ARE THE PLAYER MOVE FUNCTIONS.  The X player and the O 
	//PLAYERS INSERT CODE HERE TO PLACE X's and O's ON THE BOARD
	//IN ORDER TO WIN THE GAME
	//GOOD LUCK!
	////////////////////////////////////////////////////////////

	//X player move function
	function x_player_move(board) {
		dummy(board,"X");	
	}

	//O player move function
	function o_player_move(board) {
		dummy(board,"O");
	}

	/////////////////////////////////////////////////////////////
	//This is the actual game infrastructure below here.  
	//Feel free to poke about in this code if you like, but we'll
	//only talk about it in detail if we have time
	////////////////////////////////////////////////////////////	
	function print_board(board, win, stale, winner) {
		var i,j;

		var mytable, myrow, mycell;


		// Print out the winner
		var ctxt = document.createElement("center");
		var ctbl = document.createElement("center");
		if (stale == 1)
			ctxt.appendChild(document.createTextNode("STALEMATE!"));
		else if (win == 1)
			ctxt.appendChild(document.createTextNode(winner + " WINS!"));

		document.body.appendChild(ctxt);


		// Now display the board
		mytable = document.createElement("table");
		mytable.setAttribute("border","2");
		for (i=0;i<3;i++) {
			myrow = mytable.insertRow(i); //insert at end of table
			for (j=0;j<3;j++) {
				mycell = myrow.insertCell(j);
				mycell.innerHTML = board[i][j];
			}
		}
		ctbl.appendChild(mytable);
		document.body.appendChild(ctbl);			
		
	}

	function print_form() {
		var ctxt = document.createElement("center");
		var frm = document.createElement("FORM");
		var button = document.createElement("INPUT");
		button.setAttribute("TYPE","button");
		button.setAttribute("NAME","button");
		button.setAttribute("Value","Play Aagain");
		button.setAttribute("onClick","playGame(this.form)");
		ctxt.appendChild(button);
		frm.appendChild(ctxt);
		document.body.appendChild(frm);
        }

	// This checks to see if we have a winner
	function check_for_victory(board) {
		var i;
		var j;

		// First check for 3 in a row in the rows
		for(i=0;i<3;i++) {
			if ((board[i][0] != "") &&
			    (board[i][0] == board[i][1]) &&
			    (board[i][1] == board[i][2])) {
				return 1;
			}
		}

		// Now check the columns
		for(i=0;i<3;i++) {
			if ((board[0][i] != "") &&
			    (board[0][i] == board[1][i]) &&
			    (board[1][i] == board[2][i])) {
				return 1;
			}
		}

		// Check the diagonals
		if ((board[0][0] != "") &&
		    (board[0][0] == board[1][1]) &&
		    (board[1][1] == board[2][2])) {
			return 1;
		}

		if ((board[0][2] != "") &&
		    (board[0][2] == board[1][1]) &&
		    (board[1][1] == board[2][0])) {
			return 1;
		}

		return 0;
	}


	// This checks for a stalemate
	function check_for_stalemate(board) {

		// Stalemates happen when there is no victory
		// and the table is full
		var win;
		var i,j;
		win = check_for_victory(board);

		if (win == 1)
			return 0;

		for(i=0;i<3;i++)
			for(j=0;j<3;j++)
				if (board[i][j] == "")
					return 0;

		// No victory, and we couldn't find
		// an empty square  == stalemate
		return 1;
	}


	function sleep(seconds){

		seconds = seconds * 1000;
		var sleeping = true;
		var now = new Date();
		var alarm;
		var startingMSeconds = now.getTime();

		while(sleeping){

			alarm = new Date();
			alarmMSeconds = alarm.getTime();
			 if(alarmMSeconds - startingMSeconds > seconds){ sleeping = false; }

		}      	
	}

	// This is the entry point for the game
	function playGame(form) {

		// allocate the playing board
		var board = new Array(3);
		board[0] = new Array(3);
		board[1] = new Array(3);
		board[2] = new Array(3);

		// initalize it	
		var i, j;
		var stale=0;
		var win=0;
		var winner;
		var doc = document.getElementById("content_div");
		for(i=0;i<3;i++)
			for(j=0;j<3;j++)
				board[i][j] = "";	

		// Decide who goes first
		var rnd = Math.floor(Math.random()*10);
		var hnum = rnd /2;
		var hnumr = Math.round(hnum);
		var x_goesfirst;
		if (hnumr == hnum) {
			x_goesfirst = 1;
		} else {
			x_goesfirst = 0;
		}

		document.body = document.createElement("body");
		while (1) {
			// first player makes move
			if (x_goesfirst) {
				winner = "X";
				x_player_move(board);
			} else {
				winner = "O";
				o_player_move(board);
			}

			// Check for a victory condition
			win = check_for_victory(board)
			if (win)
				break;
			// Check for stalemate
			stale = check_for_stalemate(board);
			if (stale)
				break;

			// second player makes move
			if (!x_goesfirst) {
				winner = "X";
				x_player_move(board);
			} else {
				winner = "O";
				o_player_move(board);
			}

			// Check for a victory condition
			win = check_for_victory(board)
			if (win)
				break;
			// Check for stalemate
			stale = check_for_stalemate(board)
			if (stale)
				break;
		}

		// Print out the board and end conditions
		print_board(board, win, stale, winner);

		// Now reprint the form
		print_form();
	}


	</script>
	<FORM NAME="playgame" ACTION="" METHOD="GET"> 
	<center><INPUT TYPE="button" 
		NAME="button" 
		Value="Play Game" 
		onClick="playGame(this.form)"></center>
 	</FORM>
     ]]>
  </Content> 
</Module>
