3

I am trying to build a game board 8x8 for a small game of battleship with game pieces in place (kind of like checkers) so i could move the pieces with MySQL the players can move freely in the board to go against each others battleships.

the pieces will be place in predetermined spaces while other space will be empty and be handle by mysql

$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/b1.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')
      );

I already have a loop to display the board what I'm asking is how do I place the ($piece -> $board) I know you can use the array_replace to place the elements of and array into another array, but I do not know how with multidimensional arrays.

I am also trying to use mysql for movement inside the board

1
  • array_replace won't help you here, it works differently than you think. Best would be array_map but if you use PHP < 5.3 it is difficult to pass the $pieces array to the callback. Commented Oct 24, 2010 at 7:58

1 Answer 1

6

If you use PHP >= 5.3, you can use array_map:

$callback = function($value) use ($pieces) {
    if(array_key_exists($value, $pieces)) {
        return $pieces[$value];
    }
    return $value;
}

foreach($board as &$row) {
    $row = array_map($row, $callback);
}

If you use PHP < 5.3, you can use array_walk_recursive:

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

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

The not PHP 5.3 version would be shorter in both situations ;)

Update:

DEMO HERE :)

10
  • @Felix Kling all i ahve to is put that into the php and thats it Commented Oct 24, 2010 at 8:04
  • @hgbso: Not sure if I understand your question, but $board and $pieces would be the two arrays from your example. You have to pass them to array_walk_recursive. Commented Oct 24, 2010 at 8:06
  • @hgbso: You can find a demo here: codepad.org/ekmS46nz (make sure you use the right image link for b2 ;)) Commented Oct 24, 2010 at 8:09
  • sorry I am that should i put that function after the arrays $board $pieces Commented Oct 24, 2010 at 8:15
  • @hgbso: Like with any function, you can only call array_walk_recursive($board, 'map', $pieces); if $board and $pieces a defined previously. function map(){} can be elsewhere. Commented Oct 24, 2010 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.