Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
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. –  Felix Kling Oct 24 '10 at 7:58
add comment

1 Answer

up vote 6 down vote accepted

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 :)

share|improve this answer
    
@Felix Kling all i ahve to is put that into the php and thats it –  hgbso Oct 24 '10 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. –  Felix Kling Oct 24 '10 at 8:06
    
@hgbso: You can find a demo here: codepad.org/ekmS46nz (make sure you use the right image link for b2 ;)) –  Felix Kling Oct 24 '10 at 8:09
    
sorry I am that should i put that function after the arrays $board $pieces –  hgbso Oct 24 '10 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. –  Felix Kling Oct 24 '10 at 8:18
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.