s0n Posted April 10, 2008 Share Posted April 10, 2008 can someone please look at this and tell me whats wrong, getting an offset error and nothing seems to work. someone please look at this asap. THANK YOU. <html> <head> <title>U can't win Tic-Tac-Toe</title> <script language="javascript"type="text/javascript" src="tictactoe.js"></script> <link rel="stylesheet" type="text/css" href="tictactoe.css" </head> <body> <?php $x = "X"; $o = "O"; //My Tic Tac Toe Array $squares = array("", "", "", "", "", "", "", "", ""); //My Post Variables if($HTTP_POST_VARS) { for($i = 0; $i < count($squares); $i++) { $squares[$i]= $HTTP_POST_VARS['move'.$i]; } } //Runs the check for win functions and check for finish function. $winner = checkForWin($squares); $finish = checkfinish($squares); if($winner == '' && !$finish) { if(!isFirstMove($squares)) { $move = makeMove($squares); $squares[$move] = 'o'; $winner = checkForWin($squares); } } //Generates My Table for Tic Tac Toe echo "<form name=tictactoe method=post action=tictactoe.php>"; echo "<input type=hidden name=play value=".$winner.">"; echo "<table cellspacing=0 cellpadding=0 border=0 width=300 height=300>"; for($i = 0; $i < count($squares); $i++) { if(($i+1) % 3 == 1) echo '<tr>'; echo '<td>'; echo "<input value='" .$squares[$i]."'type=button name=move class=table onclick=makeMove('move".$i."')>"; echo '<input type=hidden name=\'move'.$i.'\' value=\''.$squares[$i].'\'>'; echo '</td>'; if(($i+1) % 3 == 0) echo '</tr>'; } echo "</table>"; echo "</form>"; echo "<br>"; echo "<font size=6"; //Message that comes up after a tie or loss if($winner == 'o') { echo 'O Wins, You n00b'; echo "<br>"; echo "<a href=http://www.3nfconsulting.com/ftp/web2/sunny/tictactoe.php>Play Again</a>"; } else if($winner == '' && $finish) { echo 'Tie Game'; echo "<br>"; echo "<a href=http://www.3nfconsulting.com/ftp/web2/sunny/tictactoe.php>Play Again</a>"; } echo "</font></center>"; // check if someone won, it checks by make sure by checking //each node in the array to see if it matches with the //other node and if they match it returns a winner //it goes through all 8 scenarios of winning function checkForWin($squaresarray) { $winner = ''; if($squaresarray[0] == $squaresarray[1] && $squaresarray[1] == $squaresarray[2]) $winner = $squaresarray[0]; else if($squaresarray[3] == $squaresarray[4] && $squaresarray[4] == $squaresarray[5]) $winner = $squaresarray[3]; else if($squaresarray[6] == $squaresarray[7] && $squaresarray[7] == $squaresarray[8]) $winner = $squaresarray[6]; else if($squaresarray[0] == $squaresarray[3] && $squaresarray[3] == $squaresarray[6]) $winner = $squaresarray[0]; else if($squaresarray[1] == $squaresarray[4] && $squaresarray[4] == $squaresarray[7]) $winner = $squaresarray[1]; else if($squaresarray[2] == $squaresarray[5] && $squaresarray[5] == $squaresarray[8]) $winner = $squaresarray[2]; else if($squaresarray[0] == $squaresarray[4] && $squaresarray[4] == $squaresarray[8]) $winner = $squaresarray[0]; else if($squaresarray[2] == $squaresarray[4] && $squaresarray[4] == $squaresarray[6]) $winner = $squaresarray[2]; return $winner; } function isFirstMove($squarearray) { // check if this is the first move $firstmove = true; for($i = 0; $i < count($squarearray); $i++) { if($squarearray[$i] != '') $firstmove = false; } return $firstmove; } function makeMove($squarearray) { global $x; global $o; if($squarearray[4] != $x) { $squarearray[4] = $o; } else { $squarearray[6] = $o; } } function checkfinish($squaresarray) { $finish = true; for($i = 0; $i < count($squaresarray); $i++) { if($squaresarray[$i] == '') $finish = false; } return $finish; } function blockMoves() { global $x; global $o; if( (($squaresarray[0] == "") && ($squaresarray[3] ==$x) && ($squaresarray[6] == $x)) || (($squaresarray[0] == "") && ($squaresarray[4] ==$x) && ($squaresarray[8] == $x)) || (($squaresarray[0] == "") && ($squaresarray[1] ==$x) && ($squaresarray[2] == $x)) ) { $squaresarray[0] = 'o'; } else { if( (($squaresarray[1] == "") && ($squaresarray[0] ==$x) && ($squaresarray[2] == $x)) || (($squaresarray[1] == "") && ($squaresarray[4] ==$x) && ($squaresarray[7] == $x)) ) { $squaresarray[1] = 'o'; } else { if( (($squaresarray[2] == "") && ($squaresarray[0] ==$x) && ($squaresarray[1] == $x)) || (($squaresarray[2] == "") && ($squaresarray[5] ==$x) && ($squaresarray[8] == $x)) || (($squaresarray[2] == "") && ($squaresarray[4] ==$x) && ($squaresarray[6] == $x)) ) { $squaresarray[2] = 'o'; } else { if( (($squaresarray[3] == "") && ($squaresarray[0] ==$x) && ($squaresarray[6] == $x)) || (($squaresarray[3] == "") && ($squaresarray[4] ==$x) && ($squaresarray[5] == $x)) ) { $squaresarray[3] = 'o'; } else { if( (($squaresarray[4] == "") && ($squaresarray[3] ==$x) && ($squaresarray[5] == $x)) || (($squaresarray[4] == "") && ($squaresarray[1] ==$x) && ($squaresarray[7] == $x)) ) { $squaresarray[4] = 'o'; } else { if( (($squaresarray[5] == "") && ($squaresarray[2] ==$x) && ($squaresarray[8] == $x)) || (($squaresarray[5] == "") && ($squaresarray[3] == $x) && ($squaresarray[4] == $x)) ) { $squaresarray[5] = 'o'; } else { if( (($squaresarray[6] == "") && ($squaresarray[0] ==$x) && ($squaresarray[3] == $x)) || (($squaresarray[6] == "") && ($squaresarray[2] ==$x) && ($squaresarray[4] == $x)) || (($squaresarray[6] == "") && ($squaresarray[7] ==$x) && ($squaresarray[8] == $x)) ) { $squaresarray[6] = 'o'; } else { if( (($squaresarray[7] == "") && ($squaresarray[1] ==$x) && ($squaresarray[4] == $x)) || (($squaresarray[7] == "") && ($squaresarray[6] == $x) && ($squaresarray[8] == $x)) ) { $squaresarray[7] = 'o'; } else { if( (($squaresarray[8] == "") && ($squaresarray[6] ==$x) && ($squaresarray[7] == $x)) || (($squaresarray[8] == "") && ($squaresarray[4] ==$x) && ($squaresarray[0] == $x)) || (($squaresarray[8] == "") && ($squaresarray[2] ==$x) && ($squaresarray[5] == $x)) ) { $squaresarray[8] ='o'; } } } } } } } } } } ?> </body> </html> THANK YOU (edited by kenrbnsn to add the tags) Link to comment https://forums.phpfreaks.com/topic/100416-help-asap-please-with-this-godforsaken-code-tic-tac-toe-unbeatble/ Share on other sites More sharing options...
tcurtis Posted April 10, 2008 Share Posted April 10, 2008 One Problem here... Tic Tac Toe by its nature can be tied everytime no matter what if every move gets seen. The checks you have coded in mean that the computer is always going to see the blocks. However the human mind does not run if-else checks quite so flawlessly. Without some random chance of failure in the check the computer is gonna beat(or at least tie) you every time. No matter what. Link to comment https://forums.phpfreaks.com/topic/100416-help-asap-please-with-this-godforsaken-code-tic-tac-toe-unbeatble/#findComment-513552 Share on other sites More sharing options...
doni49 Posted April 10, 2008 Share Posted April 10, 2008 There should be something telling you which line the error is on. That would help narrow it down. That's a LOT of code to try and digest. Link to comment https://forums.phpfreaks.com/topic/100416-help-asap-please-with-this-godforsaken-code-tic-tac-toe-unbeatble/#findComment-513556 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.