Code: Select all
'===========================================================================
' Pseudo Winboard chess program written by Thomas McBurney August 2005
'
'The following code is only an example of how to communicate with
'Winboard using a pipe.
'===========================================================================
' Declare my functions and subs. FreeBASIC gets upset if I don't do it.
declare function GetCommand() as string
declare function SendCommand(byval WBCmd as string) as integer
declare sub Main
' Declare the API functions we will be using for communicating to Winboard
dim KernelLib as integer
dim shared WriteFile as function (BYVAL hFile AS integer, lpBuffer AS ANY, BYVAL nNumberOfBytesToWrite AS integer, lpNumberOfBytesWritten AS integer, lpOverlapped AS ANY) AS integer
dim shared GetStdHandle as function (BYVAL nStdHandle AS integer) AS integer
dim shared ReadFile as function (BYVAL hFile AS integer, lpBuffer AS ANY, BYVAL nNumberOfBytesToRead AS integer, lpNumberOfBytesRead AS integer, lpOverlapped AS ANY) AS integer
dim shared PeekNamedPipe as function (BYVAL hNamedPipe AS integer, lpBuffer AS ANY, BYVAL nBufferSize AS integer, lpBytesRead AS integer, lpTotalBytesAvail AS integer, lpBytesLeftThisMessage AS integer) AS integer
' Link KernelLib to kernel32.dll
KernelLib = dylibload( "KERNEL32" )
' Link the functions to the appropriate Kernel32 library.
WriteFile = dylibsymbol( KernelLib, "WriteFile" )
GetStdHandle = dylibsymbol( KernelLib, "GetStdHandle" )
ReadFile = dylibsymbol( KernelLib, "ReadFile" )
PeekNamedPipe = dylibsymbol( KernelLib, "PeekNamedPipe" )
DIM STD_OUTPUT_HANDLE AS integer
DIM STD_INPUT_HANDLE AS integer
DIM shared OutputHandle AS integer
DIM shared InputHandle AS integer
STD_OUTPUT_HANDLE = -11&
STD_INPUT_HANDLE = -10&
OutputHandle = GetStdHandle(STD_OUTPUT_HANDLE)
InputHandle = GetStdHandle(STD_INPUT_HANDLE)
call Main
sub Main
dim sWinboardCmd as string
dim sTemp as string
DO
sWinboardCmd=GetCommand ' Get command from Winboard
if sWinboardCmd<>"" then
IF sWinboardCmd="protover 2" THEN
sTemp="feature myname="+CHR$(34)+"Pseudo chess program written in FreeBASIC"+CHR$(34)
SendCommand(sTemp)
SendCommand("feature done=1")
END IF
IF sWinboardCmd="quit" THEN EXIT DO ' this will exit the program
IF sWinboardCmd="e2e4" THEN SendCommand("move e7e5")
IF sWinboardCmd="g1f3" THEN SendCommand("move b8c6")
END IF
sleep 10
LOOP
END SUB
function GetCommand() as string
'This function will retrieve one command at a time from winboard.
DIM sBuff AS STRING
DIM iBytesRead AS integer
DIM iTotalBytes AS integer
DIM iAvailBytes AS integer
DIM iReturnCode AS integer
DIM sTemp as string
GetCommand=""
' Null character will make sBuff a C style empty string
sBuff = CHR$(0)
' PeekNamedPipe tells us if there is any data waiting to be retrieved.
iReturnCode = PeekNamedPipe(InputHandle, BYVAL STRPTR(sBuff), 1, iBytesRead, iTotalBytes, iAvailBytes)
' return empty string when there is no data to be retrieved.
IF iTotalBytes=0 THEN EXIT function
' Retrieve data
DO
iReturnCode = ReadFile(InputHandle, BYVAL STRPTR(sBuff), 1, iBytesRead, BYVAL 0&)
IF ASC(sBuff)=10 THEN EXIT DO ' exit on line feed
sTemp=sTemp+sBuff
LOOP
GetCommand=sTemp
END function
Function SendCommand(Byval sWBCmd As String) As integer
Dim iBytesWritten As integer
Dim iBytes As integer
Dim iReturnCode As integer
sWBCmd = sWBCmd + chr$(10) ' add line feed to end of string
iBytes = Len(sWBCmd)
iReturnCode = WriteFile(OutputHandle, ByVal sWBCmd, iBytes, lBytesWritten, ByVal 0&)
SendCommand = 0
End Function