我怎麼能存儲數組到一個會話和使用會話移動陣列上/下/左/右斜PHP會話陣列
$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)
);
我想這個數組存儲到會話中的元素
$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);
及其走出來,進入一個8x8的錶板時,它打印出
我的array_walk_recursive
後做,並把它變成
echo "<table border='1'>\n";
foreach ($_SESSION['board'] as $row)
{
echo "<tr>\n";
foreach ($row as $piece){
echo "<td>";
echo "$piece ";
echo "</td>\n";
}
}
echo "</tr>\n";
echo "</table>\n";
}
用戶輸入查詢到該功能(從輸入框)F5 - (輸入)G2在使用此功能
// 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
}
將其解析爲座標,我可以使用上述功能對角移動
$_SESSION['board'][[$new_i]-1][[$new_j] + 1] = $_SESSION['board'][$old_i][$old_j];
$_SESSION['board'][$old_i][$old_j] = ...;
更新了我的帖子 – hgbso 2010-11-29 06:19:22
以上@hgbso:我沒有看到任何新的問題。 – casablanca 2010-11-29 06:31:16
$ _SESSION ['board'] [$ new_i -1] [$ new_j +1] = $ _SESSION ['board'] [$ old_i] [$ old_j]; $ _SESSION ['board'] [$ old_i] [$ old_j] = ...; 我可以用它來對角移動 – hgbso 2010-11-29 06:32:00