Php 将值指定给多维数组以进行检查
我的问题是不用坐标来移动,我能分配数字和字母吗?这样我就可以用这些值移动了 编辑:我正在将线路板输出到HTML8x8表格Php 将值指定给多维数组以进行检查,php,multidimensional-array,Php,Multidimensional Array,我的问题是不用坐标来移动,我能分配数字和字母吗?这样我就可以用这些值移动了 编辑:我正在将线路板输出到HTML8x8表格 $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
$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),
);
因此,当用户输入:From: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 )
);
解析
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
}
或者是$piece=$board[$coords1[0]][$coords1[1]];不能使用是的,您可以这样做。您只需要确保能够使用某种算法移动到该区域,并确保在移动值时更改值 @您最近的评论: 如果您看一看这篇文章,其中有一节介绍多维数组,这对于参考非常有帮助。这应该对你有帮助。让我知道这是否解决了你的问题
是:将输入字符串解析为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;
}
代表电路板的片段数组?我在帖子中编辑了数组,或者我需要在使用您发布的代码之前对数组进行循环,因为我已经在循环中创建了html表8x8 boardNo loops,请确保更新变量,使它们不会冲突。因此,使用
$square
而不是$board
,并将用户输入的方块存储在$square
$piece=$board[$coords[0]][$coords[1]];//例如,将其更改为$piece=$square[$coords[0]][$coords[1]];//例如,没错。只要把变量映射成新的名称就行了。当我移动它们时,我该如何改变它们的值呢?我知道有一些数组函数
$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
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;
}
$coords = parseSquare($square);
if ($coords === FALSE) {
// Invalid input, handle this case.
} else {
$piece = $board[$coords[0]][$coords[1]]; // for example
}