ILLEGAL MOVE!

"; continue; // field already filled! } $field[lowest_row($col_count)][$col_count] = $token; $num_moves++; } } } } $won = check_winning(); if($num_moves == 1) { first_move(); $num_moves++; } if ($won == "" and $num_moves > 1 and $num_moves < 42 and $num_moves % 2 == 1) { make_movered(); $num_moves++; $won = check_winning(); } } //############################################################################## function random_move() { global $field; global $num_moves; $col = rand(0,6); if($field[0][$col] == "") {$row = lowest_row($col); $field[$row][$col] = "red"; return true;} else{ return false; } } ################################################################################# function lowest_row($col) { global $field; for ($row = 5; $row >= 0; $row--) { if ($field[$row][$col] == "") { return $row; } } } ################################################################################### ################################################################################## function check_row1() { global $field; global $num_moves; if (check_row111() == true) {return true;} elseif(check_row11() == true) {return true;} else {for($col = 0; $col <= 3; $col++) {if($field[5][$col] == "yellow" and $field[5][$col+1] == "yellow" and $field[5][$col+2] == "" and $field[5][$col+3] == "yellow") { $field[5][$col+2] = "red"; return true; } if($field[5][$col] == "yellow" and $field[5][$col+1] == "" and $field[5][$col+2] == "yellow" and $field[5][$col+3] == "yellow") { $field[5][$col+1] = "red"; return true; } } } return false; } ################################################################################## function check_row11() { global $field; global $num_moves; for($col = 0; $col <= 3; $col++) {if($field[5][$col] == "yellow" and $field[5][$col+1] == "yellow" and $field[5][$col+2] == "yellow" and $field[5][$col+3] == "") { $field[5][$col+3] = "red"; return true; } } return false; } ################################################################################## function check_row111() { global $field; global $num_moves; if($field[5][1] == "yellow" and $field[5][2] == "yellow" and $field[5][3] == "yellow" and $field[5][4] != "" and $field[5][0] == "") { $field[5][0] = "red"; return true; } if($field[5][2] == "yellow" and $field[5][3] == "yellow" and $field[5][4] == "yellow" and $field[5][5] != "" and $field[5][1] == "") { $field[5][1] = "red"; return true; } if($field[5][3] == "yellow" and $field[5][4] == "yellow" and $field[5][5] == "yellow" and $field[5][6] != "" and $field[5][2] == "") { $field[5][2] = "red"; return true; } if($field[5][4] == "yellow" and $field[5][5] == "yellow" and $field[5][6] == "yellow" and $field[5][3] == "") { $field[5][3] = "red"; return true; } return false; } ################################################################################# function check_row2() { global $field; global $num_moves; for ($row = 4; $row >= 0; $row--) { for($col = 0; $col <= 3; $col++) { if($field[$row][$col] == "yellow" and $field[$row][$col+1] == "yellow" and $field[$row][$col+2] == "yellow" and $field[$row][$col+3] == "" and $row == lowest_row($col+3)) {$field[$row][$col+3] = "red"; return true;} if($field[$row][$col] == "yellow" and $field[$row][$col+1] == "yellow" and $field[$row][$col+2] == "" and $field[$row][$col+3] == "yellow" and $row == lowest_row($col+2)) { $field[$row][$col+2] = "red"; return true;} if($field[$row][$col] == "yellow" and $field[$row][$col+1] == "" and $field[$row][$col+2] == "yellow" and $field[$row][$col+3] == "yellow" and $row == lowest_row($col+1)) { $field[$row][$col+1] = "red"; return true;} if($field[$row][$col] == "" and $field[$row][$col+1] == "yellow" and $field[$row][$col+2] == "yellow" and $field[$row][$col+3] == "yellow" and $row == lowest_row($col)) { $field[$row][$col] = "red"; return true;} } } return false; } ####################################################################################### function check_col() { global $field; global $num_moves; for($col = 6; $col >= 0; $col--) { for ($row = 0; $row <= 3; $row++) { if($field[$row+1][$col] == "yellow" and $field[$row+2][$col] == "yellow" and $field[$row+3][$col] == "yellow" and $field[$row][$col] == "") { $field[$row][$col] = "red"; return true; } } } return false; } ########################################################################################### function check_1diagonale() { global $field; global $num_moves; for($row = 0; $row <= 2; $row++) { for($col = 0; $col <=3; $col++) { if($field[$row][$col] == "yellow" and $field[$row+1][$col+1] == "yellow" and $field[$row+2][$col+2] == "yellow" and $field[$row+3][$col+3] == "" and $row+3 == lowest_row($col+3)) { $field[$row+3][$col+3] = "red"; return true; } if($field[$row][$col] == "" and $field[$row+1][$col+1] == "yellow" and $field[$row+2][$col+2] == "yellow" and $field[$row+3][$col+3] == "yellow" and $row == lowest_row($col)) { $field[$row][$col] = "red"; return true; } if($field[$row][$col] == "yellow" and $field[$row+1][$col+1] == "" and $field[$row+2][$col+2] == "yellow" and $field[$row+3][$col+3] == "yellow" and $row+1 == lowest_row($col+1)) { $field[$row+1][$col+1] = "red"; return true; } if($field[$row][$col] == "yellow" and $field[$row+1][$col+1] == "yellow" and $field[$row+2][$col+2] == "" and $field[$row+3][$col+3] == "yellow" and $row+2 == lowest_row($col+2)) { $field[$row+2][$col+2] = "red"; return true; } } } return false; } function check_2diagonale() { global $field; global $num_moves; for($row = 0; $row <= 2; $row++) { for($col = 0; $col <=3; $col++) { if($field[$row+3][$col] == "yellow" and $field[$row+2][$col+1] == "yellow" and $field[$row+1][$col+2] == "yellow" and $field[$row][$col+3] == "" and $row == lowest_row($col+3)) { $field[$row][$col+3] = "red"; return true; } if($field[$row+3][$col] == "" and $field[$row+2][$col+1] == "yellow" and $field[$row+1][$col+2] == "yellow" and $field[$row][$col+3] == "yellow" and $row+3 == lowest_row($col)) { $field[$row+3][$col] = "red"; return true; } if($field[$row+3][$col] == "yellow" and $field[$row+2][$col+1] == "" and $field[$row+1][$col+2] == "yellow" and $field[$row][$col+3] == "yellow" and $row+2 == lowest_row($col+1)) { $field[$row+2][$col+1] = "red"; return true; } if($field[$row+3][$col] == "yellow" and $field[$row+2][$col+1] == "yellow" and $field[$row+1][$col+2] == "" and $field[$row][$col+3] == "yellow" and $row+1 == lowest_row($col+2)) { $field[$row+1][$col+2] = "red"; return true; } } } return false; } ######################################################################################### function check_rowsforbeneath() { global $field; global $num_moves; for ($row = 0; $row <= 4; $row++) { for($col = 0; $col <= 3; $col++) { if($field[$row][$col] == "yellow" and $field[$row][$col+1] == "yellow" and $field[$row][$col+2] == "yellow" and $field[$row][$col+3] == "" and $field[$row+1][$col+3] == "") { $col2=random_movetwo(); while($col2 == $col+3) { $col2=random_movetwo(); } $row = lowest_row($col2); $field[$row][$col2] = "red"; return true; } if($field[$row][$col] == "yellow" and $field[$row][$col+1] == "yellow" and $field[$row][$col+2] == "" and $field[$row][$col+3] == "yellow" and $field[$row+1][$col+2] == "") { $col2=random_movetwo(); while($col2 == $col+2) { $col2=random_movetwo(); } $row = lowest_row($col2); $field[$row][$col2] = "red"; return true; } if($field[$row][$col] == "yellow" and $field[$row][$col+1] == "" and $field[$row][$col+2] == "yellow" and $field[$row][$col+3] == "yellow" and $field[$row+1][$col+1] == "") { $col2=random_movetwo(); while($col2 == $col+1) { $col2=random_movetwo(); } $row = lowest_row($col2); $field[$row][$col2] = "red"; return true; } if($field[$row][$col] == "" and $field[$row][$col+1] == "yellow" and $field[$row][$col+2] == "yellow" and $field[$row][$col+3] == "yellow" and $field[$row+1][$col] == "") { $col2=random_movetwo(); while($col2 == $col) { $col2=random_movetwo(); } $row = lowest_row($col2); $field[$row][$col2] = "red"; return true; } } } return false; } function random_movetwo() { global $field; global $num_moves; $col = rand(0,6); if($field[0][$col] == "") {$row = lowest_row($col); return $col; } else{ return false; } } ######################################################################################### ######################################################################################### function make_move() { global $field; global $num_moves; if(check_row1() == true) {return;} elseif(check_row2() == true) {return;} elseif(check_col() == true) {return;} elseif(check_1diagonale() == true) {return;} elseif(check_2diagonale() == true) {return;} elseif(check_rowsforbeneath() == true) {return;} elseif(check_fortwoinrow() == true) {return;} elseif(random_move() == true) {return;} else{make_move();} } ################################################################################### ################################################################################## //wenn computer gewinnen könnte, soll er stein so setzen, dass er gewinnt //anstatt zuerst gewinn des spielers zu verhindern function check_rowsforred() { global $field; global $num_moves; if(check_rowsred1() == true) {return true;} elseif(check_rowsred2() == true) {return true;} else{return false;} } ################################################################################ function check_rowsred1() { global $field; global $num_moves; if (check_rowsred111() == true) {return true;} elseif(check_rowsred11() == true) {return true;} else {for($col = 0; $col <= 3; $col++) if($field[5][$col] == "red" and $field[5][$col+1] == "red" and $field[5][$col+2] == "" and $field[5][$col+3] == "red") { $field[5][$col+2] = "red"; return true; } if($field[5][$col] == "red" and $field[5][$col+1] == "" and $field[5][$col+2] == "red" and $field[5][$col+3] == "red") { $field[5][$col+1] = "red"; return true; } } return false; } ################################################################################## function check_rowsred11() { global $field; global $num_moves; for($col = 0; $col <= 3; $col++) {if($field[5][$col] == "red" and $field[5][$col+1] == "red" and $field[5][$col+2] == "red" and $field[5][$col+3] == "") { $field[5][$col+3] = "red"; return true; } } return false; } ################################################################################## function check_rowsred111() { global $field; global $num_moves; if($field[5][1] == "red" and $field[5][2] == "red" and $field[5][3] == "red" and $field[5][4] != "" and $field[5][0] == "") { $field[5][0] = "red"; return true; } if($field[5][2] == "red" and $field[5][3] == "red" and $field[5][4] == "red" and $field[5][5] != "" and $field[5][1] == "") { $field[5][1] = "red"; return true; } if($field[5][3] == "red" and $field[5][4] == "red" and $field[5][5] == "red" and $field[5][6] != "" and $field[5][2] == "") { $field[5][2] = "red"; return true; } if($field[5][4] == "red" and $field[5][5] == "red" and $field[5][6] == "red" and $field[5][3] == "") { $field[5][3] = "red"; return true; } return false; } ################################################################################# function check_rowsred2() { global $field; global $num_moves; for ($row = 4; $row >= 0; $row--) { for($col = 0; $col <= 3; $col++) { if($field[$row][$col] == "red" and $field[$row][$col+1] == "red" and $field[$row][$col+2] == "red" and $field[$row][$col+3] == "" ) {if($row == lowest_row($col+3)) {$field[$row][$col+3] = "red"; return true;} return false;} if($field[$row][$col] == "red" and $field[$row][$col+1] == "red" and $field[$row][$col+2] == "" and $field[$row][$col+3] == "red") { if($row == lowest_row($col+2)) { $field[$row][$col+2] = "red"; return true;} return false; } if($field[$row][$col] == "red" and $field[$row][$col+1] == "" and $field[$row][$col+2] == "red" and $field[$row][$col+3] == "red") { if($row == lowest_row($col+1)) { $field[$row][$col+1] = "red"; return true;} return false;} if($field[$row][$col] == "" and $field[$row][$col+1] == "red" and $field[$row][$col+2] == "red" and $field[$row][$col+3] == "red") { if($row == lowest_row($col)) { $field[$row][$col] = "red"; return true;} return false; } } } return false; } #################################################################################### function check_colforred() { global $field; global $num_moves; for($col = 6; $col >= 0; $col--) { for ($row = 0; $row <= 3; $row++) { if($field[$row+1][$col] == "red" and $field[$row+2][$col] == "red" and $field[$row+3][$col] == "red" and $field[$row][$col] == "") { $field[$row][$col] = "red"; return true; } } } return false; } ###################################################################################### function check_1diagonaleforred() { global $field; global $num_moves; for($row = 0; $row <= 2; $row++) { for($col = 0; $col <=3; $col++) { if($field[$row][$col] == "red" and $field[$row+1][$col+1] == "red" and $field[$row+2][$col+2] == "red" and $field[$row+3][$col+3] == "" and $row+3 == lowest_row($col+3)) { $field[$row+3][$col+3] = "red"; return true; } if($field[$row][$col] == "" and $field[$row+1][$col+1] == "red" and $field[$row+2][$col+2] == "red" and $field[$row+3][$col+3] == "red" and $row == lowest_row($col)) { $field[$row][$col] = "red"; return true; } if($field[$row][$col] == "red" and $field[$row+1][$col+1] == "" and $field[$row+2][$col+2] == "red" and $field[$row+3][$col+3] == "red" and $row+1 == lowest_row($col+1)) { $field[$row+1][$col+1] = "red"; return true; } if($field[$row][$col] == "red" and $field[$row+1][$col+1] == "red" and $field[$row+2][$col+2] == "" and $field[$row+3][$col+3] == "red" and $row+2 == lowest_row($col+2)) { $field[$row+2][$col+2] = "red"; return true; } } } return false; } ################################################################################################# function check_2diagonaleforred() { global $field; global $num_moves; for($row = 0; $row <= 2; $row++) { for($col = 0; $col <=3; $col++) { if($field[$row+3][$col] == "red" and $field[$row+2][$col+1] == "red" and $field[$row+1][$col+2] == "red" and $field[$row][$col+3] == "" and $row == lowest_row($col+3)) { $field[$row][$col+3] = "red"; return true; } if($field[$row+3][$col] == "" and $field[$row+2][$col+1] == "red" and $field[$row+1][$col+2] == "red" and $field[$row][$col+3] == "red" and $row+3 == lowest_row($col)) { $field[$row+3][$col] = "red"; return true; } if($field[$row+3][$col] == "red" and $field[$row+2][$col+1] == "" and $field[$row+1][$col+2] == "red" and $field[$row][$col+3] == "red" and $row+2 == lowest_row($col+1)) { $field[$row+2][$col+1] = "red"; return true; } if($field[$row+3][$col] == "red" and $field[$row+2][$col+1] == "red" and $field[$row+1][$col+2] == "" and $field[$row][$col+3] == "red" and $row+1 == lowest_row($col+2)) { $field[$row+1][$col+2] = "red"; return true; } } } return false; } ################################################################################### ################################################################################### function make_movered() { global $field; global $num_moves; if(!check_foremptycol()) { if (!check_rowsforred()) { if (!check_colforred()) { if (!check_1diagonaleforred()) { if (!check_2diagonaleforred()) { make_move(); } } } } } } ############################################################################################## function check_foremptycol() { global $field; global $num_moves; if($field[0][0] == "" and $field[0][1] != "" and $field[0][2] != "" and $field[0][3] != "" and $field[0][4] != "" and $field[0][5] != "" and $field[0][6] != "") { $row = lowest_row(0); $field[$row][0] = "red"; return true;} if($field[0][0] != "" and $field[0][1] == "" and $field[0][2] != "" and $field[0][3] != "" and $field[0][4] != "" and $field[0][5] != "" and $field[0][6] != "") { $row = lowest_row(1); $field[$row][1] = "red"; return true;} if($field[0][0] != "" and $field[0][1] != "" and $field[0][2] == "" and $field[0][3] != "" and $field[0][4] != "" and $field[0][5] != "" and $field[0][6] != "") { $row = lowest_row(2); $field[$row][2] = "red"; return true;} if($field[0][0] != "" and $field[0][1] != "" and $field[0][2] != "" and $field[0][3] == "" and $field[0][4] != "" and $field[0][5] != "" and $field[0][6] != "") { $row = lowest_row(3); $field[$row][3] = "red"; return true;} if($field[0][0] != "" and $field[0][1] != "" and $field[0][2] != "" and $field[0][3] != "" and $field[0][4] == "" and $field[0][5] != "" and $field[0][6] != "") { $row = lowest_row(4); $field[$row][4] = "red"; return true;} if($field[0][0] != "" and $field[0][1] != "" and $field[0][2] != "" and $field[0][3] != "" and $field[0][4] != "" and $field[0][5] == "" and $field[0][6] != "") { $row = lowest_row(5); $field[$row][5] = "red"; return true;} if($field[0][0] != "" and $field[0][1] != "" and $field[0][2] != "" and $field[0][3] != "" and $field[0][4] != "" and $field[0][5] != "" and $field[0][6] == "") { $row = lowest_row(6); $field[$row][6] = "red"; return true;} return false; } ###################################################################################### function check_fortwoinrow() { global $field; global $num_moves; for ($row = 0; $row <= 5; $row++) { for ($col = 0; $col <= 4; $col++) { if($field[$row][$col] == "yellow" and $field[$row][$col+1] == "yellow" and $field[$row][$col+2] == "" and $row == lowest_row($col+2)) { $field[$row][$col+2] = "red"; return true; } if($field[$row][$col] == "" and $field[$row][$col+1] == "yellow" and $field[$row][$col+2] == "yellow" and $row == lowest_row($col)) { $field[$row][$col] = "red"; return true; } } } return false; } ################################################################################### ################################################################################# function first_move() { global $field; global $won; if($field[5][3] != "yellow") { $field[5][3] = "red"; return; } else { $field[5][2] = "red"; return; } } ################################################################################# function check_winning() { global $field; global $won; //check rows for($row = 0; $row < 6; $row++) { for($col = 0; $col < 4; $col++ ) { if($field[$row][$col] == $field[$row][$col+1] and $field[$row][$col] == $field[$row][$col+2] and $field[$row][$col] == $field[$row][$col+3]and $field[$row][$col] != "") { $won = $field[$row][$col]; return $won; } } } // Check cols for($col = 0; $col < 7; $col++) { for($row = 0; $row < 4; $row++) { if($field[$row][$col] == $field[$row+1][$col] and $field[$row][$col] == $field[$row+2][$col] and $field[$row][$col] == $field[$row+3][$col] and $field[$row][$col] != "") { $won = $field[$row][$col]; return $won; } } } //check diagonalen for($row = 0; $row <= 2; $row++) { for($col = 0; $col <=3; $col++) { if($field[$row][$col] == $field[$row+1][$col+1] and $field[$row][$col] == $field[$row+2][$col+2] and $field[$row][$col] == $field[$row+3][$col+3] and $field[$row][$col] != "") { $won = $field[$row][$col]; return $won; } if($field[$row+3][$col] == $field[$row+2][$col+1] and $field[$row+3][$col] == $field[$row+1][$col+2] and $field[$row+3][$col] == $field[$row][$col+3] and $field[$row+3][$col] != "") { $won = $field[$row+3][$col]; return $won; } } } return ""; } //############################################################################## function print_cell_content($row, $col) { global $field; $image = "empty444.jpg"; $value = ""; if($field[$row][$col] == "red") { $image = "red111.jpg"; $value = "red"; } if($field[$row][$col] == "yellow") { $image = "yellow111.jpg"; $value = "yellow"; } $move = "move_".$row."_".$col; echo ""; $name = "field_".$row."_".$col; echo ""; echo "\n"; } ?>

Connect 4

"; ?>


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

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

The game has ended in a draw.

"; } ?>