tags:

views:

41

answers:

1

I am trying to build PHP based battleship the php code below displays a board but now im trying to ad values to the individual squares of the board so I can use those values with mysql to move the pieces in place around the board so the players can go after each others battleship:

the game does not involve session it is strictly 2 player same computer and browser inputting on the same screen I already have a database bu i need to setup the tables in the database for the game board movement

How can I create Those database Tables for movements?

$player1=$_POST['p1name'];
$player2=$_POST['p2name'];

 $turn1 =$_POST['turn1'];
 $turn2 =$_POST['turn2']; 

//Display board with columns and rows
function display_board(){

        $pieces = array(
          //battleship 1 player 1
          "b1" => '<img src="images/b1.jpg" width="100" height="100" alt="b1">',
          //battleship 2 player2
          "b2" => '<img src="images/b2.jpg" width="100" height="100" alt="b1">',
         );
              // 'es' represents empty squares
        $board = array(
                 array('b1','es','b1','es','b1','es','b1','es'),
                 array('es','b1','es','b1','es','b1','es','b1'),
                 array('b1','es','b1','es','b1','es','b1','es'),
                 array('es','es','es','es','es','es','es','es'),
                 array('es','es','es','es','es','es','es','es'),
                 array('es','es','es','es','es','es','es','es'),
                 array('b2','es','b2','es','b2','es','b2','es'),
                 array('es','b2','es','b2','es','b2','es','b2'),
                 array('b2','es','b2','es','b2','es','b2','es')
              );

        function map(&$value, $key, $map) {
            if(array_key_exists($value, $map)) {
                $value = $map[$value];
            }
        }

      array_walk_recursive($board, 'map', $pieces);

      print_r ($board);



      //generates an array from 1 to 8 and reverses the order
      $rows = array_reverse(range(1,8));
      // generates an array containing letters from A to H
      $columns = range('A', 'H');

      echo "<table border='3' width='200'>\n";
      // generate header
      echo "<tr><td></td>";
      foreach($columns as $col) {
          echo "<td>" . $col . "</td>";
      }
      echo "</tr>";

      foreach ($board as $index=>$row) {

          echo "<tr>\n";
          echo "<td>" . $rows[$index] . "</td>";
          foreach ($row as $piece){
              echo "<td>";
              echo "$piece ";
              echo "</td>\n";
          }
       }
      echo "</tr>\n";
      echo "</table>\n";  

}



    function user_input(){
          print <<<HERE
          <div id="player1">
          <h2> $player1 </h2>
          <form method="post">
          <input type="text" name="turn1">
          <input type="submit" value="submit">      
          </form>
          </div>
          HERE;

          print <<<HERE
          <div id="player1">
          <h2> $player2 </h2>
          <form method="post">
          <input type="text" name="turn2">
          <input type="submit" value="submit">      
          </form>
          </div>
          HERE;
      }

How can I create Those database Tables for movements?

A: 

To save your $board in a database simply create a table having two columns, one must be an id to identify the board again later. The other is a text column that will store the board. Then INSERT your board into the table using either serialize() or json_encode(). I suggest using json_encode because it is easier to read (and debug) imo - and of course is easily used on client side by javascript if need should ever arise.

If you later load your board from the database you can use unserialize or json_decode to get your array back again.

To move battleships on the board you need some information:

  1. Which player initiated the move?
  2. Which battleship is moved? (eg row/column of the ship)
  3. Where is it moved? / In which direction?

With this information you can identify the battleship in the array, remove it and place it in the destination field, since you have all of this information. If you want to save each single movement in the database (eg for a replay mechanism) you will have to save exactly this information in a table for each movement.

Daniel Baulig
thanks daniel this will help me
hgbso
the tables will be created in the database right
hgbso
yes, a table is created in a database. Maybe you should read up on some more basics, like how to use a SQL database from PHP. There are plenty of tutorials out there.
Daniel Baulig
I know how to create a table in the database is just i didn't clearly understood the beginning of your paragraph and to clarify it is just 1 table
hgbso
Yes, I'd put in in one table and the entire $board into one database field - except you want to be able to WHERE against certain characteristics of the board (eg get all boards where a ship is in the top left corner). But since that is not a usual use-case for a game I would simply take a single column for the $board.
Daniel Baulig