Hey everyone. So I am trying to create a battleship game.
Instructions:
-
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.
-
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.
-
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.
-
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.
-
Finally, the game should tell the user congrads when all the ships have been sunk.
-
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 “
$i | ”;