我的问题是,不是使用坐标来移动,我可以分配数字和字母,这样我就可以使用这些值移动
编辑:我正在将电路板输出到html 8x8表
$square = 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),
);
所以当用户输入时:从:F1到:G2,棋子移动
我这样做会不会更好
Array ( 'A' => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 )
'B' => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 )
'C' => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 )
'D' => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 )
'E' => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 )
'F' => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 )
'G' => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 )
'H' => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 )
);
parseSquareFrom
function parseSquareFrom() {
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
}
和parseSquareTo
function parseSquareTo() {
if (strlen($square1) != 2) {
return FALSE;
}
$coords1 = array(ord('A') - ord($square1[0]),
$square1[1] - 1);
// Perform bounds-checking.
if ($coords1[0] < 0 || $coords1[0] > 7 || $coords1[1] < 0 || $coords1[1] > 7) {
return FALSE;
}
return $coords1;
}
$coords1 = parseSquare($square);
if ($coords1 === FALSE) {
// Invalid input, handle this case.
} else {
$piece = $board[$coords1[0]][$coords1[1]]; // for example
}
我可以使用此代码吗
$board[$coords1[0]-1][$coords1[1]+1] = $board[$coords[0]][$coords[1]];
$board[$coords[0]][$coords[1]] = 0;
//eating action
$board[$coords1[0]][$coords1[1]] = 0;
$board[$coords1[0]-2][$coords1[1]+2] = $board[$coords[0]][$coords[1]];
//if player is 'up' then the value of $way is 1 so
$board[$x+(-1*$way)][$y+(1*$way)] = $board[$coords[0]][$coords[1]]; // position 2,2 becomes 1,3
//if player is not 'up' then the value of $way is -1 so
$board[$x+(-1*$way)][$y+(1*$way)] = $board[$coords[0]][$coords[1]]; // position 2,2 becomes 3,1
或者是$ piece = $ board [$ coords1 [0]] [$ coords1 [1]];不能使用
答案 0 :(得分:2)
是:将输入字符串解析为X,Y对。例如:
function parseSquare($square) {
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;
}
因此给出了像$square = "F5";
$coords = parseSquare($square);
if ($coords === FALSE) {
// Invalid input, handle this case.
} else {
$piece = $board[$coords[0]][$coords[1]]; // for example
}
答案 1 :(得分:1)
是的,你可以这样做。您只需要确保能够使用某种算法移动到该区域,并确保在移动它们时更改值。
@您最近的评论: 如果你看看这篇文章,那么有一个多维数组的部分对于引用非常有帮助。这应该对你有帮助。如果这解决了您的问题,请告诉我。