ILLEGAL MOVE!

"; continue; // field already filled! } $field[$row_count][$col_count] = $token; $num_moves++; } } } } $won = check_winning(); if ($num_moves == 1) { move1(); $num_moves++; } if ($won == "" and $num_moves == 3 and $num_moves < 9) { movetwo(); $num_moves++; } if ($won == "" and $num_moves >= 5 and $num_moves < 9) { make_moveo(); $num_moves++; $won = check_winning(); } } //############################################################################## function random_move() { global $field; global $num_moves; do { $row = rand(0,2); $col = rand(0,2); } while($field[$row][$col]); $token = ""; if($num_moves % 2) $token = "o"; else $token = "x"; $field[$row][$col] = $token; return; } ################################################################################## function check_rows() { global $field; global $num_moves; for($row = 0; $row < 3; $row++) { if($field[$row][0] == "x" and $field[$row][1] == "x" and $field[$row][2] == "") { $field[$row][2] = "o"; return true; } if($field[$row][1] == "x" and $field[$row][2] == "x"and $field[$row][0] == "") { $field[$row][0] = "o"; return true; } if($field[$row][0] == "x" and $field[$row][2] == "x" and $field[$row][1] == "") { $field[$row][1] = "o"; return true; } } return false; } ####################################################################################### function check_col() { global $field; global $num_moves; for($col = 0; $col < 3; $col++) { if($field[0][$col] == "x" and $field[1][$col] == "x" and $field[2][$col] == "") { $field[2][$col] = "o"; return true; } if($field[1][$col] == "x" and $field[2][$col] == "x" and $field[0][$col] == "") { $field[0][$col] = "o"; return true; } if($field[0][$col] == "x" and $field[2][$col] == "x" and $field[1][$col] == "") { $field[1][$col] = "o"; return true; } } return false; } ########################################################################################### function check_1diagonale() { global $field; global $num_moves; if($field[0][0] == "x" and $field[1][1] == "x" and $field[2][2] == "") { $field[2][2] = "o"; return true; } if($field[1][1] == "x" and $field[2][2] == "x" and $field[0][0] == "") { $field[0][0] = "o"; return true; } if($field[0][0] == "x" and $field[2][2] == "x" and $field[1][1] == "") { $field[1][1] = "o"; return true; } return false; } ########################################################################################### function check_2diagonale() { global $field; global $num_moves; if($field[0][2] == "x" and $field[1][1] == "x" and $field[2][0] == "") { $field[2][0] = "o"; return true; } if($field[1][1] == "x" and $field[2][0] == "x" and $field[0][2] == "") { $field[0][2] = "o"; return true; } if($field[0][2] == "x" and $field[2][0] == "x" and $field[1][1] == "") { $field[1][1] = "o"; return true; } return false; } ######################################################################################### function make_move() { global $field; global $num_moves; if (!check_rows()) { if (!check_col()) { if (!check_1diagonale()) { if (!check_2diagonale()) { random_move(); } } } } } ################################################################################# //wenn spieler in eine ecke setzt, soll computer in die mitte setzen, //und wenn spieler in die mitte setzt, soll computer in eine ecke setzen function move1() { global $field; global $num_moves; if ($field[0][0] == "x" or $field[0][2] == "x" or $field[2][0] == "x" or $field[2][2] == "x" or $field[0][1] == "x" or $field [1][2] == "x" or $field [2][1] == "x" or $field [1][0] == "x") { $field[1][1] = "o"; return; } else { $field [0][0] = "o"; return; } } ################################################################################# //wenn computer gewinnen könnte, soll er stein so setzen, dass er gewinnt //anstatt zuerst gewinn des spielers zu verhindern function check_rowsforo() { global $field; global $num_moves; for($row = 0; $row < 3; $row++) { if($field[$row][0] == "o" and $field[$row][1] == "o" and $field[$row][2] == "") { $field[$row][2] = "o"; return true; } if($field[$row][1] == "o" and $field[$row][2] == "o"and $field[$row][0] == "") { $field[$row][0] = "o"; return true; } if($field[$row][0] == "o" and $field[$row][2] == "o" and $field[$row][1] == "") { $field[$row][1] = "o"; return true; } } return false; } ################################## function check_colforo() { global $field; global $num_moves; for($col = 0; $col < 3; $col++) { if($field[0][$col] == "o" and $field[1][$col] == "o" and $field[2][$col] == "") { $field[2][$col] = "o"; return true; } if($field[1][$col] == "o" and $field[2][$col] == "o" and $field[0][$col] == "") { $field[0][$col] = "o"; return true; } if($field[0][$col] == "o" and $field[2][$col] == "o" and $field[1][$col] == "") { $field[1][$col] = "o"; return true; } } return false; } ####################################### function check_1diagonaleforo() { global $field; global $num_moves; if($field[0][0] == "o" and $field[1][1] == "o" and $field[2][2] == "") { $field[2][2] = "o"; return true; } if($field[1][1] == "o" and $field[2][2] == "o" and $field[0][0] == "") { $field[0][0] = "o"; return true; } if($field[0][0] == "o" and $field[2][2] == "o" and $field[1][1] == "") { $field[1][1] = "o"; return true; } return false; } ######################################## function check_2diagonaleforo() { global $field; global $num_moves; if($field[0][2] == "o" and $field[1][1] == "o" and $field[2][0] == "") { $field[2][0] = "o"; return true; } if($field[1][1] == "o" and $field[2][0] == "o" and $field[0][2] == "") { $field[0][2] = "o"; return true; } if($field[0][2] == "o" and $field[2][0] == "o" and $field[1][1] == "") { $field[1][1] = "o"; return true; } return false; } ############################################ function make_moveo() { global $field; global $num_moves; if (!check_rowsforo()) { if (!check_colforo()) { if (!check_1diagonaleforo()) { if (!check_2diagonaleforo()) { make_move(); } } } } } ################################################################################ function movetwo() { global $field; global $num_moves; if($field[2][0] == "x" and $field[0][2] == "x") { $field[0][1] = "o"; return true; } if($field[0][0] == "x" and $field[2][2] == "x") { $field[0][1] = "o"; return true; } if($field[1][1] == "x" and $field[2][2] == "x") { $field[2][0] = "o"; return true; } if($field[2][0] == "x" and $field[1][2] == "x" or $field[2][1] == "x" and $field[0][2] == "x") { $field[2][2] = "o"; return true; } if($field[2][0] == "x" and $field[0][1] == "x" or $field[1][0] == "x" and $field[0][2] == "x") { $field[0][0] = "o"; return true; } if($field[1][0] == "x" and $field[2][2] == "x" or $field[2][1] == "x" and $field[0][0] == "x") { $field[2][0] = "o"; return true; } if($field[2][2] == "x" and $field[0][1] == "x" or $field[0][0] == "x" and $field[1][2] == "x") { $field[0][2] = "o"; return true; } if($field[1][0] == "x" and $field [0][1] == "x") { $field[0][0] = "o"; return true; } if($field[1][2] == "x" and $field [0][1] == "x") { $field[0][2] = "o"; return true; } if($field[2][1] == "x" and $field [1][2] == "x") { $field[2][2] = "o"; return true; } if($field[1][0] == "x" and $field [2][1] == "x") { $field[2][0] = "o"; return true; } make_moveo(); } ################################################################################# function check_winning() { global $field; global $won; // Check winning conditions by exhaustive enumeration // not very scalable, but very simple. // Check rows for($row = 0; $row < 3; $row++) { if($field[$row][0] == $field[$row][1] and $field[$row][0] == $field[$row][2] and $field[$row][0] != "") { $won = $field[$row][0]; return $won; } } // Check cols for($col = 0; $col < 3; $col++) { if($field[0][$col] == $field[1][$col] and $field[0][$col] == $field[2][$col] and $field[0][$col] != "") { $won = $field[0][$col]; return $won; } } // Check diagonal 1 if($field[0][0] == $field[1][1] and $field[0][0] == $field[2][2] and $field[0][0] != "") { $won = $field[0][0]; return $won; } // Check diagonal 2 if($field[0][2] == $field[1][1] and $field[0][2] == $field[2][0] and $field[0][2] != "") { $won = $field[0][2]; return $won; } return ""; } //############################################################################## function print_cell_content($row, $col) { global $field; $image = "emptyt.jpg"; $value = ""; if($field[$row][$col] == "x") { $image = "x1.jpg"; $value = "x"; } if($field[$row][$col] == "o") { $image = "o1.jpg"; $value = "o"; } $move = "move_".$row."_".$col; echo ""; $name = "field_".$row."_".$col; echo ""; echo "\n"; } ?>

Tic Tac Toe

"; ?>



The game is won by player ".strtoupper($won).".

\n"; } if($won == "" and $num_moves == 9) { echo "

The game has ended in a draw.

"; } ?>