我创建了一个网格视图并放置了我想要的所有部分。然后,我继续创建一个函数来计算每个棋子的有效移动。我完成了大部分功能,但是我遇到了主教片的问题。这是显示错误的函数。
`列表 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;
}`
问题似乎出在计算主教棋子有效移动的代码部分。
您可以使用chess flutter 包
字符串 move1 = 'e3'; 字符串 move2 = 'e4';
最终成功 = chess.move(