chimdog Posted April 10, 2008 Share Posted April 10, 2008 ok first off I'm fairly new to PHP so I might have just made some common mistakes. The problem I'm having is related to a battleship program I am making. I can't seam to get it to place the battleship for some reason and I don't see why it won't or what I am doing wrong. also I seam to get this error for some reason on the html page so maybe that has something to do with it. "Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at W:\www\battleshipDevel.php:3) in W:\www\battleshipDevel.php on line 4" here is the code for my program. any problems or help would be a great help to me. <? session_start(); $dimension = 30; $sizeOfBattleShip = 5; print <<<HERE <form> Row: <input type = "text" name = "row" size = "1"> Col: <input type = "text" name = "col" size = "1"> <br><br> <input type="submit" value="Shoot" action = "battleshipDevel.php"> </form> <br> HERE; //checks if this is the first time here and places the battleships //it also puts the player to 1 if the boards havent been created if($_SESSION["player1"] == NULL){ $_SESSION["player"]=1; placeBattleShip(); } if($_SESSION["player2"] == NULL){ placeBattleShip2(); } // replace $row,$col with $_REQUEST["row/col"] after form is developed attackBattleShip($_REQUEST["row"],$_REQUEST["col"]); function attackBattleShip($row,$col){ global $sizeOfBattleShip; print "<BR>"; print "Player: "; print $_SESSION["player"]; print "<BR>"; // right + down //player 1 attack if($_SESSION["player"]==1){ if($_SESSION["player1"]["areasAttacked"][$row . "-" . $col] != 1){ if($col >= $_SESSION["player1"]["startCol"] && $col <= $_SESSION["player1"]["endCol"] && $row >= $_SESSION["player1"]["startRow"] && $row <= $_SESSION["player1"]["endRow"]){ print "HIT!"; $_SESSION["player1"]["hitsAgainst"]++; // left + up }else if($col <= $_SESSION["player1"]["startCol"] && $col >= $_SESSION["player1"]["endCol"] && $row <= $_SESSION["player1"]["startRow"] && $row >= $_SESSION["player1"]["endRow"]){ print "HIT!"; $_SESSION["player1"]["hitsAgainst"]++; }else{ print "MISS!"; } if($_SESSION["player1"]["hitsAgainst"] >= $sizeOfBattleShip){ print "BATTLESHIP SUNK!<br>" . $sizeOfBattleShip; } }else if($_SESSION["player1"]["areasAttacked"][$row . "-" . $col] == 1){ print "MISS!"; } // register hit to player1 $_SESSION["player1"]["areasAttacked"][$row . "-" . $col] = 1; } //player 2 attack if($_SESSION["player"]==2){ if($_SESSION["player2"]["areasAttacked"][$row . "-" . $col] != 1){ if($col >= $_SESSION["player2"]["startCol"] && $col <= $_SESSION["player2"]["endCol"] && $row >= $_SESSION["player2"]["startRow"] && $row <= $_SESSION["player2"]["endRow"]){ print "HIT!"; $_SESSION["player2"]["hitsAgainst"]++; // left + up }else if($col <= $_SESSION["player2"]["startCol"] && $col >= $_SESSION["player2"]["endCol"] && $row <= $_SESSION["player2"]["startRow"] && $row >= $_SESSION["player2"]["endRow"]){ print "HIT!"; $_SESSION["player2"]["hitsAgainst"]++; }else{ print "MISS!"; } if($_SESSION["player2"]["hitsAgainst"] >= $sizeOfBattleShip){ print "BATTLESHIP SUNK!<br>" . $sizeOfBattleShip; } }else{ print "MISS!"; } // register hit to player1 $_SESSION["player2"]["areasAttacked"][$row . "-" . $col] = 1; } } //changes the current player if($_SESSION["player"]==2){ print makeBoard(); $_SESSION["player"]=1; }else{ $_SESSION["player"]=2; print makeBoard2(); } function placeBattleShip(){ global $dimension,$sizeOfBattleShip; $direction = rand(0,3); print "<br> Direction $direction <br>"; // up if($direction == 1){ $startRow = rand($sizeOfBattleShip, $dimension - 1); $startCol = rand(0, $dimension - 1); $endRow = $startRow - $sizeOfBattleShip; $endCol = $startCol; // down }else if($direction == 2){ $startRow = rand(0, $dimension - 1 - $sizeOfBattleShip); $startCol = rand(0, $dimension - 1); $endRow = $startRow + $sizeOfBattleShip; $endCol = $startCol; // left }else if($direction == 3){ $startRow = rand(0, $dimension - 1); $startCol = rand($sizeOfBattleShip, $dimension - 1); $endRow = $startRow; $endCol = $startCol - $sizeOfBattleShip; // right }else{ $startRow = rand(0, $dimension - 1); $startCol = rand(0, $dimension - 1 - $sizeOfBattleShip); $endRow = $startRow; $endCol = $startCol + $sizeOfBattleShip; } $_SESSION["player1"]["startRow"] = $startRow; $_SESSION["player1"]["startCol"] = $startCol; $_SESSION["player1"]["endRow"] = $endRow; $_SESSION["player1"]["endCol"] = $endCol; } //player 2 function placeBattleShip2(){ $direction = rand(0,3); // up if($direction == 1){ $startRow = rand($sizeOfBattleShip, $dimension - 1); $startCol = rand(0, $dimension - 1); $endRow = $startRow - $sizeOfBattleShip; $endCol = $startCol; // down }else if($direction == 2){ $startRow = rand(0, $dimension - 1 - $sizeOfBattleShip); $startCol = rand(0, $dimension - 1); $endRow = $startRow + $sizeOfBattleShip; $endCol = $startCol; // left }else if($direction == 3){ $startRow = rand(0, $dimension - 1); $startCol = rand($sizeOfBattleShip, $dimension - 1); $endRow = $startRow; $endCol = $startCol - $sizeOfBattleShip; // right }else{ $startRow = rand(0, $dimension - 1); $startCol = rand(0, $dimension - 1 - $sizeOfBattleShip); $endRow = $startRow; $endCol = $startCol + $sizeOfBattleShip; } $_SESSION["player2"]["startRow"] = $startRow; $_SESSION["player2"]["startCol"] = $startCol; $_SESSION["player2"]["endRow"] = $endRow; $_SESSION["player2"]["endCol"] = $endCol; } function makeBoard(){ global $dimension; $boardOutput =""; print "Start Row" . $_SESSION["player1"]["startRow"] . "<br>"; print "End Row" . $_SESSION["player1"]["endRow"] . "<br>"; print "Start Col" . $_SESSION["player1"]["startCol"] . "<br>"; print "End Col" . $_SESSION["player1"]["endCol"] . "<br>"; $boardOutput .= "<table>\n"; $boardOutput .= "<tr>\n"; for($row = 0; $row < $dimension; $row++){ for($col = 0; $col < $dimension; $col++){ if($_SESSION["player1"]["areasAttacked"][$row . "-" . $col] && $col >= $_SESSION["player1"]["startCol"] && $col <= $_SESSION["player1"]["endCol"] && $row >= $_SESSION["player1"]["startRow"] && $row <= $_SESSION["player1"]["endRow"]){ $boardOutput .="<td> H </td>"; }else if($_SESSION["player1"]["areasAttacked"][$row . "-" . $col] && $col <= $_SESSION["player1"]["startCol"] && $col >= $_SESSION["player1"]["endCol"] && $row <= $_SESSION["player1"]["startRow"] && $row >= $_SESSION["player1"]["endRow"]){ $boardOutput .="<td> H </td>"; }else if($_SESSION["player1"]["areasAttacked"][$row . "-" . $col]){ $boardOutput .="<td> M </td>"; }else{ $boardOutput .="<td> W </td>"; } } $boardOutput .= "</tr>"; } $boardOutput .= "</table>"; return $boardOutput; } function makeBoard2(){ global $dimension; $boardOutput =""; print "Start Row" . $_SESSION["player2"]["startRow"] . "<br>"; print "End Row" . $_SESSION["player2"]["endRow"] . "<br>"; print "Start Col" . $_SESSION["player2"]["startCol"] . "<br>"; print "End Col" . $_SESSION["player2"]["endCol"] . "<br>"; $boardOutput .= "<table>\n"; $boardOutput .= "<tr>\n"; for($row = 0; $row < $dimension; $row++){ for($col = 0; $col < $dimension; $col++){ if($_SESSION["player2"]["areasAttacked"][$row . "-" . $col] && $col >= $_SESSION["player2"]["startCol"] && $col <= $_SESSION["player2"]["endCol"] && $row >= $_SESSION["player2"]["startRow"] && $row <= $_SESSION["player2"]["endRow"]){ $boardOutput .="<td> H </td>"; }else if($_SESSION["player2"]["areasAttacked"][$row . "-" . $col] && $col <= $_SESSION["player2"]["startCol"] && $col >= $_SESSION["player2"]["endCol"] && $row <= $_SESSION["player2"]["startRow"] && $row >= $_SESSION["player2"]["endRow"]){ $boardOutput .="<td> H </td>"; }else if($_SESSION["player2"]["areasAttacked"][$row . "-" . $col]){ $boardOutput .="<td> M </td>"; }else{ $boardOutput .="<td> W </td>"; } } $boardOutput .= "</tr>"; } $boardOutput .= "</table>"; return $boardOutput; } ?> Quote Link to comment Share on other sites More sharing options...
btherl Posted April 10, 2008 Share Posted April 10, 2008 To fix the session_start() error, remove any blank lines before the "<?" at the top of your script. I'm taking a look at the other problem now. Quote Link to comment Share on other sites More sharing options...
btherl Posted April 10, 2008 Share Posted April 10, 2008 Ok, for your second problem, start adding more debugging statements (prints) to the battleship placement code. If you get totally stuck, post your current code along with the EXACT output, and we can most likely help you out! Quote Link to comment Share on other sites More sharing options...
chimdog Posted April 10, 2008 Author Share Posted April 10, 2008 thanks ill look into it. Quote Link to comment Share on other sites More sharing options...
chimdog Posted April 10, 2008 Author Share Posted April 10, 2008 well just removing those lines seamed to fix most of my problems I think. It seams to look like the problem is the placement of the second players battleship. The first players works fine and is correctly placed and saved. when it switches to player 1's turn to shoot it says players 2 battleship is placed at a start and end rows of 0 and start and end columns of 0 which means its not being placed. This doesn't seam to make sense to me at least since all I did was copy and paste the same code that is used to place the one that works and just changed the session variables from player1 to player2. I even attempted to place the part where it makes the board in the same if then structure that creates player 1's board. here is my current code <? session_start(); $dimension = 30; $sizeOfBattleShip = 5; print <<<HERE <form> Row: <input type = "text" name = "row" size = "1"> Col: <input type = "text" name = "col" size = "1"> <br><br> <input type="submit" value="Shoot" > </form> <br> HERE; //checks if this is the first time here and places the battleships //it also puts the player to 1 if the boards havent been created if($_SESSION["player1"] == NULL){ $_SESSION["player"]=1; placeBattleShip(); placeBattleShip2(); } // replace $row,$col with $_REQUEST["row/col"] after form is developed attackBattleShip($_REQUEST["row"],$_REQUEST["col"]); function attackBattleShip($row,$col){ global $sizeOfBattleShip; print "<BR>"; print "Player: "; print $_SESSION["player"]; print "<BR>"; // right + down //player 1 attack if($_SESSION["player"]==1){ if($_SESSION["player1"]["areasAttacked"][$row . "-" . $col] != 1){ if($col >= $_SESSION["player1"]["startCol"] && $col <= $_SESSION["player1"]["endCol"] && $row >= $_SESSION["player1"]["startRow"] && $row <= $_SESSION["player1"]["endRow"]){ print "HIT!"; $_SESSION["player1"]["hitsAgainst"]++; // left + up }else if($col <= $_SESSION["player1"]["startCol"] && $col >= $_SESSION["player1"]["endCol"] && $row <= $_SESSION["player1"]["startRow"] && $row >= $_SESSION["player1"]["endRow"]){ print "HIT!"; $_SESSION["player1"]["hitsAgainst"]++; }else{ print "MISS!"; } if($_SESSION["player1"]["hitsAgainst"] >= $sizeOfBattleShip){ print "BATTLESHIP SUNK!<br>" . $sizeOfBattleShip; } }else if($_SESSION["player1"]["areasAttacked"][$row . "-" . $col] == 1){ print "MISS!"; } // register hit to player1 $_SESSION["player1"]["areasAttacked"][$row . "-" . $col] = 1; } //player 2 attack if($_SESSION["player"]==2){ if($_SESSION["player2"]["areasAttacked"][$row . "-" . $col] != 1){ if($col >= $_SESSION["player2"]["startCol"] && $col <= $_SESSION["player2"]["endCol"] && $row >= $_SESSION["player2"]["startRow"] && $row <= $_SESSION["player2"]["endRow"]){ print "HIT!"; $_SESSION["player2"]["hitsAgainst"]++; // left + up }else if($col <= $_SESSION["player2"]["startCol"] && $col >= $_SESSION["player2"]["endCol"] && $row <= $_SESSION["player2"]["startRow"] && $row >= $_SESSION["player2"]["endRow"]){ print "HIT!"; $_SESSION["player2"]["hitsAgainst"]++; }else{ print "MISS!"; } if($_SESSION["player2"]["hitsAgainst"] >= $sizeOfBattleShip){ print "BATTLESHIP SUNK!<br>" . $sizeOfBattleShip; } }else{ print "MISS!"; } // register hit to player2 $_SESSION["player2"]["areasAttacked"][$row . "-" . $col] = 1; } } //changes the current player if($_SESSION["player"]==2){ print makeBoard(); $_SESSION["player"]=1; }else if($_SESSION["player"]==1){ $_SESSION["player"]=2; print makeBoard2(); }else{ print "FAIL"; } function placeBattleShip(){ global $dimension,$sizeOfBattleShip; $direction = rand(0,3); print "<br> Direction $direction <br>"; // up if($direction == 1){ $startRow = rand($sizeOfBattleShip, $dimension - 1); $startCol = rand(0, $dimension - 1); $endRow = $startRow - $sizeOfBattleShip; $endCol = $startCol; // down }else if($direction == 2){ $startRow = rand(0, $dimension - 1 - $sizeOfBattleShip); $startCol = rand(0, $dimension - 1); $endRow = $startRow + $sizeOfBattleShip; $endCol = $startCol; // left }else if($direction == 3){ $startRow = rand(0, $dimension - 1); $startCol = rand($sizeOfBattleShip, $dimension - 1); $endRow = $startRow; $endCol = $startCol - $sizeOfBattleShip; // right }else{ $startRow = rand(0, $dimension - 1); $startCol = rand(0, $dimension - 1 - $sizeOfBattleShip); $endRow = $startRow; $endCol = $startCol + $sizeOfBattleShip; } $_SESSION["player1"]["startRow"] = $startRow; $_SESSION["player1"]["startCol"] = $startCol; $_SESSION["player1"]["endRow"] = $endRow; $_SESSION["player1"]["endCol"] = $endCol; } //player 2 function placeBattleShip2(){ global $dimension,$sizeOfBattleShip; $direction = rand(0,3); print "<br> Direction $direction <br>"; // up if($direction == 1){ $startRow = rand($sizeOfBattleShip, $dimension - 1); $startCol = rand(0, $dimension - 1); $endRow = $startRow - $sizeOfBattleShip; $endCol = $startCol; // down }else if($direction == 2){ $startRow = rand(0, $dimension - 1 - $sizeOfBattleShip); $startCol = rand(0, $dimension - 1); $endRow = $startRow + $sizeOfBattleShip; $endCol = $startCol; // left }else if($direction == 3){ $startRow = rand(0, $dimension - 1); $startCol = rand($sizeOfBattleShip, $dimension - 1); $endRow = $startRow; $endCol = $startCol - $sizeOfBattleShip; // right }else{ $startRow = rand(0, $dimension - 1); $startCol = rand(0, $dimension - 1 - $sizeOfBattleShip); $endRow = $startRow; $endCol = $startCol + $sizeOfBattleShip; } $_SESSION["player2"]["startRow"] = $startRow; $_SESSION["player2"]["startCol"] = $startCol; $_SESSION["player2"]["endRow"] = $endRow; $_SESSION["player2"]["endCol"] = $endCol; } function makeBoard(){ global $dimension; $boardOutput =""; print "Start Row" . $_SESSION["player1"]["startRow"] . "<br>"; print "End Row" . $_SESSION["player1"]["endRow"] . "<br>"; print "Start Col" . $_SESSION["player1"]["startCol"] . "<br>"; print "End Col" . $_SESSION["player1"]["endCol"] . "<br>"; $boardOutput .= "<table>\n"; $boardOutput .= "<tr>\n"; for($row = 0; $row < $dimension; $row++){ for($col = 0; $col < $dimension; $col++){ if($_SESSION["player1"]["areasAttacked"][$row . "-" . $col] && $col >= $_SESSION["player1"]["startCol"] && $col <= $_SESSION["player1"]["endCol"] && $row >= $_SESSION["player1"]["startRow"] && $row <= $_SESSION["player1"]["endRow"]){ $boardOutput .="<td> H </td>"; }else if($_SESSION["player1"]["areasAttacked"][$row . "-" . $col] && $col <= $_SESSION["player1"]["startCol"] && $col >= $_SESSION["player1"]["endCol"] && $row <= $_SESSION["player1"]["startRow"] && $row >= $_SESSION["player1"]["endRow"]){ $boardOutput .="<td> H </td>"; }else if($_SESSION["player1"]["areasAttacked"][$row . "-" . $col]){ $boardOutput .="<td> M </td>"; }else{ $boardOutput .="<td> W </td>"; } } $boardOutput .= "</tr>"; } $boardOutput .= "</table>"; return $boardOutput; } function makeBoard2(){ global $dimension; $boardOutput =""; print "Start Row" . $_SESSION["player2"]["startRow"] . "<br>"; print "End Row" . $_SESSION["player2"]["endRow"] . "<br>"; print "Start Col" . $_SESSION["player2"]["startCol"] . "<br>"; print "End Col" . $_SESSION["player2"]["endCol"] . "<br>"; $boardOutput .= "<table>\n"; $boardOutput .= "<tr>\n"; for($row = 0; $row < $dimension; $row++){ for($col = 0; $col < $dimension; $col++){ if($_SESSION["player2"]["areasAttacked"][$row . "-" . $col] && $col >= $_SESSION["player2"]["startCol"] && $col <= $_SESSION["player2"]["endCol"] && $row >= $_SESSION["player2"]["startRow"] && $row <= $_SESSION["player2"]["endRow"]){ $boardOutput .="<td> H </td>"; }else if($_SESSION["player2"]["areasAttacked"][$row . "-" . $col] && $col <= $_SESSION["player2"]["startCol"] && $col >= $_SESSION["player2"]["endCol"] && $row <= $_SESSION["player2"]["startRow"] && $row >= $_SESSION["player2"]["endRow"]){ $boardOutput .="<td> H </td>"; }else if($_SESSION["player2"]["areasAttacked"][$row . "-" . $col]){ $boardOutput .="<td> M </td>"; }else{ $boardOutput .="<td> W </td>"; } } $boardOutput .= "</tr>"; } $boardOutput .= "</table>"; return $boardOutput; } ?> Quote Link to comment Share on other sites More sharing options...
chimdog Posted April 10, 2008 Author Share Posted April 10, 2008 i forgot to delete the cookies everything works now thanks a lot for your help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.