Chess-Challenge icon indicating copy to clipboard operation
Chess-Challenge copied to clipboard

how to check if a piece has an attack available (on your side )

Open Marki2019 opened this issue 2 years ago • 3 comments

Okay so i am kinda starting out C# and this whole thing is new to me so i am a little confused. Do i just loop through the entire board and checking every square or is there a craftier way to do this.

Help would be appreciated

Marki2019 avatar Jul 24 '23 20:07 Marki2019

By attack do you mean a capture move? If so, you can just loop through all the moves in board.getLegalMoves() and check if the start square of the move is the square of your piece and the captured piece of the move isn't PieceType.None

mcthouacbb avatar Jul 24 '23 22:07 mcthouacbb

Use board.getLegalMoves(true)

SomeRandomOnlineDev avatar Jul 25 '23 03:07 SomeRandomOnlineDev

Use board.getLegalMoves(true)

Specifically, the line: Move[] allCaptureMoves = board.GetLegalMoves(true); will give you all of your moves stored in an array variable named "allCaptureMoves". You can then iterate over these moves to check which pieces have captures available, or look to see if the piece you are interested in is in any of the moves in the array.

dudecon avatar Jul 25 '23 05:07 dudecon