douyi6168 2010-11-15 21:15
浏览 13
已采纳

为检查器分配值到多维数组

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,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

  • 写回答

2条回答 默认 最新

  • dongnuo3749 2010-11-15 21:23
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接