Im working on this game, but its a work in progress
i need to get the fire buttons in the grid first and then I will go from there- I have all the elements of the game I think, I just need to make them work together. In this game the user does not get fired upon nor does he have any ships..
please and thank you ..
<?php
if(isset($_POST['start_game'])
$form = "showOptions";
}else{
$form = "showStart";
}
if($form == "showOptions"){
?>
<h2>
<form method="post" action="" name="target">
<input type="Submit" value="Submit" align="MIDDLE">
</form></h2>
<?php
$step = $_POST['step'];
if ($step > 2) {
$step = 0;
} else {
$step += 1;
}//end if
//player fireing
if ($step == 0) {
for ($i = a; $i < k; $i++){
echo "<tr><td width='10' align ='right'>$i</td>";
for ($j = 1; $j < 11; $j++){
echo "<td><input type='submit' value='fire' name='$i$j'></td>";
} // end for loop
echo "</tr>";
} // end for loop
echo "<tr><td></td>";
for ($j = 1; $j < 11; $j++){
echo "<td>$j</td>";
}
echo "</tr></table>";
//player fireing
} else if($step == 1) {
for ($i = a; $i < k; $i++){
echo "<tr><td width='10' align='right'>$i</td>";
for ($j = 1; $j < 11; $j++){
echo "<td><input type='submit' value='fire' name='$i$j'></td>";
} // end for loop
echo "</tr>";
} // end for loop
echo "<tr><td></td>";
for ($j = 1; $j < 11; $j++){
echo "<td>$j</td>";
}
echo "</tr></table>";
// result of fire
} else {
echo "Result ";
for ($i = a; $i < k; $i++){
echo "<tr><td width='10' align='right'>$i</td>";
for ($j = 1; $j < 11; $j++){
echo "<td> <input type='checkbox' checked='checked' disabled='disabled' name='$i$j'></td>";
} // end for loop
echo "</tr>";
} // end for loop
echo "<tr><td></td>";
for ($j = 1; $j < 11; $j++){
echo "<td>$j</td>";
}
echo "</tr></table><br><input type='submit' name='' value='shoot'>";
}//end if
?>
<?php
//Display the grid
echo '<table width="300" height="300" border="1" align="center">';
for ( $row = 1; $row <= 10; $row++ ) {
echo '<tr>';
for( $col = 1; $col <= 10; $col++ ) {
echo '<td width="30" align="center">';
echo ( isset($grid[$row][$col]) ? $grid[$row][$col] : ' ' );
echo '</td>';
}
echo '</tr>';
}
echo '</table>';
?>
<input type="hidden" name="step" value="<?php echo "$step";?>" />
<h2>The number of squares for each ship is determined by the type of the ship. <BR />
The ships cannot overlap (i.e., only one ship can occupy any given square in the grid). <BR />
In each round, the Human clicks the target square which is to be shot at. <BR />
The Computer announces whether or not the square is occupied by a ship, <BR />
and if it is a hit they mark this on their own primary grid. <BR />
When all of the squares of a ship have been hit, the ship is sunk, <BR />
and the Computer owner announces this (e.g. You sunk my battleship!!). <BR />
If all of a player's ships have been sunk, the game is over and their opponent wins. </h2>
<h2><?php echo "<br> <a href='game.php'>Reset Game</a>" ?></h2>
<?php
error_reporting( E_ALL );
// $ships['Ship Name'] = size (int);
$ships = array(
'Carrier' => 5,
'Battleship' => 4,
'Destroyer' => 3,
'Submarine' => 3,
'Patrol Boat' => 2
);
// $grid[x-coord][y-coord]
$grid = array();
// Loop through ships for placement
foreach( $ships as $name => $size ) {
// Begin an infinite loop ( dangerous, but we can break it when
// the ship is happily placed )
while ( TRUE ) {
// Determine direction of ship
// x- horizontal, y- vertical
$axis = ( mt_rand(0,1) == 1 ? 'x' : 'y' );
// Maximum values on grid
$max = array( 'x' => 10, 'y' => 10 );
// Subtract $size from the max value to compensate for ship size
$max[ $axis ] -= $size;
// Generate random placement
$x = mt_rand( 1, $max['x'] );
$y = mt_rand( 1, $max['y'] );
// Check to see if the grid is empty by checking $size squares in $axis direction
for ( $i = 0; $i < $size; $i++ ) {
//Create a temporary holder for our coordinates
$curr = array( 'x' => $x, 'y' => $y );
////Add current grid position to the direction we're going
$curr[ $axis ] += $i;
// Check to see if the grids populated
if ( isset( $grid[ $curr['x'] ][ $curr['y'] ] ) )
//If it is, start at the beginning of the while loop and find new coordinates
continue 2;
}
// If the for loop didn't run into a collision, then we know the grid space is empty
//and we can break out of the infinite loop!
break;
}
//Now that we have a position for the ship, write it into the grid!
for ( $i = 0; $i < $size; $i++ ) {
// Create a temporary holder for our coordinates
$curr = array( 'x' => $x, 'y' => $y );
// Add current grid position to the direction we're going
$curr[ $axis ] += $i;
// Add the first letter of the ships name to the grid ( just for example purposes )
$grid[ $curr['x'] ][ $curr['y'] ] = substr( $name, 0, 1 );
}
}
//Display the grid
echo '<table width="300" height="300" border="1" align="center">';
for ( $row = 1; $row <= 10; $row++ ) {
echo '<tr>';
for( $col = 1; $col <= 10; $col++ ) {
echo '<td width="30" align="center">';
echo ( isset($grid[$row][$col]) ? $grid[$row][$col] : ' ' );
echo '</td>';
}
echo '</tr>';
}
echo '</table>';
?>
<?php
} else{
?>
When play begins, The Computer secretly arranges Her ships on Her primary grid. <BR />
Each ship occupies a number of consecutive squares on the grid,
<BR />arranged either horizontally or vertically. <BR />
<BR /> The number of squares for each ship is determined by the type of the ship. <BR />
The ships cannot overlap (i.e., only one ship can occupy any given square in the grid). <BR />
In each round, the Human clicks the target square which is to be shot at. <BR />
The Computer announces whether or not the square is occupied by a ship, <BR />
and if it is a "hit" they mark this on their own primary grid. <BR />
The attacking player notes the hit or miss on their own "tracking" grid, in order to build up a picture of the opponent's fleet. <BR />
When all of the squares of a ship have been hit, the ship is sunk, <BR />
and the ship's owner announces this (e.g. "You sunk my battleship!"). <BR />
If all of a player's ships have been sunk, the game is over and their opponent wins.
<form method="post" action="" name="choice">
<input name="start_game" id="start_game" value="Start Game"
type="submit" src="game.png" name="image" width="100" height="150"><br>
</form>
</html>