| px | top | add code | search | signup | login | help |
<html>
<?php
$head = "Tic Tac Toe";
$db_host = "localhost";
$db_user = "dbuser";
$db_passwd = "dbpasswd";
$db_name = "dbname";
# $game_id = 1;
echo "<head> <title> $head </title> </head> <br> <b> $head </b> <br> <br>";
$db_server = mysql_connect($db_host, $db_user, $db_passwd) or
die("Could not connect to database server");
$db_select = mysql_select_db($db_name,$db_server) or
die ("Could not select database $db_name");
if ($button_label == "New Game") {
unset($button_label);
# for ($square_num = 0; $square_num < 9; $square_num++) {
# $sql_command = "UPDATE Games SET Square_$square_num=0 WHERE Game_ID=$game_id";
# $result = mysql_query($sql_command);
# } // end for
} //end if
if ($button_label) {
# test of what vars exist...
# while (list($name, $value) = each($HTTP_POST_VARS)) {
# echo "$name = $value<br>\n";
# } //end while
# determine who made the last move
# $x_or_o = 1 = X; 2 = O
if ($button_label == "Mark an X") {
$x_or_o = 1;
# echo '$x_or_o equals ';
# echo $x_or_o;
# echo "<br>";
}
else if ($button_label == "Mark an O") {
$x_or_o = 2;
# echo '$x_or_o equals ';
# echo $x_or_o;
# echo "<br>";
} // end if
# update the database with last player move
$sql_command = "UPDATE Games SET Square_$square_num=$x_or_o WHERE Game_ID=$game_id";
$result = mysql_query($sql_command);
# echo "Thank you! Information entered.\n<br>";
# check if game is over
for ($square_num = 0; $square_num < 9; $square_num++) {
$sql_command3 = "SELECT Square_$square_num FROM Games WHERE Game_ID = $game_id";
$sql_result3 = mysql_query($sql_command3);
$sql_xoro3 = mysql_result($sql_result3,0);
$array_1d[$square_num] = $sql_xoro3;
if ( $x_or_o == 1 ) {
if ( $array_1d[$square_num] != 1 ) {
$array_1d_conv[$square_num] = 0;
}
else {
$array_1d_conv[$square_num] = 1;
}
}
if ( $x_or_o == 2 ) {
if ( $array_1d[$square_num] != 2 ) {
$array_1d_conv[$square_num] = 0;
}
else {
$array_1d_conv[$square_num] = 1;
}
}
}
for ($j = 0; $j < 3; $j++) {
for ($i = 0; $i < 3; $i++) {
$array_2d[$i][$j] = $array_1d_conv[3*($j)+$i];
# echo $array_2d[$i][$j];
}
}
# echo "<br>";
# check for columns
$game_over = 0;
$draw = 0;
for ($j = 0; $j < 3; $j++) {
if ( $array_2d[0][$j] && $array_2d[1][$j] && $array_2d[2][$j] ) {
$game_over = 1;
}
}
# check for rows
for ($i = 0; $i < 3; $i++) {
if ( $array_2d[$i][0] && $array_2d[$i][1] && $array_2d[$i][2] ) {
$game_over = 1;
}
}
# check for diags
if ( $array_2d[0][0] && $array_2d[1][1] && $array_2d[2][2] ) {
$game_over = 1;
}
if ( $array_2d[0][2] && $array_2d[1][1] && $array_2d[2][0] ) {
$game_over = 1;
}
#check for draw
for ( $square_num = 0 ; $square_num < 9 && $array_1d[$square_num] ; $square_num++ ) {
# this is intentionally empty
# echo $square_num;
# echo $array_1d[$square_num];
}
if ( $square_num == 9 && $game_over == 0 ) {
$game_over = 1;
$draw = 1;
}
if ($game_over) {
if ($draw) {
echo "GAME OVER -- It's a draw.";
}
else {
if ($x_or_o == 1) {
echo "GAME OVER -- X wins!!!";
}
else {
echo "GAME OVER -- O wins!!!";
}
}
$button_label = '"New Game"';
}
# if not over switch to other player
else {
if ($button_label == "Mark an X") {
$x_or_o = 2;
$button_label = '"Mark an O"';
}
else {
$x_or_o = 1;
$button_label = '"Mark an X"';
} // end if
} // end if
} //end if
else { // new game is starting
$button_label = '"Mark an X"';
srand((double)microtime()*1000000);
$my_rand = rand(1,1000000);
$sql_command = "INSERT INTO Games SET new_game=$my_rand";
$sql_result = mysql_query($sql_command);
$sql_command = "SELECT Game_ID FROM Games WHERE new_game=$my_rand";
$sql_result = mysql_query($sql_command);
$game_id = mysql_result($sql_result, 0);
$sql_command = "UPDATE Games SET new_game=0 WHERE Game_ID=$game_id";
$sql_result = mysql_query($sql_command);
# echo "Game ID is $game_id";
} //end if
# display the tictactoe board
?>
<form action="<?php echo $PHP_SELF ?>" method="post">
<table border>
<?php
$square_num = 0;
for ($row_num = 0; $row_num < 3; $row_num++) {
echo "<tr>";
for($col_num = 0; $col_num < 3; $col_num++) {
echo "<th width=50 height=50>";
$sql_command2 = "SELECT Square_$square_num FROM Games WHERE Game_ID = $game_id";
$sql_result2 = mysql_query($sql_command2);
$sql_xoro = mysql_result($sql_result2,0);
if ($sql_xoro == 0) {
echo "<input type=radio name=square_num value=$square_num>";
}
else if ($sql_xoro == 1) {
echo "X";
}
else {
echo "O";
}
echo "</th>";
$square_num++;
}
echo "</tr>";
}
echo "</table> <input type='hidden' name=game_id value=$game_id> <input type='Submit' name=button_label value=$button_label>";
?>
</form>
</html>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.