how can i store this array into a session and use sessions to move the elements inside the array up/down/left/right diagonally
$board = array(A B C D E F G H
0 array(0,0,0,0,0,0,0,0),
1 array(0,0,0,0,0,0,0,0),
2 array(0,0,0,0,0,0,0,0),
3 array(0,0,0,0,0,0,0,0),
4 array(0,0,0,0,0,0,0,0),
5 array(0,0,0,0,0,0,0,0),
6 array(0,0,0,0,0,0,0,0),
7 array(0,0,0,0,0,0,0,0)
);
I am trying to store this array into a session
$pieces = array(
//checkers pieces player 1
"b" => '<img src="bp.png" width="33" height="37" alt="black piece">',
//Checkers pieces for player2
"r" => '<img src="rp.png" width="33" height="32" alt="red piece">',
// Empty Squares
// Black
"bs" => '<img src="bs.png" width="30" height="30" alt="black square">',
// Red
"rs" => '<img src="rs.png" width="30" height="30" alt="black square">'
);
// 'es' represents empty squares
$board = array( A B C D E F G H
0 array('b','rs','b','rs','b','rs','b','rs'),
1 array('rs','b','rs','b','rs','b','rs','b'),
2 array('b','rs','b','rs','b','rs','b','rs'),
3 array('rs','bs','rs','bs','rs','bs','rs','bs'),
4 array('bs','rs','bs','rs','bs','rs','bs','rs'),
5 array('r','bs','r','bs','r','bs','r','bs'),
6 array('bs','r','bs','r','bs','r','bs','r'),
7 array('r','bs','r','bs','r','bs','r','bs')
);
function map(&$value, $key, $map) {
if(array_key_exists($value, $map)) {
$value = $map[$value];
}
}
array_walk_recursive($board, 'map', $pieces);
and its going to come out into an 8x8 table board when it prints out
I did $_SESSION['board'] = $board;
after the array_walk_recursive
and put it into
echo "<table border='1'>
";
foreach ($_SESSION['board'] as $row)
{
echo "<tr>
";
foreach ($row as $piece){
echo "<td>";
echo "$piece ";
echo "</td>
";
}
}
echo "</tr>
";
echo "</table>
";
}
the user is inputing into this function (FROM input box) F5 - (TO Input) G2 the parses it into coordinates with this function
// parses the users input --FROM-- and to where the user wnats to move the piece
// if the user inputs F1 it parses that into (0,0) coordinates
function parseSquare() {
if (strlen($square) != 2) {
return FALSE;
}
$coords = array(ord('A') - ord($square[0]),
$square[1] - 1);
// Perform bounds-checking.
if ($coords[0] < 0 || $coords[0] > 7 || $coords[1] < 0 || $coords[1] > 7) {
return FALSE;
}
return $coords;
}
$coords = parseSquare($square);
if ($coords === FALSE) {
// Invalid input, handle this case.
} else {
$piece = $board[$coords[0]][$coords[1]]; // for example
}
can i use the function above to move diagonally
$_SESSION['board'][[$new_i]-1][[$new_j] + 1] = $_SESSION['board'][$old_i][$old_j];
$_SESSION['board'][$old_i][$old_j] = ...;