php - Assigning values to a multi-dimensional array for checkers -
my question instead of using coordinates move can assign numbers , letters can move values
edit: outputting board html 8x8 table
$square = array( // 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), );
so when user inputs : from: f1 to: g2 pieces move
wouldn't better 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 case. } else { $piece = $board[$coords[0]][$coords[1]]; // 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 case. } else { $piece = $board[$coords1[0]][$coords1[1]]; // example }
can use 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 'up' value of $way 1 $board[$x+(-1*$way)][$y+(1*$way)] = $board[$coords[0]][$coords[1]]; // position 2,2 becomes 1,3 //if player not 'up' value of $way -1 $board[$x+(-1*$way)][$y+(1*$way)] = $board[$coords[0]][$coords[1]]; // position 2,2 becomes 3,1
or $piece = $board[$coords1[0]][$coords1[1]]; cannot used
yes: parse input string x,y pair. 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 square string $square = "f5";
$coords = parsesquare($square); if ($coords === false) { // invalid input, handle case. } else { $piece = $board[$coords[0]][$coords[1]]; // example }
Comments
Post a Comment