My question is instead of using coordinates to move can i assign numbers and letters so i can move with those values
Edit: I am outputting the board to html 8x8 table
$square = array( // A B C D E F G H
0 array(0,0,0,0,开发者_JAVA百科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),
);
so when the user inputs : From: F1 to: G2 the pieces move
wouldn't it be better is i do this
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
}
and 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
}
can i use that with this code
$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
or is the $piece = $board[$coords1[0]][$coords1[1]]; cannot be used
Yes: Parse the input string into an X,Y pair. For example:
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;
}
So given a square string like $square = "F5";
$coords = parseSquare($square);
if ($coords === FALSE) {
// Invalid input, handle this case.
} else {
$piece = $board[$coords[0]][$coords[1]]; // for example
}
Yes you can do that. You will just need to make sure that you are able to move to that area with some algorithm and make sure to change values when moving them around.
@ your recent comment: If you take a look at this article there is a section for multidimensional arrays that is pretty helpful for a reference. This should help you. Let me know if this solves your issue.
The Article Link
精彩评论