From 7060f8080cfac55c883718e886fcb919afab6f2b Mon Sep 17 00:00:00 2001 From: sekelsenmat Date: Mon, 11 Apr 2011 05:46:27 +0000 Subject: [PATCH] fpchess: Patch from Brian Chalega da Silva, improves the bishop moves git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@1562 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- applications/fpchess/chessgame.pas | 195 +++++++---------------------- 1 file changed, 45 insertions(+), 150 deletions(-) diff --git a/applications/fpchess/chessgame.pas b/applications/fpchess/chessgame.pas index dbe669700..90456309b 100644 --- a/applications/fpchess/chessgame.pas +++ b/applications/fpchess/chessgame.pas @@ -321,166 +321,61 @@ end; function TChessGame.ValidateBishopMove(AFrom, ATo: TPoint): Boolean; var - AttackedSquares : BitBoard; i,j : Integer; - x,y : integer; - haveCaptured: boolean = false; //already have captured a piece - willBeACapture : boolean = false;// the movement will be a capture - validMove : boolean = false; //if the piece in the 'to' square is not of the same color of the player - mensagem : String; begin - for i:=1 to 8 do // initialize the bitboard of attacked pieces. - for j:=1 to 8 do - AttackedSquares[i][j]:= false; -//////////////////////////////////////UP LEFT/////////////////////////////////// - y := AFrom.y+1; - x := AFrom.x-1; - if (x>=1) and (y<=8) then begin - if (CurrentPlayerIsWhite) then begin - willBeACapture:= (Board[x][y] in BlackPieces); - validMove:= not (Board[x][y] in WhitePieces); - end - else begin - willBeACapture:=(Board[x][y] in WhitePieces); - validMove:=not (Board[x][y] in BlackPieces) - end; - end - else - y :=0; // if it is in the border of the board, put 0 in l to skip the while below. - haveCaptured:=false; - while ( (x>=1) and (y <= 8) and (validMove) and (not haveCaptured)) do begin - AttackedSquares[x][y] := true; - if (willBeACapture) then - haveCaptured:=true; - y := y+1; - x := x-1; - if (x>=1) and (y<=8) then begin //again to not have an 'out of bounds' error - if (CurrentPlayerIsWhite) then begin - willBeACapture:= (Board[x][y] in BlackPieces); - validMove:= not (Board[x][y] in WhitePieces); - end - else begin - willBeACapture:=(Board[x][y] in WhitePieces); - validMove:=not (Board[x][y] in BlackPieces) - end; + result :=false; + //Up left + if (AFrom.X>ATo.X) and (AFrom.Y=ATo.X+1) and (j<=ATo.Y-1) do + begin + if Board[i][j] <> ctEmpty then Exit; + i := i - 1; + j := j + 1; end; + exit(True); end; -///////////////////////////////////END UP LEFT////////////////////////////////// - -//////////////////////////////////////UP RIGHT////////////////////////////////// - y := AFrom.y+1; - x := AFrom.x+1; - willBeACapture:=false; - if (x<=8) and (y<=8) then begin - if (CurrentPlayerIsWhite) then begin - willBeACapture:= (Board[x][y] in BlackPieces); - validMove:= not (Board[x][y] in WhitePieces); - end - else begin - willBeACapture:=(Board[x][y] in WhitePieces); - validMove:=not (Board[x][y] in BlackPieces) - end; - end - else - y :=0; // if it is in the border of the board, put 0 in l to skip the while below. - haveCaptured:=false; - while ( (x<=8) and (y <= 8) and (validMove) and (not haveCaptured)) do begin - AttackedSquares[x][y] := true; - if (willBeACapture) then - haveCaptured:=true; - y := y+1; - x := x+1; - if (x<=8) and (y<=8) then begin //again to not have an 'out of bounds' error - if (CurrentPlayerIsWhite) then begin - willBeACapture:= (Board[x][y] in BlackPieces); - validMove:= not (Board[x][y] in WhitePieces); - end - else begin - willBeACapture:=(Board[x][y] in WhitePieces); - validMove:=not (Board[x][y] in BlackPieces) - end; + //Up right + if (AFrom.X ctEmpty then Exit; + i := i + 1; + j := j + 1; end; + exit(True); end; -///////////////////////////////////END UP RIGHT///////////////////////////////// -//////////////////////////////////////DOWN LEFT///////////////////////////////// - y := AFrom.y-1; - x := AFrom.x-1; - willBeACapture:=false; - if (x>=1) and (y>=1) then begin - if (CurrentPlayerIsWhite) then begin - willBeACapture:= (Board[x][y] in BlackPieces); - validMove:= not (Board[x][y] in WhitePieces); - end - else begin - willBeACapture:=(Board[x][y] in WhitePieces); - validMove:=not (Board[x][y] in BlackPieces) - end; - end - else - y :=0; // if it is in the border of the board, put 0 in l to skip the while below. - haveCaptured:=false; - while ( (x>=1) and (y >= 1) and (validMove) and (not haveCaptured)) do begin - AttackedSquares[x][y] := true; - if (willBeACapture) then - haveCaptured:=true; - y := y-1; - x := x-1; - if (x>=1) and (y>=1) then begin //again to not have an 'out of bounds' error - if (CurrentPlayerIsWhite) then begin - willBeACapture:= (Board[x][y] in BlackPieces); - validMove:= not (Board[x][y] in WhitePieces); - end - else begin - willBeACapture:=(Board[x][y] in WhitePieces); - validMove:=not (Board[x][y] in BlackPieces) - end; + //Down left + if (AFrom.X>ATo.X) and (AFrom.Y>ATo.Y) and (AFrom.X-ATo.X=AFrom.Y-ATo.Y) then + begin + i := AFrom.X-1; + j := AFrom.Y-1; + while (i>=ATo.X+1) and (j>=ATo.Y+1) do + begin + if Board[i][j] <> ctEmpty then Exit; + i := i - 1; + j := j - 1; end; + exit(True); end; -///////////////////////////////////END DOWN LEFT//////////////////////////////// -//////////////////////////////////////DOWN RIGHT//////////////////////////////// - y := AFrom.y-1; - x := AFrom.x+1; - willBeACapture:=false; - if (x<=8) and (y>=1) then begin - if (CurrentPlayerIsWhite) then begin - willBeACapture:= (Board[x][y] in BlackPieces); - validMove:= not (Board[x][y] in WhitePieces); - end - else begin - willBeACapture:=(Board[x][y] in WhitePieces); - validMove:=not (Board[x][y] in BlackPieces) - end; - end - else - y :=0; // if it is in the border of the board, put 0 in l to skip the while below. - haveCaptured:=false; - while ( (x<=8) and (y >= 1) and (validMove) and (not haveCaptured)) do begin - AttackedSquares[x][y] := true; - if (willBeACapture) then - haveCaptured:=true; - y := y-1; - x := x+1; - if (x<=8) and (y>=1) then begin //again to not have an 'out of bounds' error - if (CurrentPlayerIsWhite) then begin - willBeACapture:= (Board[x][y] in BlackPieces); - validMove:= not (Board[x][y] in WhitePieces); - end - else begin - willBeACapture:=(Board[x][y] in WhitePieces); - validMove:=not (Board[x][y] in BlackPieces) - end; + //Down right + if (AFrom.XATo.Y) and (ATo.X-AFrom.X=AFrom.Y-ATo.Y)then + begin + i := AFrom.X+1; + j := AFrom.Y-1; + while (i<=ATo.X-1) and (j>=ATo.Y+1) do + begin + if Board[i][j] <> ctEmpty then Exit; + i := i + 1; + j := j - 1; end; + exit(True); end; -///////////////////////////////////END DOWN RIGHT/////////////////////////////// - - {for i:=1 to 8 do begin //To show the bitboard - for j:=1 to 8 do - mensagem := mensagem + BoolToStr(AttackedSquares[i][j],'1','0') + ' '; - mensagem := mensagem + #13; - end; - -ShowMessage(mensagem);} - result := (AttackedSquares[Ato.X][Ato.y]); end; function TChessGame.ValidateQueenMove(AFrom, ATo: TPoint): Boolean;