我正在使用 Flutter 开发国际象棋游戏,我在创建一个函数来查看其中一个棋子的有效动作时遇到一些困难

问题描述 投票:0回答:1

我创建了一个网格视图并放置了我想要的所有部分。然后,我继续创建一个函数来计算每个棋子的有效移动。我完成了大部分功能,但是我遇到了主教片的问题。这是显示错误的函数。

`列表 checkValideMoves(int row ,int col,ChessPiece?piece){ 列表候选移动= []; int 方向 = 块!.isWhite? -1:1;

switch(piece.type){
  case ChessPieceType.knight:
    //all eight move that the knight can do 
    var knightMoves = [
      [-2,-1]
      ,[-2,1]
      ,[-1,-2],
      [-1,2],
      [1,-2]
      ,[1,2]
      ,[2,-1],
      [2,1]
    ];
    for(var move in knightMoves){
      var newRow = row + move[0];
      var newCol = col + move[1];
      if(!isInBoard(newRow, newCol)){
        continue;
      }
      if(board[newRow][newCol]!=null){
        if(board[newRow][newCol]!.isWhite != piece.isWhite){
          candidateMoves.add([newRow,newCol]);
        }
        continue;
      }
      candidateMoves.add([newRow,newCol]);
    }
    break;
  case ChessPieceType.bishop:
    //diagonal directions
     var directions = [
      [-1,-1]
      ,[-1,1]
      ,[1,-1],
      [1,1]
    ];
    for(var direction in directions){
      var i =0;
      while(true){
        var newRow = row+i *direction[0];
        var newCol = col+i *direction[1];
        if(!isInBoard(newRow, newCol)){
          break;
        }
        if(board[newRow][newCol]!=null){
          if(board[newRow][newCol]!.isWhite != piece.isWhite){
            candidateMoves.add([newRow,newCol]);
          }
          break;
        }
      candidateMoves.add([newRow,newCol]);
      i++;
      }
    }
    break;
}

return candidateMoves;

}`

问题似乎出在计算主教棋子有效移动的代码部分。

flutter dart game-development 2d-games
1个回答
0
投票

您可以使用chess flutter 包

字符串 move1 = 'e3'; 字符串 move2 = 'e4';

最终成功 = chess.move({ “来自”:移动1, “到”:移动2, }); 并检查成功是真还是假。

© www.soinside.com 2019 - 2024. All rights reserved.