PHP move checker pieces inside multi-dimensional array part 2
I already have the pieces mapped out in the array and it prints just fine
$board = array(
array('1','rs','1','rs','1','rs','1','rs'),
array('rs','1','rs','1','rs','1','rs','1'),
array('1','rs','1','rs','1','rs','1','rs'),
array('rs','bs','rs','bs','rs','bs','rs','bs'),
array('bs','rs','bs','rs','bs','rs','bs','rs'),
array('2','bs','2','bs','2','bs','2','bs'),
array('bs','2','bs','2','bs','2','bs','2'),
array('2','bs','2','bs','2','bs','2','bs')
);
1 = black pieces
2 =开发者_运维知识库 red pieces rs = red square bs = black squarethis code parse the input of a player : example FROM F2 into (0,0) coordinates
function parseSquareFrom($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;
}
I have repeated the same function for the TO input ( to where the player wants to move the piece
my question is this next code a valid way to move with the functions above
$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]];
$way = ($_POST['way'] === 'up')? 1:-1;
$way = ($_POST['way'] === 'down')? -1:+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
I plan to have a function to update the tile movements in the screen so the piece moves as the it highlights the piece as is moving to the next square
this is using serialize into file to hold start positions, movements, kings and queens positions
I'm going to assume that $coords
is a $_POST
variable like a suggested on the first part of this question. If that's the case, the first part of your code is correct:
$board[$coords1[0]-1][$coords1[1]+1] = $board[$coords[0]][$coords[1]];
$board[$coords[0]][$coords[1]] = 0;
This moves a piece diagonally one step up-right in the board. The second part on the other hand skips the actual 'eating' action. Unless, the location specified by the user is the one where the enemy piece is. In which case you code would work.
As for the bound-checking you do, you're not being bullet-proof since adding 2 in the eating move, while a piece is next to a border would result in your code trying to place the piece out of the board. So you could check bounds, depending on the move, so if the move is a standard diagonal one, you should check that the ending position is within the limits, not the starting one since you are assuming that piece is in a correct position before.
I think you have a few issues
$board[$coords1[0]-1][$coords1[1]+1] = $board[$coords[0]][$coords[1]];
if $coords[1] == 7 (which appears to be allowed) then $coords[1]+1 == 8 which is out of bounds. Let alone the coords +/- 2 we see later.
You probably don't want that 'bs'/'rs' in your storage, all of that can be inferred.
$getSquareColor = function($x,$y){return ($x+$y)/2 == 1 ? 'red' : 'black';} //or better yet, const or enum
function getSquareColor($x, $y) //alternatively
{
if ( ($x+$y) % 2 == 0) return 'black';
else return 'red';
}
ps - if you drop those dollar signs the function is just as good in javascript
精彩评论