webdevdea Posted October 26, 2013 Share Posted October 26, 2013 HERE Is the link http://dandewebwonders.com/game.php you can play around .. it will say 'o' wins when they obviously dont and when 'x' wins it does not acknowledge it.. Trying to put this on my site and cant get it to work correctly.. please and thank you .. <?php $winner = 'n'; $box = array('','','','','','','','',''); if (isset($_POST["submitbtn"])) { $box[0] = $_POST['box0']; $box[1] = $_POST['box1']; $box[2] = $_POST['box2']; $box[3] = $_POST['box3']; $box[4] = $_POST['box4']; $box[5] = $_POST['box5']; $box[6] = $_POST['box6']; $box[7] = $_POST['box7']; $box[8] = $_POST['box8']; //print_r($box); if (($box[0] =='x' && $box[1]=='x' && $box[2] =='x') || ($box[3] =='x' && $box[4]=='x' && $box[5] =='x') || ($box[6] =='x' && $box[7]=='x' && $box[8] =='x') || ($box[0] =='x' && $box[3]=='x' && $box[6] =='x') || ($box[1] =='x' && $box[5]=='x' && $box[7] =='x') ||($box[2] =='x' && $box[6]=='x' && $box[8] =='x') || ($box[0] =='x' && $box[4]=='x' && $box[8] =='x') ||($box[2] =='x' && $box[4]=='x' && $box[6] =='x')){ $winner = 'x'; print("X wins"); } $blank = 0; for ($i=0; $i<=8; $i++){ if ($box[$i] == '') { $blank = 1; } } if ($blank == 1 && $winner == 'n') { $i = rand() % 8; while ($box[$i] != '') { $i = rand() % 8; }//end while loop $box[$i] = 'o'; if (($box[0] =='o' && $box[1]=='o' && $box[2] =='o') || ($box[3] =='o' && $box[4]=='o' && $box[5] =='o') || ($box[6] =='o' && $box[7]=='o' && $box[8] =='o') || ($box[0] =='o' && $box[3]=='o' && $box[6] =='o') || ($box[1] =='o' && $box[5]=='o' && $box[7] =='o') ||($box[2] =='o' && $box[6]=='o' && $box[8] =='o') || ($box[0] =='o' && $box[4]=='o' && $box[8] =='o') ||($box[2] =='o' && $box[4]=='o' && $box[6] =='o')) { $winner = 'o'; print("O wins"); } } else if ($winner =='n'){ $winner = 't'; print("Tied Game"); } } ?> <html> <head> <title>game</title> <style type="text/css"> #box{ background-color :#99FFCC; border: 1px solid #008000; width: 100px; height: 100px; font-size: 66px; text-align: center; } #go { width:200px; font-family: 'Comic Sans MS'; font-size: 40px; } </style> </head> <body bgcolor="pink"> <body><CENTER> <H1>TIC-TAC-TOE</H1> <P> PLACE AN X IN A SQUARE, THEN CLICK FIRE FOR THE COMPUTER TO TAKE A TURN</P> <form name="tictactoe" method="post" action="game.php"> <?php for ($i=0; $i<=8; $i++) { printf('<input type="text" name="box%s" value ="%s" id = "box">', $i, $box[$i]); if ($i == 2 || $i == 5 || $i == 8 ) { print('<br>'); } } // end for loop if ($winner == 'n') { print('<input type="submit" name="submitbtn" value="fire" id="go">'); } else { print('<input type="button" name="newgamebtn" value= "New Game" onclick ="window.location.href=\'game.php\'">'); } ?> </form></CENTER> </body> </html> Link to comment https://forums.phpfreaks.com/topic/283294-working-on-tic-tac-toe-game/ Share on other sites More sharing options...
ignace Posted October 26, 2013 Share Posted October 26, 2013 if (isset($_POST['submitbtn'])) { $hasBlank = false; foreach (range(0, as $i) { $val = array_key_exists('box' . $i, $_POST) ? $_POST['box' . $i] : ''; if (empty($val) && $hasBlank === false) $hasBlank = true; $box[] = $val; } function check($box, $x) { // [ 0, 1, 2 ] // [ 3, 4, 5 ] // [ 6, 7, 8 ] // horizontal return ($box[0] == $x && $box[1] == $x && $box[2] == $x) || ($box[3] == $x && $box[4] == $x && $box[5] == $x) || ($box[6] == $x && $box[7] == $x && $box[8] == $x) // vertical || ($box[0] == $x && $box[3] == $x && $box[6] == $x) || ($box[1] == $x && $box[4] == $x && $box[7] == $x) || ($box[2] == $x && $box[5] == $x && $box[8] == $x) // diagonal || ($box[0] == $x && $box[4] == $x && $box[8] == $x) || ($box[2] == $x && $box[4] == $x && $box[6] == $x); } function doComputerTurn(&$box, $x) { $empties = array_keys($box, ''); if (!$empties) return; $key = array_rand($empties); $box[$empties[$key]] = $x; } if ($hasBlank) { echo 'keep playing!'; } elseif (check($box, 'o') && check($box, 'x')) { echo 'draw!'; } elseif (check($box, 'x')) { echo 'x wins!'; } elseif (check($box, 'o')) { echo 'o wins!'; } } Link to comment https://forums.phpfreaks.com/topic/283294-working-on-tic-tac-toe-game/#findComment-1455500 Share on other sites More sharing options...
webdevdea Posted October 28, 2013 Author Share Posted October 28, 2013 That was not a fix… Anyone have any advice? Link to comment https://forums.phpfreaks.com/topic/283294-working-on-tic-tac-toe-game/#findComment-1455802 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.