hackypro Posted October 5, 2008 Share Posted October 5, 2008 well its getting there, i need some help with the session variables however. my unedited (LONG) code is posted below <?PHP session_cache_limiter('private, must-revalidate'); session_start(); ?> <html> <head> <style> table { border: 1px solid black; } table tr,td { border: 1px solid black; } </style> </head> <center> <?PHP //SESSION VARIABLES! YAY if (!isset($_SESSION['Progress'])) { $_SESSION['Progress'] = 1; setvar(); overall(); } function overall() { $_SESSION['Minimum']=0; $_SESSION['Maximum']=0; $_SESSION['Average']=0; $_SESSION['Games Played']=0; } function setvar() { $_SESSION['Misses'] = 0; $_SESSION['Allowed Misses'] = 0; $_SESSION['Aircraft Carrier']= 0; $_SESSION['Sunk']=0; $_SESSION['Battleship']= 0; $_SESSION['Destroyer']= 0; $_SESSION['Submarine']= 0; $_SESSION['Patrol Boat'] = 0; $_SESSION['Grid']= array(); $_SESSION['Attacked'] = array(); setgrid(); setships(); } //set the session grid function setgrid() { for ($i=1; $i<11; $i++) { for($j=1; $j<11; $j++) { $array[$i][$j] = 0; } } $_SESSION['Grid'] = $array; $_SESSION['Attacked'] = $array; } function setships() { $ships= array( 'Aircraft Carrier' => 5, 'Battleship' => 4, 'Destroyer' => 3, 'Submarine'=> 3, 'Patrol Boat' => 2 ); $shipcounter=1; $grid = $_SESSION['Grid']; //send the ships through a foreach loop foreach( $ships as $name => $size) { while (TRUE) { //if it is 1, go left-right, if it is 0, go up-down if (mt_rand(0,1) == 1) { $coord= 'x'; } else $coord= 'y'; //independant of $coord, must apply boundaries $max = array( 'x' => 10, 'y' =>10); //subtract from the boundaries, the size of the ship for placement $max[ $coord ] -=$size; //set x and y for placement of the ship's first spot $x = mt_rand( 1, $max['x']); $y = mt_rand( 1, $max['y']); //put ship in a for loop to find other spots for ($i =0; $i < $size; $i++ ) { //set temporary array to hold values of first spot $temp = array ('x' => $x, 'y' => $y ); $temp[ $coord ] += $i; //check if there is already a ship for all spots, if so start loop over if ($grid[$temp['x']][$temp['y']]!=0) { //continue 2 to resume at the while loop continue 2; } } //all spots are available, end the infinite loop break; } //spots available, place ships for ($i = 0; $i != $size; $i++) { $temp= array ('x' => $x, 'y' =>$y); $temp[$coord] += $i; $grid[$temp['x']][$temp['y']]=$shipcounter; } $shipcounter++; } $_SESSION['Grid'] = $grid; } echo "<form action ='".$_SERVER['PHP_SELF']."' method='POST'>"; echo "<input type='submit' name='shoot' value='shoot'><br>"; echo "<input type='submit' name='I give up' value='I give up'>"; echo "<input type='submit' name='show ships' value='show ships'>"; echo "</form>"; if(isset($_POST['shoot'])) { $temp1= $_SESSION['Grid']; $temp2= $_SESSION['Attacked']; $location = $_POST['hitme']; for ($i=1; $i<11; $i++) { for ($j=1; $j<11; $j++) { if((($i * 10)+ $j) == $location) { if ($temp1[$i][$j] == 1) { $temp2[$i][$j]=1; $_SESSION['Aircraft Carrier']++; echo "Hit"; if ($_SESSION['Aircraft Carrier']==5) { echo "SUNK AIRCRAFT CARRIER"; $_SESSION['Sunk']++; } if ($_SESSION['Sunk']==5) { $_SESSION['Games Played']++; setvar(); } } else if ($temp1[$i][$j] == 2) { $temp2[$i][$j]=1; $_SESSION['Battleship']++; echo "Hit"; if ($_SESSION['Battleship']==4) { echo "SUNK BATTLESHIP"; $_SESSION['Sunk']++; } if ($_SESSION['Sunk']==5) { $_SESSION['Games Played']++; setvar(); } } else if ($temp1[$i][$j] == 3) { $temp2[$i][$j]=1; $_SESSION['Destroyer']++; echo "Hit"; if ($_SESSION['Destroyer']==3) { echo "SUNK DESTROYER"; $_SESSION['Sunk']++; } if ($_SESSION['Sunk']==5) { $_SESSION['Games Played']++; setvar(); } } else if ($temp1[$i][$j] == 4) { $temp2[$i][$j]=1; $_SESSION['Submarine']++; echo "Hit"; if ($_SESSION['Submarine']==3) { echo "SUNK SUBMARINE"; $_SESSION['Sunk']++; } if ($_SESSION['Sunk']==5) { $_SESSION['Games Played']++; setvar(); } } else if ($temp1[$i][$j] == 5) { $temp2[$i][$j]=1; $_SESSION['Patrol Boat']++; echo "Hit"; if ($_SESSION['Patrol Boat']==2) { echo "SUNK PATROL BOAT"; $_SESSION['Sunk']++; } if ($_SESSION['Sunk']==5) { $_SESSION['Games Played']++; setvar(); } } else { $temp2[$i][$j]=2; echo "Try Again"; $_SESSION['Misses']++; $_SESSION['Allowed Misses']++; if ($_SESSION['Allowed Misses']==20) { if ($_SESSION['Allowed Misses']>$_SESSION['Maximum']) { $_SESSION['Maximum'] = $_SESSION['Allowed Misses']; } else if ($_SESSION['Allowed Misses']<=$_SESSION['Minimum']) { $_SESSION['Minimum'] = $_SESSION['Allowed Misses']; } $_SESSION['Games Played']++; $_SESSION['Average']+=($_SESSION['Allowed Misses']); setvar(); echo "TOO MANY MISSES. YOU LOST THE GAME"; } } } } } $_SESSION['attacked']= $temp2; } else if (isset($_POST['i give up'])) { if ($_SESSION['Allowed Misses']>$_SESSION['Maximum']) { $_SESSION['Maximum'] = $_SESSION['Allowed Misses']; } else if ($_SESSION['Allowed Misses']<=$_SESSION['Minimum']) { $_SESSION['Minimum'] = $_SESSION['Allowed Misses']; } $_SESSION['Games Played']++; $_SESSION['Average']+=($_SESSION['Allowed Misses']); setvar(); echo "YOU LOST THE GAME"; } if (isset($_POST['show ships'])) { echo "<table bgcolor =FFFFFF>"; echo "<tr><td></td><td> A</td><td> B</td><td> C</td><td> D</td><td> E</td><td> F</td><td> G</td><td> H</td><td> I</td><td> J</td></tr>"; $temporary = $_SESSION['Grid']; for($i=1; $i<11; $i++) { echo "<tr><td> $i </td>"; for ($j=1; $j<11; $j++) { if ($temporary[$i][$j] == 1) { echo "<td bgcolor = 7FFF00>1</td>"; } else if ($temporary[$i][$j] == 2) { echo "<td bgcolor = 8A2BE2>2</td>"; } else if ($temporary[$i][$j] == 3) { echo "<td bgcolor = 00CED1>3</td>"; } else if ($temporary[$i][$j] == 4) { echo "<td bgcolor = 8B0000>4</td>"; } else if ($temporary[$i][$j] == 5) { echo "<td bgcolor = FFFACD>5</td>"; } else echo "<td>0</td>"; } echo "</tr>"; } } echo "<form action ='".$_SERVER['PHP_SELF']."' method='POST'>"; echo "<table>"; echo "<tr><td></td><td> A</td><td> B</td><td> C</td><td> D</td><td> E</td><td> F</td><td> G</td><td> H</td><td> I</td><td> J</td></tr>"; $count=0; $temp3 = $_SESSION['Attacked']; for($i=0; $i<100; $i+=10) { $h=$i/10; echo "<tr><td> $h </td>"; for($j=1; $j<11;$j++) { $count++; if($temp3[$h+1][$j]==0) { echo "<td><input type='radio'name='hitme'value='$count'></td>"; } else if ($temp3[$h+1][$j]==1) { echo "<td bgcolor = 'RED'></td>"; } else echo "<td bgcolor = 'WHITE'></td>"; } echo "</tr>"; } echo "</table>"; echo "</form>"; echo "Misses so far: ".$_SESSION['Misses']; echo "Allowed Misses: ".(20- $_SESSION['Allowed Misses']; echo "Games Played: ".$_SESSION['Games Played']; echo "Maximum Misses: ".$_SESSION['Maximum']; echo "Minimum Misses: ".$_SESSION['Minimum']; echo "Average Misses: ".$_SESSION['Average']; echo "Ships Sunk : ".$_SESSION['Sunk']; ?> </center> </html> phew... anyways the actual code cannot figure out the index for "hitme" on my radio buttons. the show ships button won't work and it doesn't move through my for loops and if statements correctly. it seems like it shouldn't be the hardest thing to fix but i'm a noob and the code is long and convoluted at this point. if anyone has suggestions as to why it doesn't work please feel free to let me know. Link to comment https://forums.phpfreaks.com/topic/127162-remember-that-battleship-game/ Share on other sites More sharing options...
genericnumber1 Posted October 5, 2008 Share Posted October 5, 2008 You'll definitely need to localize the problem more before we can help reading your whole code, figuring out how it works, and then trying to bugfix it would take way too long. Link to comment https://forums.phpfreaks.com/topic/127162-remember-that-battleship-game/#findComment-657792 Share on other sites More sharing options...
Bendude14 Posted October 6, 2008 Share Posted October 6, 2008 this line is wrong echo "Allowed Misses: ".(20- $_SESSION['Allowed Misses']; needs to be echo "Allowed Misses: ".(20- $_SESSION['Allowed Misses']); can you expalin more what problems you are having? Link to comment https://forums.phpfreaks.com/topic/127162-remember-that-battleship-game/#findComment-657905 Share on other sites More sharing options...
PFMaBiSmAd Posted October 6, 2008 Share Posted October 6, 2008 You need to be learning php, developing php code, or debugging php code on a system where error_reporting is set to E_ALL and display_errors is set to ON in your php.ini to get php to help you with parse, warning, and notice errors. The posted code contains the following fatal parse error (which will only be shown if those two settings are set before the page is requested) - Parse error: syntax error, unexpected ';' in your_file.php on line 334 Link to comment https://forums.phpfreaks.com/topic/127162-remember-that-battleship-game/#findComment-657953 Share on other sites More sharing options...
bicho Posted October 17, 2008 Share Posted October 17, 2008 I made some changes to your program, check it out: 1.- variables can not have spaces in the variable name. 2.- your numbers displayed on the Game board and the 'Show Ships' don't match, you have a list of numbers from 0 thru 9 and the other list is from 1 thru 10. 3.- I added some esthetics to the program... 4.- Check it out... <?PHP session_cache_limiter('private, must-revalidate'); session_start(); ?> <html> <head> <style> table, tr,td { border: 1px solid blue; } </style> </head> <body background="bgship.jpg"> <center> <?PHP //Session variables... if (!isset($_SESSION['Progress'])) { $_SESSION['Progress'] = 1; setvar(); overall(); } function overall() { $_SESSION['Minimum']=0; $_SESSION['Maximum']=0; $_SESSION['Average']=0; $_SESSION['Games_Played']=0; } function setvar() { $_SESSION['Misses'] = 0; $_SESSION['Allowed Misses'] = 0; $_SESSION['Aircraft Carrier']= 0; $_SESSION['Sunk']=0; $_SESSION['Battleship']= 0; $_SESSION['Destroyer']= 0; $_SESSION['Submarine']= 0; $_SESSION['Patrol_Boat'] = 0; $_SESSION['Grid']= array(); $_SESSION['Attacked'] = array(); setgrid(); setships(); } //set the session's grid... function setgrid() { for ($i=1; $i<11; $i++) { for($j=1; $j<11; $j++) { $array[$i][$j] = 0; } } $_SESSION['Grid'] = $array; $_SESSION['Attacked'] = $array; } function setships() { $ships= array( 'Aircraft Carrier' => 5, 'Battleship' => 4, 'Destroyer' => 3, 'Submarine'=> 3, 'Patrol Boat' => 2 ); $shipcounter=1; $grid = $_SESSION['Grid']; //send the ships through a foreach loop foreach( $ships as $name => $size) { while (TRUE) { //if it is 1, go left-right, if it is 0, go up-down if (mt_rand(0,1) == 1) { $coord= 'x'; } else { $coord= 'y'; } //independant of $coord, must apply boundaries $max = array( 'x' => 10, 'y' =>10); //subtract from the boundaries, the size of the ship for placement $max[ $coord ] -=$size; //set x and y for placement of the ship's first spot $x = mt_rand( 1, $max['x']); $y = mt_rand( 1, $max['y']); //put ship in a for loop to find other spots for ($i =0; $i < $size; $i++ ) { //set temporary array to hold values of first spot $temp = array ('x' => $x, 'y' => $y ); $temp[ $coord ] += $i; //check if there is already a ship for all spots, if so start loop over if ($grid[$temp['x']][$temp['y']]!=0) { //continue 2 to resume at the while loop continue 2; } } //all spots are available, end the infinite loop break; } //spots available, place ships for ($i = 0; $i != $size; $i++) { $temp= array ('x' => $x, 'y' =>$y); $temp[$coord] += $i; $grid[$temp['x']][$temp['y']]=$shipcounter; } $shipcounter++; } $_SESSION['Grid'] = $grid; } echo "<form action ='".$_SERVER['PHP_SELF']."' method='POST'>"; echo "<input type='submit' name='shoot' value='Shoot the Ships/Show Game Board'><br>"; echo "<input type='submit' name='I_give_up' value='Abort Game'>"; echo "<input type='submit' name='show_ships' value='Show Ships Position'>"; echo "</form>"; if(isset($_POST['shoot'])) { $temp1= $_SESSION['Grid']; $temp2= $_SESSION['Attacked']; $location = $_POST['hitme']; for ($i=1; $i<11; $i++) { for ($j=1; $j<11; $j++) { if((($i * 10)+ $j) == $location) { if ($temp1[$i][$j] == 1) { $temp2[$i][$j]=1; $_SESSION['Aircraft Carrier']++; echo "Hit"; if ($_SESSION['Aircraft Carrier']==5) { echo "SUNK AIRCRAFT CARRIER"; $_SESSION['Sunk']++; } if ($_SESSION['Sunk']==5) { $_SESSION['Games_Played']++; setvar(); } } else if ($temp1[$i][$j] == 2) { $temp2[$i][$j]=1; $_SESSION['Battleship']++; echo "Hit"; if ($_SESSION['Battleship']==4) { echo "SUNK BATTLESHIP"; $_SESSION['Sunk']++; } if ($_SESSION['Sunk']==5) { $_SESSION['Games_Played']++; setvar(); } } else if ($temp1[$i][$j] == 3) { $temp2[$i][$j]=1; $_SESSION['Destroyer']++; echo "Hit"; if ($_SESSION['Destroyer']==3) { echo "SUNK DESTROYER"; $_SESSION['Sunk']++; } if ($_SESSION['Sunk']==5) { $_SESSION['Games_Played']++; setvar(); } } else if ($temp1[$i][$j] == 4) { $temp2[$i][$j]=1; $_SESSION['Submarine']++; echo "Hit"; if ($_SESSION['Submarine']==3) { echo "SUNK SUBMARINE"; $_SESSION['Sunk']++; } if ($_SESSION['Sunk']==5) { $_SESSION['Games_Played']++; setvar(); } } else if ($temp1[$i][$j] == 5) { $temp2[$i][$j]=1; $_SESSION['Patrol_Boat']++; echo "Hit"; if ($_SESSION['Patrol_Boat']==2) { echo "SUNK PATROL BOAT"; $_SESSION['Sunk']++; } if ($_SESSION['Sunk']==5) { $_SESSION['Games_Played']++; setvar(); } } else { $temp2[$i][$j]=2; echo "Try Again"; $_SESSION['Misses']++; $_SESSION['Allowed Misses']++; if ($_SESSION['Allowed Misses']==20) { if ($_SESSION['Allowed Misses']>$_SESSION['Maximum']) { $_SESSION['Maximum'] = $_SESSION['Allowed Misses']; } else if ($_SESSION['Allowed Misses']<=$_SESSION['Minimum']) { $_SESSION['Minimum'] = $_SESSION['Allowed Misses']; } $_SESSION['Games_Played']++; $_SESSION['Average']+=($_SESSION['Allowed Misses']); setvar(); echo "TOO MANY MISSES. YOU LOST THE GAME"; } } } } } $_SESSION['attacked']= $temp2; } else if (isset($_POST['I_give_up'])) { if ($_SESSION['Allowed Misses']>$_SESSION['Maximum']) { $_SESSION['Maximum'] = $_SESSION['Allowed Misses']; } else if ($_SESSION['Allowed Misses']<=$_SESSION['Minimum']) { $_SESSION['Minimum'] = $_SESSION['Allowed Misses']; } $_SESSION['Games_Played']++; $_SESSION['Average']+=($_SESSION['Allowed Misses']); setvar(); echo "<font color=\"red\">"; echo "<h4>YOU LOST THE GAME<br />YOU GAVE UP!</h4>"; echo "</font>"; } if (isset($_POST['show_ships'])) { echo "<table>"; echo "<tr><td width=25 height=25><img src=ship1.jpg width=25 height=25 /></td><th width=25 height=25>A</th><th width=25 height=25>B</th><th width=25 height=25>C</th><th width=25 height=25>D</th><th width=25 height=25>E</th><th width=25 height=25>F</th><th width=25 height=25>G</th><th width=25 height=25>H</th><th width=25 height=25>I</th><th width=25 height=25>J</th></tr>"; $temporary = $_SESSION['Grid']; for($i=0; $i < 10; $i++) { echo "<tr><th width=25 height=25> $i </th>"; for ($j=1; $j < 11; $j++) { if ($temporary[$i][$j] == 1) { echo "<th bgcolor = green>1</th>"; } else if ($temporary[$i][$j] == 2) { echo "<th bgcolor = gray>2</th>"; } else if ($temporary[$i][$j] == 3) { echo "<th bgcolor = cyan>3</th>"; } else if ($temporary[$i][$j] == 4) { echo "<th bgcolor = brown>4</th>"; } else if ($temporary[$i][$j] == 5) { echo "<th bgcolor = magenta>5</th>"; } else echo "<th bgcolor=white>#</th>"; } echo "</tr>"; } } echo "<table border=1pt>"; echo "<tr>"; echo "<td><img src=headership.gif width=315pt height=40pt /></td>"; echo "</tr>"; echo "</table>"; echo "<form action ='".$_SERVER['PHP_SELF']."' method='POST'>"; echo "<table>"; echo "<tr><th width=25 height=25><img src=ship1.jpg width=25 height=25 /></th><th width=25 height=25><font color=white>A</font></th><th width=25 height=25><font color=white>B</font></th><th width=25 height=25><font color=white>C</font></th><th width=25 height=25><font color=white>D</font></th><th width=25 height=25><font color=white>E</font></th><th width=25 height=25><font color=white>F</font></th><th width=25 height=25><font color=white>G</font></th><th width=25 height=25><font color=white>H</font></th><th width=25 height=25><font color=white>I</font></th><th width=25 height=25><font color=white>J</font></th></tr>"; $count=0; $temp3 = $_SESSION['Attacked']; for($i=0; $i < 100; $i += 10) { $h=$i/10; echo "<tr><th><font color=white>$h</font></th>"; for($j = 1; $j < 11;$j++) { $count++; if($temp3[$h+1][$j]==0) { echo "<td><input type=radio name=hitme value=$count></td>"; } else if ($temp3[$h+1][$j]==1) { echo "<th bgcolor = 'RED'></th>"; } else echo "<th bgcolor = 'WHITE'></th>"; } echo "</tr>"; } echo "</table>"; echo "</form>"; echo "<table>"; echo "<tr>"; echo "<td bgcolor=green></td>"; echo "<font size=4 color=white>"; echo "Misses so far: ".$_SESSION['Misses']; echo "<br />Allowed Misses: ".(20 - $_SESSION['Allowed Misses']); echo "<br />Games Played: ".$_SESSION['Games_Played']; echo "<br />Maximum Misses: ".$_SESSION['Maximum']; echo "<br />Minimum Misses: ".$_SESSION['Minimum']; echo "<br />Average Misses: ".$_SESSION['Average']; echo "<br />Ships Sunk : ".$_SESSION['Sunk']; echo "</font>"; echo "</td>"; echo "</tr>"; echo "</table>"; ?> </center> </body> </html> Link to comment https://forums.phpfreaks.com/topic/127162-remember-that-battleship-game/#findComment-667779 Share on other sites More sharing options...
bicho Posted October 17, 2008 Share Posted October 17, 2008 I made some changes to your program, check it out: 1.- variables can not have spaces in the variable name. 2.- your numbers displayed on the Game board and the 'Show Ships' don't match, you have a list of numbers from 0 thru 9 and the other list is from 1 thru 10. 3.- I added some esthetics to the program... 4.- Check it out... <?PHP session_cache_limiter('private, must-revalidate'); session_start(); ?> <html> <head> <style> table, tr,td { border: 1px solid blue; } </style> </head> <body background="bgship.jpg"> <center> <?PHP //Session variables... if (!isset($_SESSION['Progress'])) { $_SESSION['Progress'] = 1; setvar(); overall(); } function overall() { $_SESSION['Minimum']=0; $_SESSION['Maximum']=0; $_SESSION['Average']=0; $_SESSION['Games_Played']=0; } function setvar() { $_SESSION['Misses'] = 0; $_SESSION['Allowed Misses'] = 0; $_SESSION['Aircraft Carrier']= 0; $_SESSION['Sunk']=0; $_SESSION['Battleship']= 0; $_SESSION['Destroyer']= 0; $_SESSION['Submarine']= 0; $_SESSION['Patrol_Boat'] = 0; $_SESSION['Grid']= array(); $_SESSION['Attacked'] = array(); setgrid(); setships(); } //set the session's grid... function setgrid() { for ($i=1; $i<11; $i++) { for($j=1; $j<11; $j++) { $array[$i][$j] = 0; } } $_SESSION['Grid'] = $array; $_SESSION['Attacked'] = $array; } function setships() { $ships= array( 'Aircraft Carrier' => 5, 'Battleship' => 4, 'Destroyer' => 3, 'Submarine'=> 3, 'Patrol Boat' => 2 ); $shipcounter=1; $grid = $_SESSION['Grid']; //send the ships through a foreach loop foreach( $ships as $name => $size) { while (TRUE) { //if it is 1, go left-right, if it is 0, go up-down if (mt_rand(0,1) == 1) { $coord= 'x'; } else { $coord= 'y'; } //independant of $coord, must apply boundaries $max = array( 'x' => 10, 'y' =>10); //subtract from the boundaries, the size of the ship for placement $max[ $coord ] -=$size; //set x and y for placement of the ship's first spot $x = mt_rand( 1, $max['x']); $y = mt_rand( 1, $max['y']); //put ship in a for loop to find other spots for ($i =0; $i < $size; $i++ ) { //set temporary array to hold values of first spot $temp = array ('x' => $x, 'y' => $y ); $temp[ $coord ] += $i; //check if there is already a ship for all spots, if so start loop over if ($grid[$temp['x']][$temp['y']]!=0) { //continue 2 to resume at the while loop continue 2; } } //all spots are available, end the infinite loop break; } //spots available, place ships for ($i = 0; $i != $size; $i++) { $temp= array ('x' => $x, 'y' =>$y); $temp[$coord] += $i; $grid[$temp['x']][$temp['y']]=$shipcounter; } $shipcounter++; } $_SESSION['Grid'] = $grid; } echo "<form action ='".$_SERVER['PHP_SELF']."' method='POST'>"; echo "<input type='submit' name='shoot' value='Shoot the Ships/Show Game Board'><br>"; echo "<input type='submit' name='I_give_up' value='Abort Game'>"; echo "<input type='submit' name='show_ships' value='Show Ships Position'>"; echo "</form>"; if(isset($_POST['shoot'])) { $temp1= $_SESSION['Grid']; $temp2= $_SESSION['Attacked']; $location = $_POST['hitme']; for ($i=1; $i<11; $i++) { for ($j=1; $j<11; $j++) { if((($i * 10)+ $j) == $location) { if ($temp1[$i][$j] == 1) { $temp2[$i][$j]=1; $_SESSION['Aircraft Carrier']++; echo "Hit"; if ($_SESSION['Aircraft Carrier']==5) { echo "SUNK AIRCRAFT CARRIER"; $_SESSION['Sunk']++; } if ($_SESSION['Sunk']==5) { $_SESSION['Games_Played']++; setvar(); } } else if ($temp1[$i][$j] == 2) { $temp2[$i][$j]=1; $_SESSION['Battleship']++; echo "Hit"; if ($_SESSION['Battleship']==4) { echo "SUNK BATTLESHIP"; $_SESSION['Sunk']++; } if ($_SESSION['Sunk']==5) { $_SESSION['Games_Played']++; setvar(); } } else if ($temp1[$i][$j] == 3) { $temp2[$i][$j]=1; $_SESSION['Destroyer']++; echo "Hit"; if ($_SESSION['Destroyer']==3) { echo "SUNK DESTROYER"; $_SESSION['Sunk']++; } if ($_SESSION['Sunk']==5) { $_SESSION['Games_Played']++; setvar(); } } else if ($temp1[$i][$j] == 4) { $temp2[$i][$j]=1; $_SESSION['Submarine']++; echo "Hit"; if ($_SESSION['Submarine']==3) { echo "SUNK SUBMARINE"; $_SESSION['Sunk']++; } if ($_SESSION['Sunk']==5) { $_SESSION['Games_Played']++; setvar(); } } else if ($temp1[$i][$j] == 5) { $temp2[$i][$j]=1; $_SESSION['Patrol_Boat']++; echo "Hit"; if ($_SESSION['Patrol_Boat']==2) { echo "SUNK PATROL BOAT"; $_SESSION['Sunk']++; } if ($_SESSION['Sunk']==5) { $_SESSION['Games_Played']++; setvar(); } } else { $temp2[$i][$j]=2; echo "Try Again"; $_SESSION['Misses']++; $_SESSION['Allowed Misses']++; if ($_SESSION['Allowed Misses']==20) { if ($_SESSION['Allowed Misses']>$_SESSION['Maximum']) { $_SESSION['Maximum'] = $_SESSION['Allowed Misses']; } else if ($_SESSION['Allowed Misses']<=$_SESSION['Minimum']) { $_SESSION['Minimum'] = $_SESSION['Allowed Misses']; } $_SESSION['Games_Played']++; $_SESSION['Average']+=($_SESSION['Allowed Misses']); setvar(); echo "TOO MANY MISSES. YOU LOST THE GAME"; } } } } } $_SESSION['attacked']= $temp2; } else if (isset($_POST['I_give_up'])) { if ($_SESSION['Allowed Misses']>$_SESSION['Maximum']) { $_SESSION['Maximum'] = $_SESSION['Allowed Misses']; } else if ($_SESSION['Allowed Misses']<=$_SESSION['Minimum']) { $_SESSION['Minimum'] = $_SESSION['Allowed Misses']; } $_SESSION['Games_Played']++; $_SESSION['Average']+=($_SESSION['Allowed Misses']); setvar(); echo "<font color=\"red\">"; echo "<h4>YOU LOST THE GAME<br />YOU GAVE UP!</h4>"; echo "</font>"; } if (isset($_POST['show_ships'])) { echo "<table>"; echo "<tr><td width=25 height=25><img src=ship1.jpg width=25 height=25 /></td><th width=25 height=25>A</th><th width=25 height=25>B</th><th width=25 height=25>C</th><th width=25 height=25>D</th><th width=25 height=25>E</th><th width=25 height=25>F</th><th width=25 height=25>G</th><th width=25 height=25>H</th><th width=25 height=25>I</th><th width=25 height=25>J</th></tr>"; $temporary = $_SESSION['Grid']; for($i=0; $i < 10; $i++) { echo "<tr><th width=25 height=25> $i </th>"; for ($j=1; $j < 11; $j++) { if ($temporary[$i][$j] == 1) { echo "<th bgcolor = green>1</th>"; } else if ($temporary[$i][$j] == 2) { echo "<th bgcolor = gray>2</th>"; } else if ($temporary[$i][$j] == 3) { echo "<th bgcolor = cyan>3</th>"; } else if ($temporary[$i][$j] == 4) { echo "<th bgcolor = brown>4</th>"; } else if ($temporary[$i][$j] == 5) { echo "<th bgcolor = magenta>5</th>"; } else echo "<th bgcolor=white>#</th>"; } echo "</tr>"; } } echo "<table border=1pt>"; echo "<tr>"; echo "<td><img src=headership.gif width=315pt height=40pt /></td>"; echo "</tr>"; echo "</table>"; echo "<form action ='".$_SERVER['PHP_SELF']."' method='POST'>"; echo "<table>"; echo "<tr><th width=25 height=25><img src=ship1.jpg width=25 height=25 /></th><th width=25 height=25><font color=white>A</font></th><th width=25 height=25><font color=white>B</font></th><th width=25 height=25><font color=white>C</font></th><th width=25 height=25><font color=white>D</font></th><th width=25 height=25><font color=white>E</font></th><th width=25 height=25><font color=white>F</font></th><th width=25 height=25><font color=white>G</font></th><th width=25 height=25><font color=white>H</font></th><th width=25 height=25><font color=white>I</font></th><th width=25 height=25><font color=white>J</font></th></tr>"; $count=0; $temp3 = $_SESSION['Attacked']; for($i=0; $i < 100; $i += 10) { $h=$i/10; echo "<tr><th><font color=white>$h</font></th>"; for($j = 1; $j < 11;$j++) { $count++; if($temp3[$h+1][$j]==0) { echo "<td><input type=radio name=hitme value=$count></td>"; } else if ($temp3[$h+1][$j]==1) { echo "<th bgcolor = 'RED'></th>"; } else echo "<th bgcolor = 'WHITE'></th>"; } echo "</tr>"; } echo "</table>"; echo "</form>"; echo "<table>"; echo "<tr>"; echo "<td bgcolor=green></td>"; echo "<font size=4 color=white>"; echo "Misses so far: ".$_SESSION['Misses']; echo "<br />Allowed Misses: ".(20 - $_SESSION['Allowed Misses']); echo "<br />Games Played: ".$_SESSION['Games_Played']; echo "<br />Maximum Misses: ".$_SESSION['Maximum']; echo "<br />Minimum Misses: ".$_SESSION['Minimum']; echo "<br />Average Misses: ".$_SESSION['Average']; echo "<br />Ships Sunk : ".$_SESSION['Sunk']; echo "</font>"; echo "</td>"; echo "</tr>"; echo "</table>"; ?> </center> </body> </html> Link to comment https://forums.phpfreaks.com/topic/127162-remember-that-battleship-game/#findComment-667783 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.