mauroct Posted November 11, 2008 Share Posted November 11, 2008 Hello everyone, I need some help to finish my code. I'm working on a tictactoe program, 4x4. I have to make it so that the player will play against the pc, and it should also refresh the web page. this is what i have so far <?PHP // If the game has already started, continue // Else start a new game - X goes first session_start(); $move = $HTTP_GET_VARS['move']; // Reset if commanded if($HTTP_GET_VARS['do'] == "reset") { $move = ""; for($a = "0"; $a <= "3"; $a++) { for($b = "0"; $b <= "3"; $b++) { $value = "boardArray"; $value .= $a; $value .= $b; $_SESSION[$value] = ""; } } $_SESSION['whosTurn'] = ""; } // Check for continuing or new game if($move == "") { // Start Game // Initialize variables $whosTurn = "X"; $gameRunning = "1"; $boardArray[0][0] = ""; $boardArray[0][1] = ""; $boardArray[0][2] = ""; $boardArray[1][0] = ""; $boardArray[1][1] = ""; $boardArray[1][2] = ""; $boardArray[2][0] = ""; $boardArray[2][1] = ""; $boardArray[2][2] = ""; // Set session variables $_SESSION['boardArray'] = $boardArray; $_SESSION['whosTurn'] = "X"; } else { // Get board values for($a = "0"; $a <= "3"; $a++) { for($b = "0"; $b <= "3"; $b++) { $value = "boardArray"; $value .= $a; $value .= $b; $boardArray[$a][$b] = $_SESSION[$value]; } } // Get game values and move coords $whosTurn = $_SESSION['whosTurn']; $coOrds = explode("-",$HTTP_GET_VARS['move']); $x = $coOrds[0]; $y = $coOrds[1]; // Perform move, but check for refresh (F5) first if($boardArray[$x][$y] == "") {$boardArray[$x][$y] = $whosTurn;} else {$refreshed = "1";} // Stalemate function function doStalemate() { global $staleMate; $staleMate = "1"; } // Game win function $gameWon = "0"; function doWin($whoWon,$wonWith1,$wonWith2,$wonWith3, $wonWith4) { // We want to use the global gameWon variable global $gameWon; // Explode the wonWith (which combination won) vars $wonWith1 = explode("-",$wonWith1); $wonWith2 = explode("-",$wonWith2); $wonWith3 = explode("-",$wonWith3); $wonWith4 = explode("-",$wonWith4); // Transfer the exploded vars to global vars $GLOBALS['whoWon'] = $whoWon; $GLOBALS['wonWith1[0]'] = $wonWith1[0]; $GLOBALS['wonWith1[1]'] = $wonWith1[1]; $GLOBALS['wonWith2[0]'] = $wonWith2[0]; $GLOBALS['wonWith2[1]'] = $wonWith2[1]; $GLOBALS['wonWith3[0]'] = $wonWith3[0]; $GLOBALS['wonWith3[1]'] = $wonWith3[1]; $GLOBALS['wonWith4[0]'] = $wonWith4[0]; $GLOBALS['wonWith4[1]'] = $wonWith4[1]; // The game has been won $gameWon = "1"; } // Check for Win or stalemate // X First if($boardArray[0][0] == "X" && $boardArray[0][1] == "X" && $boardArray[0][2] == "X" && $boardArray[0][3]) {doWin("X","0-0","0-1","0-2","0-3");} if($boardArray[0][0] == "X" && $boardArray[1][1] == "X" && $boardArray[2][2] == "X" && $boardArray[3][3]) {doWin("X","0-0","1-1","2-2","3-3");} if($boardArray[0][0] == "X" && $boardArray[1][0] == "X" && $boardArray[2][0] == "X" && $boardArray[3][0]) {doWin("X","0-0","1-0","2-0","3-0");} if($boardArray[1][0] == "X" && $boardArray[1][1] == "X" && $boardArray[1][2] == "X" && $boardArray[1][3]) {doWin("X","1-0","1-1","1-2","1-3");} if($boardArray[2][0] == "X" && $boardArray[2][1] == "X" && $boardArray[2][2] == "X" && $boardArray[2][3]) {doWin("X","2-0","2-1","2-2","2-3");} if($boardArray[3][0] == "X" && $boardArray[3][1] == "X" && $boardArray[3][2] == "X" && $boardArray[3][3]) {doWin("X","3-0","3-1","3-2","3-3");} if($boardArray[0][1] == "X" && $boardArray[1][1] == "X" && $boardArray[2][1] == "X" && $boardArray[3][1]) {doWin("X","0-1","1-1","2-1","3-1");} if($boardArray[0][2] == "X" && $boardArray[1][2] == "X" && $boardArray[2][2] == "X" && $boardArray[3][2]) {doWin("X","0-2","1-2","2-2","3-2");} if($boardArray[0][3] == "X" && $boardArray[1][3] == "X" && $boardArray[2][3] == "X" && $boardArray[3][3]) {doWin("X","0-3","1-3","2-3","3-3");} if($boardArray[3][0] == "X" && $boardArray[2][1] == "X" && $boardArray[1][2] == "X" && $boardArray[0][3]) {doWin("X","3-0","2-1","1-2","0-3");} // Now O if($boardArray[0][0] == "O" && $boardArray[0][1] == "O" && $boardArray[0][2] == "O" && $boardArray[0][3]) {doWin("X","0-0","0-1","0-2","0-3");} if($boardArray[0][0] == "O" && $boardArray[1][1] == "O" && $boardArray[2][2] == "O" && $boardArray[3][3]) {doWin("X","0-0","1-1","2-2","3-3");} if($boardArray[0][0] == "O" && $boardArray[1][0] == "O" && $boardArray[2][0] == "O" && $boardArray[3][0]) {doWin("X","0-0","1-0","2-0","3-0");} if($boardArray[1][0] == "O" && $boardArray[1][1] == "O" && $boardArray[1][2] == "O" && $boardArray[1][3]) {doWin("X","1-0","1-1","1-2","1-3");} if($boardArray[2][0] == "O" && $boardArray[2][1] == "O" && $boardArray[2][2] == "O" && $boardArray[2][3]) {doWin("X","2-0","2-1","2-2","2-3");} if($boardArray[3][0] == "O" && $boardArray[3][1] == "O" && $boardArray[3][2] == "O" && $boardArray[3][3]) {doWin("X","3-0","3-1","3-2","3-3");} if($boardArray[0][1] == "O" && $boardArray[1][1] == "O" && $boardArray[2][1] == "O" && $boardArray[3][1]) {doWin("X","0-1","1-1","2-1","3-1");} if($boardArray[0][2] == "O" && $boardArray[1][2] == "O" && $boardArray[2][2] == "O" && $boardArray[3][2]) {doWin("X","0-2","1-2","2-2","3-2");} if($boardArray[0][3] == "O" && $boardArray[1][3] == "O" && $boardArray[2][3] == "O" && $boardArray[3][3]) {doWin("X","0-3","1-3","2-3","3-3");} if($boardArray[3][0] == "O" && $boardArray[2][1] == "O" && $boardArray[1][2] == "O" && $boardArray[0][3]) {doWin("X","3-0","2-1","1-2","0-3");} // No win. How about stalemate? if($gameWon != "1"){ if($boardArray[0][0] != "" && $boardArray[0][1] != "" && $boardArray[0][2] != "" && $boardArray[0][3] != "" && $boardArray[1][0] != "" && $boardArray[1][1] != "" && $boardArray[1][2] != "" && $boardArray[1][3] != "" &&$boardArray[2][0] != "" && $boardArray[2][1] != "" && $boardArray[2][2] != "" && $boardArray[2][3] != "" && $boardArray[3][0] != "" && $boardArray[3][1] != "" && $boardArray[3][2] != "" && $boardArray[3][3] != "") { // Stalemate doStalemate(); } } // Update turn if($refreshed != "1") {if($whosTurn == "X") {$whosTurn = "O";} else {$whosTurn = "X";}} $_SESSION['whosTurn'] = $whosTurn; // Save board values for($a = "0"; $a <= "3"; $a++) { for($b = "0"; $b <= "3"; $b++) { $value = "boardArray"; $value .= $a; $value .= $b; $_SESSION[$value] = $boardArray[$a][$b]; } } } ?> <html> <head> <title>Tic-Tac-Toe</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <style type="text/css"> a {color: gray; text-decoration: none;} a:hover {color: black; text-decoration: none;} .gameSquare {width: 100px; height: 100px; font-family: Tahoma; font-size: 36pt; color: black;} .gameSquareWon {width: 100px; height: 100px; font-family: Tahoma; font-size: 36pt; color: red;} </style> </head> <body> <div align="center"> <font face="Tahoma" size="5">Tic-Tac-Toe</font><br> <font face="Tahoma" size="3"><?PHP if($staleMate == "1") {echo("Stalemate.");} else {if($gameWon != "1") {echo $whosTurn; ?>'s Turn<?PHP } else {echo $GLOBALS['whoWon']; echo(" WON!");}} ?></font> <table width="300" height="300" border="1" cellpadding="5" cellspacing="0" bordercolor="#000000"> <?PHP $draw_x = "-1"; $draw_y = "-1"; for($row = 0; $row < 4; $row++) { echo("<tr align=\"center\" valign=\"middle\">"); $draw_x++; for($col = 0; $col < 4; $col++) { $draw_y++; echo("<td class=\"gameSquare"); if($gameWon == "1") { if($draw_x == $GLOBALS['wonWith1[0]'] && $draw_y == $GLOBALS['wonWith1[1]']) {$winningSpot = "1";} else if($draw_x == $GLOBALS['wonWith2[0]'] && $draw_y == $GLOBALS['wonWith2[1]']) {$winningSpot = "1";} else if($draw_x == $GLOBALS['wonWith3[0]'] && $draw_y == $GLOBALS['wonWith3[1]']) {$winningSpot = "1";} else if($draw_x == $GLOBALS['wonWith4[0]'] && $draw_y == $GLOBALS['wonWith4[1]']) {$winningSpot = "1";} if($winningSpot == "1") {echo("Won");$winningSpot = "0";} } echo("\">"); if($boardArray[$draw_x][$draw_y] != "") { echo $boardArray[$draw_x][$draw_y]; } else { if($gameWon != "1") { echo("<A HREF=\"tictactoe.php?do=move&move=".$draw_x."-".$draw_y."&which=".$whosTurn."\">?</A>"); } else { echo("-"); } } echo("</td>"); } $draw_y = "-1"; echo("</tr>"); } ?> </table> <font face="tahoma" size="2"><a href="tictactoe.php?do=reset">Reset Game / Play Again</a></font> </div> </body> </html> Thanks! Link to comment https://forums.phpfreaks.com/topic/132280-random-selection/ Share on other sites More sharing options...
genericnumber1 Posted November 11, 2008 Share Posted November 11, 2008 I have no idea what you want. http://www.phpfreaks.com/page/forum-rules Consider reading rules 4-18 and revising your question in this thread. Link to comment https://forums.phpfreaks.com/topic/132280-random-selection/#findComment-687807 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.