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?