PHP battleship game help plz!!

Hey everyone. So I am trying to create a battleship game.

Instructions:

  1. First create your main routine, and your functions. However, don’t put any code in your function. Just put one print line to make sure your functions are being called at the right time.

  2. Then create the code to create the initial grid. At first, just create the grid. Make sure that works. Then, once it does, try to place the ships on the grid. Make sure than works.

  3. Then attempt to create the code that displays and records a “hit” or “miss” when the user clicks buttons. Think about this. Don’t keep putting the same code under a lot of buttons. Create a function that is called from the button that does the work. You probably want to pass the coordinate of your button (such as row 1 column 2) to your function to determine if it was a hit or miss.

  4. Once you get this working, then update it for it to determine what ship has been hit and whether it has been sunk. You MUST be able to display “You sunk my battleship”. That’s the whole goal of the game.

  5. Finally, the game should tell the user congrads when all the ships have been sunk.

  6. The program should ask if they want to play again, and then reset everything if they do.

Anyway here is my code so far I have created a grid and it displays

I am trying to clear the letters in the grid with a clearBoard() function. Idk if this is necessary but it seems it would be useful to restart the game and stuff like that.

Idk exactly whats going on but I haven’t made it very far into the instructions before I’m kinda stuck.

I am doing something wrong with the clearBoard function as I get an error there:

Any help would be great. Thanks.

[php]<?php

$buttons = array();
$array_of_ship_locations = array();
//print array legible
function print_r2($val){
echo ‘

’;
print_r($val);
echo ‘
’;
}
// Constructor class assigning constants
// takes supplied code and loops through array to determine how many times 
// that ship has been hit.

makeBoard();
clearBoard();
//Array to hold the grid data

//clear board function
function clearBoard() {
//initialize board with a null in each cell
global $boardData, $buttons,

for ($row = 0; $row < $boardData["$cnt"]; $col++)
	for ($col = 0; $col < $boardData["$name"]; $col++){
	  $board[$row][$col] = ".";
	} // end col for loop
  } // end row for loop
} // end clearBoard

}

function makeBoard(){
//Fill board with ships by calling placeShips for each ship size

global $buttons;
$direction = array(“E”, “S”);
$cnt = 0;

for ($i = ‘a’; $i != ‘k’; $i++){
echo “

”;
echo “”;
for ($j = 1; $j < 11; $j++){
$name = $i . $j;
                        echo "<td><button type='submit'  style= 'width:42;height:24' value='$name' name='button_pressed'
                        id='lol'> $name </button></td>";
                    
            $buttons[$cnt] = $name;     //populates an array with a unique identifier to each button. 
             $cnt++;
             
           } // 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>";
    print "<br><br>";
    print "These are the values of every button on the grid in a single array. ";
	print_r2($buttons);

}

function addShips(){

$array_of_ship_locations = array();
}

//Function that is called from fire button, accepts coordinates from the radio button and determines if it was a hit or miss

//clear game board and start new game

?>[/php]

$i

Well I already realized some obvious problems actually… I have a long way to go…

Basically I have a grid. I want to be able to see Letters on the grid which identify with each type of ship.

‘Carrier’ would show C for every spot in the grid that it takes up for example
‘Battleship’ a B
‘Destroyer’ D…
‘Submarine’ …
‘Patrol Boat’ …

here is the code that shows the grid.

[php]
function makeBoard(){
//Fill board with ships by calling placeShips for each ship size

global $buttons;
$direction = array(“E”, “S”);
global $cnt

for ($i = ‘a’; $i != ‘k’; $i++){
echo “

”;
echo “”;
for ($j = 1; $j < 11; $j++){
$name = $i . $j;
                        echo "<td><button type='submit'  style= 'width:42;height:24' value='$name' name='button_pressed'
                        id='lol'> $name </button></td>";
                    
            $buttons[$cnt] = $name;     //populates an array with a unique identifier to each button. 
             $cnt++;
             
           } // 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>";
    print "<br><br>";
    print "These are the values of every button on the grid in a single array. ";
	print_r2($buttons);

}

makeBoard();[/php]

$i

ok this is where me and my partner are at. We have two functions. One to create the board and the other to place ships on it. Not quite working yet though.

[php]<?php

//print array legible
function print_r2($val){
echo ‘

’;
print_r($val);
echo ‘
’;
}

$buttons = array();

$ships = array(
‘Carrier’ => 5,
‘Battleship’ => 4,
‘Destroyer’ => 3,
‘Submarine’ => 3,
‘Patrol Boat’ => 2,

);

//create ship array

// takes supplied code and loops through array to determine how many times 
// that ship has been hit.

makeBoard();
placeShips();
//Array to hold the grid data

function makeBoard(){
//Fill board with ships by calling placeShips for each ship size

global $buttons;
$direction = array(“E”, “S”);
$cnt = 0;

for ($i = ‘a’; $i != ‘k’; $i++){
echo “

”;
echo “”;
for ($j = 1; $j < 11; $j++){
$name = $i . $j;
                        echo "<td><button type='submit'  style= 'width:42;height:24' value='$name' name='button_pressed'
                        id='lol'> $name </button></td>";
                    
            $buttons[$cnt] = $name;     //populates an array with a unique identifier to each button. 
             $cnt++;
             
           } // 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>";
    print "<br><br>";
    print "These are the values of every button on the grid in a single array. ";
	print_r2($buttons);

}

function placeShips() {

global $ships; 

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 );
	}
	
}

}
//Function that is called from fire button, accepts coordinates from the radio button and determines if it was a hit or miss

//clear game board and start new game

?>[/php]

$i
Sponsor our Newsletter | Privacy Policy | Terms of Service