#Include "d:\basic\freebasic\tests\bipipe\bipipe.bi" '#Include Once "windows.bi" Type direction col As Integer row As Integer End Type Type tFen 'for parsed fen castling As String board As String opponent As String enPassant As String halfmoves As String moves As String End Type Dim Shared fenP As tFen Const As String w = "white", b = "black" Dim Shared As direction north, northeast, east, southeast, south, southwest, west, northwest ', _ 'knightpattern(8) Dim Shared As Integer board(12,12), sBoard(12,12) 'internal representation of board Dim Shared As Double gameStamp Dim Shared As String enPassantField 'set direction offsets north.col = 0 north.row = -1 northeast.col = 1 northeast.row = -1 east.col = 1 east.row = 0 southeast.col = 1 southeast.row = 1 south.col = 0 south.row = 1 southwest.col = -1 southwest.row = 1 west.col = -1 west.row = 0 northwest.col = -1 northwest.row = -1 'set knight movement pattern Dim Shared As direction knightpattern(1 To ...) = {(-1,-2),(1,-2),(2,-1),(2,1), _ (1,2),(-1,2),(-2,1),(-2,-1)} Function deleteFromString(text As String, del As String) As String Dim As String g Dim As Integer x, y For x = 1 To Len(text) If InStr(Mid(text,x,1), Any del) = 0 Then g += Mid(text,x,1) EndIf Next Return g End Function Function testForCheck(opp As String) As Integer Dim As Integer x, col, row, drow, dcol, kingrow, kingcol, kingsig 'set Sgn of the king kingsig = IIf(opp = w,1,-1) 'get the internal coordinates of the king For kingrow = 2 To 9 For kingcol = 2 To 9 If board(kingcol,kingrow) = kingsig * 6 Then 'king found Exit For,For 'terminate searching EndIf Next Next 'let the king look around For dcol = -1 To 1 'left -> rest -> right For drow = -1 To 1 'up -> rest -> down col = kingcol 'start at king coordinates row = kingrow Do 'look along the desired direction col += dcol 'next field row += drow If board(col,row) Then 'field not empty If (board(col,row) = 7) Or (Sgn(board(col,row)) = kingsig) Then 'border or piece of own colour Exit Do 'next direction EndIf Select Case Abs(board(col,row)) 'kind of piece Case 1 'pawn If (Abs(kingcol - col) = 1) And ((kingrow - row) = kingsig) Then 'check by pawn Return 100*col + 10*row + 1 Else Exit Do 'next direction EndIf Case 2 'rook If (dcol = 0) Or (drow = 0) Then 'king is looking straight Return 100*col + 10*row + 2 Else Exit Do EndIf Case 3 'knight Exit Do 'next direction Case 4 'bishop If (dcol <> 0) And (drow <> 0) Then 'king is looking diagonal Return 100*col + 10*row + 3 Else Exit Do EndIf Case 5 'queen Return 100*col + 10*row + 4 Case 6 'king If (Abs(kingcol - col) < 2) And (Abs(kingrow - row) < 2) Then Return 100*col + 10*row + 5 Else Exit Do EndIf End Select EndIf Loop Next Next For x = 1 To 8 If board(kingcol + knightpattern(x).col, kingrow + knightpattern(x).row) = kingsig * -3 Then 'knight Return 100*col + 10*row + 6 EndIf Next Return 0 'no check End Function Sub parseFen(fen As String) Dim As Integer ptr1, ptr2 ptr1 = InStr(fen," ") fenP.Board = Left(fen,ptr1 - 1) ptr2 = InStr(ptr1 + 1,fen," ") fenP.Opponent = Mid(fen,ptr1 + 1,ptr2 - ptr1 - 1) ptr1 = InStr(ptr2 + 1,fen," ") fenP.Castling = Mid(fen,ptr2 + 1,ptr1 - ptr2 - 1) ptr2 = InStr(ptr1 + 1,fen," ") fenP.EnPassant = Mid(fen,ptr1 + 1,ptr2 - ptr1 - 1) ptr1 = InStr(ptr2 + 1,fen," ") fenP.Halfmoves = Mid(fen,ptr2 + 1,ptr1 - ptr2 - 1) fenP.Moves = Mid(fen,ptr1 + 1) End Sub Sub restoreBoard Dim As Integer col, row CopyMemory(@board(0,0),@sBoard(0,0),13*13*SizeOf(Integer)) 'For row = 0 To 12 ' For col = 0 To 12 ' Board(col,row) = sBoard(col,row) ' Next 'Next End Sub Sub saveBoard Dim As Integer col, row 'dim as any ptr bp '= @board(lbound(board)), sbp = @sBoard(LBound(sBoard)) CopyMemory(@sBoard(0,0),@board(0,0),13*13*SizeOf(Integer)) 'For row = 0 To 12 ' For col = 0 To 12 ' sBoard(col,row) = board(col,row) ' Next 'Next End Sub