Jump to content

Help trying to figure out what I am doing


webdevdea

Recommended Posts

Trying to make a game where the user plays the computer, the user does not get to place ships and the computer does not try to sink the human's ships.

on submit, the game is supposed to tell the user hit or miss or you sank my ship

the board is supposed to remember where the user clicked each time until the game is over

the computer randomly places 5 boats,

I have the computer placing the boats, and I have some fire buttons, but its not connecting, and its not supposed to show where the boats are ( user is not supposed to see that part) 

<?php session_start() ?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Battleship</title>
<style type = "text/css">

#footer {font-size: small;
         text-align:center;
  clear:right;
         padding-bottom:20px;
}
body { 
background-image:url("infor.jpg");
 color: white;text-align:center  }
p {  color: yellow; text-align:center;}
</style>
</head>
<body>
 
<?php
 
 if(isset($_POST['start_game']) || isset($_POST['myGuess'])){
if (isset($_POST["counter"])){
$counter = $_POST["counter"];
 } else {
$counter = 0;
 } // end if
 $counter++;
 
 //store new data in counter
 $_SESSION["counter"] = $counter;
  if(isset($_POST['start_game']) || isset($_POST['myGuess'])){
if (isset($_POST["counter"])){
$counter = $_POST["counter"];
 } else {
$counter = 0;
 } // end if
 $counter++;
 
 //store new data in counter
 $_SESSION["counter"] = $counter;
 

 
if(isset($_POST['choice'])){
if($_POST['choice']=="correct")
print "<br>I got it!
it took me $counter tries. <br>";
 }
 $myGuess=rand($lowest,$highest);
 
//This is used for debugging - keeping track of what each value is when the submit button is pressed
// if(isset($_POST['choice'])){
//print_r($_POST);
//}  
$form = "showOptions";
 }else{
$form = "showStart";
 }
 
 
if($form == "showOptions"){
?>
<h2>
<form method="post" action="" name="target">
<input type="Submit" value="Submit" align="MIDDLE">
</form> </h2>


<form action="" method="post" > 

<table border="1" align="center"> 

<?PHP

 $step = $_POST['step']; 


if ($step > 2) { 

    $step = 0; 

} else { 

    $step += 1; 

}//end if 

if($step == 1) { 

   for ($i = a; $i < k; $i++){ 

     echo "<tr><td width='20' 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 

?> 

 

<input type="hidden" name="step" value="<?php echo "$step";?>" />

</form> 

</center> 
<?php

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 

?> 

 

<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='battleshipmajorstart.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>

<?php
}
?>
 <p id="footer">Copyright © 2013 DeAnna Riddlespur & Gene Lau & Matthew Semple</p>
</body>
</html>
  
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.