tags:

views:

44

answers:

1

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

+6  A: 

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

Felix Kling
@Felix Kling all i ahve to is put that into the php and thats it
hgbso
@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
@hgbso: You can find a demo here: http://codepad.org/ekmS46nz (make sure you use the right image link for `b2` ;))
Felix Kling
sorry I am that should i put that function after the arrays $board $pieces
hgbso
@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
@Felix Kling http://codepad.org/i8J42z1a I added the loop I mentioned can i get what you think about that loop and Thank You for the help
hgbso
@hgbso: Here is an updated version: http://codepad.org/lriyLf81 You cannot apply `switch` on a `$row` like you did. `$row` contains an array, not a number (`case "8"` is meaningless). And even if it works, you assign a number to `$row` then and you want to loop over it again later (`foreach($row as $pieces)`). Always be careful *which* datatype a variable contains.
Felix Kling
@Felix Kling ok but i did that so i could assign value so then i could use mysql to move around the board
hgbso
@hgbso: Here is another example, if you want to print the row and column headers: http://codepad.org/kmZvN1zR Not sure what you mean with *using mysql to move around the board*. I think you would have to explain how your whole application should work in general, but this would be a new question.
Felix Kling
@Felix Kling I mean use mysql to move and then remove the piece the takes over like in checkers game
hgbso