fb:porticula NoPaste
chess_gui_engine_vs_engine_0.4
Uploader: | grindstone |
Datum/Zeit: | 21.11.2013 10:33:34 |
#Include Once "windows.bi"
#Include "fbgfx.bi"
Type chessMove
x1 As Integer
x2 As Integer
y1 As Integer
y2 As Integer
t1 As Integer
t2 As Integer
move As String
End Type
Type wText
opponent As String
move As String
depth As String
End Type
Const As Integer castlingWhite = 1, castlingBlack = 2
Const As String w = "white", b = "black"
Dim Shared As Integer mx, my, btn, ox, oy, dx, dy, depth, moveTime, errorCount, totalErrors, _
moveCount, relSpeed, animationSpeed, ExitCode
Dim Shared As Integer board(12,12) 'internal representation of board
Dim Shared As Integer castlingFlagWhite, castlingFlagBlack, mateFlag, errorFlag
Dim Shared As Integer bx, by 'position top/left of board
Dim Shared As wText wt
Dim Shared As FB.Image Ptr sImage
Dim Shared As FB.Image Ptr iPiece(12) '12 images
ReDim Shared As String matesign(1) '
Dim Shared As String hod, currDepth, engine1, engine2, engineWhite, engineBlack, nameWhite, _
nameBlack, WindowText, move, opponent, playmode, lastmove
Dim Shared As HANDLE hReadChildPipeWhite, hWriteChildPipeWhite, hReadChildPipeBlack, _
hWriteChildPipeBlack, hReadPipeWhite, hWritePipeWhite, hReadPipeBlack, _
hWritePipeBlack, hThisWindow, hProcessHandleWhite, hProcessHandleBlack
Declare Function GetEngineResponse(opponent As String) As String
Declare Function GetMove(opponent As String) As String
Declare Sub WriteEngineInfo(opponent As String, s As String)
Declare Sub update()
Declare Function strMoveToNumMove(move As String) As chessMove
Declare Sub makeMove(cMove As chessMove)
Declare Sub SetTitleBar
Declare Sub CloseEngines
Dim As Double timerem
Dim As Integer x, y, winEngine1, winEngine2, drawnGames
Dim As String datum, g, matecomp, movecomp, winner
Dim As chessMove cMove
Dim As STARTUPINFO siWhite, siBlack
Dim As PROCESS_INFORMATION pi
Dim As SECURITY_ATTRIBUTES sa
Dim Shared As Integer gameCount
'***************************************************************
'for testing. in the final version these variables will be
' set up with an .ini - file
engine1 = "stockfish_4_32bit.exe"
'engine2 = "stockfish_4_32bit.exe"
'engine1 = "Fruit-2-3-1.exe"
engine2 = "Fruit-2-3-1.exe"
depth = 10
moveTime = 100
'playmode = "depth"
playmode = "movetime"
ReDim matesign(2)
matesign(1) = "a1a1"
matesign(2) = "(none)"
animationSpeed = 20
'***************************************************************
ScreenRes 640,480,32
'sImage = ImageCreate( 42, 42, 0 ) 'save screen data
hThisWindow = GetForegroundWindow() 'get the window handle
'generate a value to make th delay loop independent from the system speed
timerem = Timer
Do
relSpeed += 1
x = Log(Timer)
Loop Until Timer > timerem + .1
relSpeed = relSpeed / 100 'representing about 1ms
winEngine1 = 0
winEngine2 = 0
drawnGames = 0
moveTime -= 100
Do 'turnament loop
Open ExePath + "\responselog.txt" For Output As #3
Close 3
totalErrors = 0
gameCount += 1 'game counter
If gameCount And 1 Then 'every odd game number
engineWhite = engine1
engineBlack = engine2
moveTime += 100
Else 'every even game number
engineWhite = engine2
engineBlack = engine1
EndIf
'install the pipes to the two engines
sa.nLength = SizeOf(SECURITY_ATTRIBUTES)
sa.lpSecurityDescriptor = NULL
sa.bInheritHandle = TRUE
'start and connect white engine
CreatePipe(@hReadChildPipeWhite,@hWritePipeWhite,@sa,0)
SetHandleInformation(hWritePipeWhite,HANDLE_FLAG_INHERIT,0)
CreatePipe(@hReadPipeWhite,@hWriteChildPipeWhite,@sa,0)
SetHandleInformation(hReadPipeWhite,HANDLE_FLAG_INHERIT,0)
GetStartupInfo(@siWhite)
siWhite.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
siWhite.wShowWindow = SW_SHOW
siWhite.hStdOutput = hWriteChildPipeWhite
siWhite.hStdError = hWriteChildPipeWhite
siWhite.hStdInput = hReadChildPipeWhite
CreateProcess(0,engineWhite,0,0,TRUE,0,0,0,@siWhite,@pi)
hProcessHandleWhite = pi.hProcess
'start and connect black engine
CreatePipe(@hReadChildPipeBlack,@hWritePipeBlack,@sa,0)
SetHandleInformation(hWritePipeBlack,HANDLE_FLAG_INHERIT,0)
CreatePipe(@hReadPipeBlack,@hWriteChildPipeBlack,@sa,0)
SetHandleInformation(hReadPipeBlack,HANDLE_FLAG_INHERIT,0)
GetStartupInfo(@siBlack)
siBlack.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
siBlack.wShowWindow = SW_SHOW
siBlack.hStdOutput = hWriteChildPipeBlack
siBlack.hStdError = hWriteChildPipeBlack
siBlack.hStdInput = hReadChildPipeBlack
CreateProcess(0,engineBlack,0,0,TRUE,0,0,0,@siBlack,@pi)
hProcessHandleBlack = pi.hProcess
CloseHandle(hWriteChildPipeWhite)
CloseHandle(hReadChildPipeWhite)
CloseHandle(hWriteChildPipeBlack)
CloseHandle(hReadChildPipeBlack)
'ScreenRes 640,480,32
sImage = ImageCreate( 42, 42, 0 ) 'save screen data
'THIS DETERMINES WHERE TO PLACE BOARD IN WINDOW
bx = 130
by = 30
'read board
Restore boardLayout
For j As Integer = 0 To 11
For i As Integer = 0 To 11
Read board(i,j)
Next i
Next j
'copy images from data statements to screen
Restore Pieces
For i As Integer = 0 To 5 'for each piece
For y As Integer = 0 To 41 'for each row
Read datum
For x As Integer = 0 To 41 'for each column
If Mid(datum,x+1,1)="." Then
PSet(x+i*42,y),RGB(255,0,255) 'transparent color
PSet(x+i*42+252,y),RGB(255,0,255)
End If
If Mid(datum,x+1,1)="#" Then
PSet(x+i*42,y),RGB(0,0,0)
PSet(x+i*42+252,y),RGB(0,0,0)
End If
If Mid(datum,x+1,1)="*" Then
PSet(x+i*42,y),RGB(120,120,180)
PSet(x+i*42+252,y),RGB(250,200,200)
End If
Next x
Next y
Next i
'sleep
'allocate memory for 12 images
For i As Integer = 0 To 11
iPiece(i) = ImageCreate(42,42,0)
Next i
'get set of 12 images
For i As Integer = 0 To 11
Get (i*42,0)-(i*42+41,41), iPiece(i)
Next i
update()
move = ""
wt.move = "xxxx"
opponent = b '(!)
hod = "position startpos moves"
'50 züge
'hod = "position startpos moves b1c3 d7d5 d2d4 g8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1e1 e5d5 e1d1 d5e6 b4b3 f5f6 a3a2 f6f7 d1e1 e6d5 e1f1 a7b7 b3c3 b7a7 c3b2 a7b7 b2c2 b7a7 a2a1q a7a1 f1f7 a1e1 c2b2 e1g1 b2b3 g1a1 f7f3 a1b1 b3c2 b1a1 c2b2 a1d1 f3c3 d1e1 b2b3 e1a1 b3b2 a1d1 b2b3 d1a1"
'hod = " position startpos moves e2e4 g8f6 e4e5 f6d5 c2c4 d5b4 d2d4 b8c6 a2a3 b4a6 b2b4 a6b8 b1c3 d7d6 g1f3 c8g4 b4b5 g4f3 g2f3 c6a5 c1g5 h7h6 g5e3 a7a6 f1e2 a6b5 c3b5 b8d7 f3f4 c7c6 b5c3 d7b6 c4c5 d6c5 d4c5 b6d5 d1c2 d5e3 f2e3 e7e6 c3e4 d8h4 e1d1 g7g5 e4f6 e8e7 f4g5 f8g7 h1f1 h6g5 f6g4 h8d8 d1c1 h4h8 c2c3 d8d5 g4f6 g7f6 e5f6 e7e8 c1b1 h8h2 a1a2 h2h7 a2c2 e8f8 e3e4 h7e4 e2f3 e4f5 f1h1 f8g8 f3d5 c6d5 c3h3 f5f6 h3h7 g8f8 h7h8 f8e7 h8a8 f6f3 h1h8 f3b3 b1c1 b3e3 c1d1 e3g1 d1e2 g1g2 e2e1 g2g1 e1d2 g1f2 d2c3 f2e3 c3b2 e3b3 b2a1 b3a3 a1b1 a3b4 b1c1 b4e1 c1b2 e1b4 b2a1 *b4a3
'*"
castlingFlagWhite = 1 'set white castling flag
castlingFlagBlack = 1 'set black castling flag
mateFlag = 0
'totalErrors = 0
moveCount = 0
Open ExePath + "\gamelog.txt" For Output As #1 'open the protocol file
Print #1, "spiel";gameCount
Locate 52,30
Print "Wait loading engines!"
WriteEngineInfo(w,"uci")
WriteEngineInfo(b,"uci")
Do
g = GetEngineResponse(w)
Loop Until InStr(g,"uciok")
x = InStr(g,"id name ") + 8
nameWhite = Mid(g, x, InStr(x,g,Chr(13)) - x) 'get the name of the white engine
Do
g = GetEngineResponse(b)
Loop Until InStr(g,"uciok")
x = InStr(g,"id name ") + 8
nameBlack = Mid(g, x, InStr(x,g,Chr(13)) - x) 'get the name of the black engine
'write to protocol file
Print #1, "white ";nameWhite;" ";engineWhite
Print #1, "black ";nameBlack;" ";engineBlack
Print #1, "mode ";playmode;
Select Case playmode
Case "depth"
Print #1, depth
Case "movetime"
Print #1, moveTime
End Select
Print #1, ""
Open ExePath + "\turnamentlog.txt" For Append As #2
Print #2, ""
Print #2, "game";gameCount
Print #2, "white ";nameWhite;" ";engineWhite
Print #2, "black ";nameBlack;" ";engineBlack
Print #2, "mode ";playmode;
Select Case playmode
Case "depth"
Print #2, depth
Case "movetime"
Print #2, moveTime
End Select
Close 2
Locate 52,30
Print "Calculate position..."
'*************** MAIN LOOP ************************
Do
'switch engine
If opponent = w Then
opponent = b 'black
Else
opponent = w 'white
moveCount += 1
EndIf
Print #1, Time;" ";moveCount;" ";opponent;" ";hod 'write to protocol file
Print #1, ""
errorCount = 0
Do 'calculate next move
WriteEngineInfo(opponent,hod) 'send the actual board setup to the engine
Select Case playmode
Case "depth"
WriteEngineInfo(opponent,"go depth " + Str(depth)) 'depth mode
Case "movetime"
'If ((opponent = w) And InStr(namewhite,"ruit")) Or ((opponent = b) And InStr(nameBlack,"ruit")) Then
' WriteEngineInfo(opponent,"go movetime " + Str(moveTime)) 'movetime mode
'Else
' WriteEngineInfo(opponent,"go movetime " + Str(10)) 'movetime mode
'EndIf
WriteEngineInfo(opponent,"go movetime " + Str(moveTime)) 'movetime mode
End Select
move = GetMove(opponent) 'read the "bestmove" from the engine
lastmove = move
'check if the response is a correct move
Do
If move = "mate" Then
Locate 53,19
Print " << CHECK MATE >> "
'write to protocol file
Print #1, " << CHECK MATE >> "
If Len (move) Then 'zug ist matt
Else 'letzter zug war matt --> tauschen
If opponent = w Then
opponent = b
Else
opponent = w
EndIf
EndIf
Select Case opponent
Case w
Print #1, "black (";nameBlack;
If engineBlack = engine1 Then
winEngine1 += 1
Else
winEngine2 += 1
EndIf
Case b
Print #1, "white (";nameWhite;
If engineWhite = engine1 Then
winEngine1 += 1
Else
winEngine2 += 1
EndIf
End Select
Print #1, ") wins in"; moveCount; " moves"
Print #1, "total response errors ";totalErrors
Open ExePath + "\turnamentlog.txt" For Append As #2
Print #2, "check mate ";
Select Case opponent
Case w
Print #2, nameBlack;"wins as black ";
Case b
Print #2, nameWhite;"wins as white ";
End Select
Print #2, "in"; moveCount; " moves"
Print #2, Mid(hod,25) 'moves
Print #2, "*";lastmove;"*"
Print #2, "total response errors ";totalErrors
Print #2, "total wins ";engine1;winEngine1
Print #2, "total wins ";engine2;winEngine2
Print #2, "total drawn ";drawnGames
Close
Sleep 100000
Exit Do,Do,Do
EndIf
errorFlag = 1
Select Case Len(move)
Case 4,5
If InStr(Mid(move,1,1),Any "abcdefgh") = 0 Then '1st character
Exit Do 'error, don't reset the errorflag
EndIf
If InStr(Mid(move,2,1),Any "12345678") = 0 Then '2nd character
Exit Do
EndIf
If InStr(Mid(move,3,1),Any "abcdefgh") = 0 Then '3rd character
Exit Do
EndIf
If InStr(Mid(move,4,1),Any "12345678") = 0 Then '4th character
Exit Do
EndIf
If (Len(move) = 5) And (InStr(Mid(move,5,1),Any "qrbk") = 0) Then '5th character (if existing)
Exit Do
EndIf
Case Else 'wrong length
Exit Do
End Select
errorFlag = 0 'no error, reset flag
Loop Until 1 'always leave the loop. Workaround to avoid "Goto"-statement
If errorflag Then
errorCount += 1
If errorCount > 9 Then
Locate 52,30
Print #1, move
Print #1, "game aborted due to response error caused by ";
Select Case opponent
Case w
Print #1, nameWhite
Case b
Print #1, nameBlack
End Select
Open ExePath + "\turnamentlog.txt" For Append As #2
Print #2, hod;" *";move;"*"
Print #2, "game aborted due to response error caused by ";
Select Case opponent
Case w
Print #2, nameWhite
Case b
Print #2, nameBlack
End Select
Close 2
Exit Do,Do 'terminate current game
Else
Print #1, ""
Print #1,"response error";errorCount;" ";opponent;" ";move
Print #1, ""
Locate 52,30
Print "response error";errorCount;", try again"
? "move *";move;"*"
sleep
EndIf
Else
Exit Do 'correct move, leave the loop
EndIf
Loop 'response error, try again
totalErrors += errorCount
hod += " " + move 'add the last move to the hod-string
cMove.move = move 'for pawn promotion
wt.move = move 'for title bar
SetTitleBar 'set screenwindow title bar
Locate 52,30
'show animated move
cMove = strMoveToNumMove(move)
makeMove(cMove)
'castling
If (move = "e1g1") And castlingFlagWhite Then
cMove = strMoveToNumMove("h1f1")
makeMove(cMove)
castlingFlagWhite = 0 'reset white castling flag
ElseIf (move = "e1c1") And castlingFlagWhite Then
cMove = strMoveToNumMove("a1d1")
makeMove(cMove)
castlingFlagWhite = 0 'reset white castling flag
ElseIf (move = "e8g8") And castlingFlagBlack Then
cMove = strMoveToNumMove("h8f8")
makeMove(cMove)
castlingFlagBlack = 0 'reset black castling flag
ElseIf (move = "e8c8") And castlingFlagBlack Then
cMove = strMoveToNumMove("a8d8")
makeMove(cMove)
castlingFlagBlack = 0 'reset black castling flag
EndIf
update()
Locate 52,30
Print move
Loop Until InKey = Chr(27) 'Esc
CloseEngines
Close
Loop 'next game
Print #1, Time;" game aborted" 'write to protocol file
Close
End
Sub update()
Dim As Integer x
ScreenLock()
'drawBoard
Color RGB(0,0,0),RGB(100,255,100)
Cls
Dim As Integer shade
shade = 1
For y As Integer = 0 To 7
shade = -shade
For x As Integer = 0 To 7
If shade = -1 Then
Line (bx+x*42,by+y*42)-(bx+x*42+41,by+y*42+41),RGB(252,206,156),bf
Else
Line (bx+x*42,by+y*42)-(bx+x*42+41,by+y*42+41),RGB(179,110,44),bf
End If
shade = -shade
Next x
Next y
'border of board
Line (bx-1,by-1)-(bx+336,by+336),RGB(0,0,0),b
'now draw image on square
For y As Integer = 0 To 7 'column
For x As Integer = 0 To 7 'line
'put image onto square
If board(x+2,y+2) <> 7 And board(x+2,y+2) <> 0 Then
If board(x+2,y+2) < 0 Then 'black
Put (bx+x*42,by+y*42),iPiece(Abs(board(x+2,y+2))-1),Trans
Else 'white
Put (bx+x*42,by+y*42),iPiece(Abs(board(x+2,y+2))+5),Trans
End If
End If
Next x
Next y
'print engine names on screen
Locate 2,20
Print nameBlack
Locate 49,20
Print nameWhite
For x = 1 To 8
Locate 48 - 5.15 * x,14
Print x
Next
Locate 47,20
Print "A B C D E F G H"
Locate 52,20
Print "Move";moveCount
Locate 53,20
Print "Game";gameCount
ScreenUnlock()
End Sub
Sub makeMove(cMove As chessMove)
Dim As Integer choice, i, x 'number of selected piece
Dim As Integer px1, py1, px2, py2, dx, dy 'moving data
Locate 1,1
cMove.y1 = 8 - cMove.y1 'set correct line of internal board
cMove.y2 = 8 - cMove.y2
px1 = cMove.x1*42+bx 'screen pointer to chosen image
py1 = cMove.y1*42+by
px2 = cMove.x2*42+bx
py2 = cMove.y2*42+by
ox = px1
oy = py1
'make move on internal board
choice = board(cMove.x1+2,cMove.y1+2)
'pawn promotion
Select Case Mid(cMove.move,5,1)
Case "q"
choice = Sgn(choice) * 5 'promote to queen
Case "b"
choice = Sgn(choice) * 4 'promote to bishop
Case "n"
choice = Sgn(choice) * 3 'promote to knight
Case "r"
choice = Sgn(choice) * 2 'promote to rook
End Select
board(cMove.x2+2,cMove.y2+2) = choice
board(cMove.x1+2,cMove.y1+2)=0 'erase data
'convert to image ID of that number
If choice < 0 Then
choice = Abs(choice) - 1
Else
choice = choice + 5
End If
Line (px1,py1)-(px1+41,py1+41),Point(px1+2,py1+2),bf 'clear
For i = 0 To 3
Line (px1+i,py1+i)-(px1+41-i,py1+41-i),RGB(0,0,244),b 'source
Line (px2+i,py2+i)-(px2+41-i,py2+41-i),RGB(0,255,0),b 'destination
Next i
'compute direction of movement
If px1 > px2 Then
dx = -1
Else
dx = 1
End If
If py1 > py2 Then
dy = -1
Else
dy = 1
End If
Get (px1,py1)-(px1+41,py1+41),sImage
Put (px1,py1),iPiece(choice),Trans
ox = px1
oy = py1
While px1 <> px2 Or py1 <> py2
'update coordinates
'Sleep 4
If px1 <> px2 Then
px1 = px1 + dx
End If
If py1 <> py2 Then
py1 = py1 + dy
End If
'Sleep 4
Put (ox,oy),sImage,PSet 'restore old back ground
Get (px1,py1)-(px1+41,py1+41),sImage 'save new back ground
Put (px1,py1),iPiece(choice),Trans 'place on new back ground
ox = px1
oy = py1
'Sleep 4
If animationSpeed Then 'waste some time
For i = 1 To animationSpeed * relSpeed
x = Log(Timer)
Next
EndIf
Wend
End Sub
'convert string chess moves to numbers
Function strMoveToNumMove(move As String) As chessMove
Dim As chessMove cm
Dim As String umove
cm.move = move
umove = UCase(move)
cm.x1 = Asc(Mid(umove,1,1))-65
cm.y1 = Val(Mid(umove,2,1))
cm.x2 = Asc(Mid(umove,3,1))-65
cm.y2 = Val(Mid(umove,4,1))
Return cm
End Function
'convert chess move numbers to string format
Function numMovetoStrMove(cm As chessMove) As String
Return Chr(cm.x1+65)+Str(cm.y1)+Chr(cm.x2+65)+Str(cm.y2)
End Function
Function GetMove(opp As String) As String
Dim As String g, sRet
Dim As Integer x
'REPLAY *************
Static As Integer anfang, ende
Dim As String replaylist
GoTo noreplay
'replaylist = "e2e4 g8f6 e4e5 f6d5 c2c4 d5b4 d2d4 d7d6 d1a4 b8c6 a2a3 b4a6 g1f3 c8d7 a4b3 d6e5 d4e5 b7b6 b3c2 g7g6 b2b4 f8g7 c2c3 d7g4 b1d2 a6b8 b4b5 c6a5 f1d3 b8d7 d3e4 a8c8 h2h3 g4e6 e4d5 e6d5 c4d5 d7c5 d2c4 d8d5 c4a5 b6a5 e1g1 e8g8 c1e3 c5d3 a1d1 d3e5 f3e5 g7e5 c3a5 d5e4 f1e1 f8d8 d1d8 c8d8 e3c5 e4c2 c5e7 d8d1 e7b4 e5d4 e1d1 c2d1 b4e1 d1e2 a3a4 e2e4 a5b4 e4h4 g1f1 h4f4 b4d2 f4e5 f2f3 g8g7 d2d3 e5c5 e1d2 g7g8 d2h6 c5d6 d3e4 d6d8 f1e2 d4b6 g2g3 b6c5 h6e3 c5b6 e4e5 f7f6 e5e6 g8g7 h3h4 h7h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b1c3 d7d5 g1f3 d5d4 "
g = GetEngineResponse(opp) 'request a message from the engine
anfang = ende
ende = InStr(anfang + 1,replaylist," ")
If ende = 0 Then
ende = anfang
EndIf
sRet = Mid(replaylist,anfang + 1,ende - anfang - 1)
Locate 1,1
? "*";sRet;"*";anfang;ende
'sleep
Return sRet
'? "*";sRet;"*";anfang;ende
'Sleep
'GoTo s
'End
'END REPLAY *************
noreplay:
wt.opponent = opp 'for title bar
g = ""
Do
g += GetEngineResponse(opp) 'request a message from the engine
'currDepth = ""
'print opponent and current depth to the screen
x = InStrRev(g,"info depth ")
currDepth = Mid(g,x+11,2)
Locate 55, 20
Print UCase(opp)
Locate 56, 20
Print "Current depth ";currDepth
wt.depth = currDepth 'for title bar
SetTitleBar 'set title bar
Sleep 1
x = InStrRev(g,"bestmove")
Loop Until x 'the engine has finished the calculating for this move
sRet = Mid(g,x + 9,InStr(x + 9,g," ") - (x + 9)) 'isolate the data of the move
For x = 1 To UBound(matesign)
If sRet = matesign(x) Then
Return "mate"
EndIf
Next
Select Case Mid(sRet,5,1)
Case "q","b","n","r" 'pawn promotion
sRet = Left(sRet,5) 'clip string to 5 characters
Case Else
sRet = Left(sRet,4) 'clip string to 4 characters
End Select
Return sRet 'send the move to the gui
End Function
Function GetEngineResponse(opp As String) As String
Dim As Integer iTotalBytesAvail, iNumberOfBytesWritten, iBytesToRead
Dim As String sRet = "", sBuf
Const As Integer MaxBytesToRead = 4096 'maximum number of bytes to be returned at one 'ReadFile'-operation
Do
sBuf = "" 'clear buffer
'look if there's any data in the pipe
Select Case opp 'choose engine
Case "white"
PeekNamedPipe(hReadPipeWhite,NULL,NULL,NULL,@iTotalBytesAvail,NULL)
Case "black"
PeekNamedPipe(hReadPipeBlack,NULL,NULL,NULL,@iTotalBytesAvail,NULL)
End Select
If iTotalBytesAvail Then 'pipe is not empty
If iTotalBytesAvail < MaxBytesToRead Then
iBytesToRead = iTotalBytesAvail 'set all available bytes to be read
Else
iBytesToRead = MaxBytesToRead 'set the first 4096 bytes to be read
EndIf
sBuf = String(iBytesToRead,Chr(0)) 'set the length of the buffer string to the necessary value
'read the specified amount of bytes from the pipe
Select Case opp 'choose engine
Case "white"
ReadFile(hReadPipeWhite,StrPtr(sBuf),iBytesToRead,@iNumberOfBytesWritten,NULL)
Case "black"
ReadFile(hReadPipeBlack,StrPtr(sBuf),iBytesToRead,@iNumberOfBytesWritten,NULL)
End Select
sRet += sBuf 'add the buffer to the return string
Sleep 1
Else 'pipe is empty
Exit Do 'return
EndIf
Loop
Open ExePath + "\responselog.txt" For Append As #3
Print #3, "OPPONENT: ";opp
Print #3, sRet;"*"
Print #3, ""
Close 3
Return sRet
End Function
Sub WriteEngineInfo(opp As String, s As String)
Dim As Integer iNumberOfBytesWritten
Dim As String sBuf
sBuf = s + Chr(10)
'send the command string to the engine
Select Case opp 'choose engine
Case "white"
WriteFile(hWritePipeWhite,StrPtr(sBuf),Len(sBuf),@iNumberOfBytesWritten,NULL)
Case "black"
WriteFile(hWritePipeBlack,StrPtr(sBuf),Len(sBuf),@iNumberOfBytesWritten,NULL)
End Select
End Sub
Sub SetTitleBar
Dim As String text
'set the text of the screen window
text = Left(UCase(wt.opponent),1) + " " + wt.move + " " + wt.depth
SetWindowText(hThisWindow,StrPtr(text))
Sleep 1
End Sub
Sub CloseEngines
TerminateProcess(hProcessHandleWhite, @ExitCode)
TerminateProcess(hProcessHandleBlack, @ExitCode)
End Sub
'***************************************************
' 1 = pawn, 2 = rook, 3 = knight, 4 = bishop, 5 = queen, 6 = king, 7 = border
' black pieces given negative value, sgn() returns -1 for black and +1 for white
' 0 1 2 3 4 5 6 7 8 9 10 11 <--- internal coordinates
' A B C D E F G H <--- display coordinates
boardLayout:
Data 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 '0
Data 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 '1
Data 7, 7,-2,-3,-4,-5,-6,-4,-3,-2, 7, 7 '2 8
Data 7, 7,-1,-1,-1,-1,-1,-1,-1,-1, 7, 7 '3 7
Data 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7 '4 6
Data 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7 '5 5
Data 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7 '6 4
Data 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7 '7 3
Data 7, 7, 1, 1, 1, 1, 1, 1, 1, 1, 7, 7 '8 2
Data 7, 7, 2, 3, 4, 5, 6, 4, 3, 2, 7, 7 '9 1
Data 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 '10
Data 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 '11
Pieces:
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data "....................####.................."
Data "...................#****#................."
Data "..................#******#................"
Data ".................#********#..............."
Data ".................#********#..............."
Data ".................#********#..............."
Data ".................#********#..............."
Data "..................#******#................"
Data "...................#****#................."
Data "...................######................."
Data "..................#******#................"
Data "................##********##.............."
Data "...............#************#............."
Data "..............#**************#............"
Data "..............################............"
Data ".................#********#..............."
Data ".................#********#..............."
Data ".................#********#..............."
Data "................#**********#.............."
Data "................#**********#.............."
Data "...............#************#............."
Data "..............#**************#............"
Data ".............#****************#..........."
Data "............#******************#.........."
Data "............#******************#.........."
Data "............####################.........."
Data "...........#********************#........."
Data "...........#********************#........."
Data "...........######################........."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data "...........#####..######..#####..........."
Data "...........#***#..#****#..#***#..........."
Data "...........#***#..#****#..#***#..........."
Data "...........#***#..#****#..#***#..........."
Data "...........#***####****####***#..........."
Data "...........#******************#..........."
Data "...........#******************#..........."
Data "...........#******************#..........."
Data "...........#******************#..........."
Data "...........####################..........."
Data "............#****************#............"
Data ".............################............."
Data ".............#****#****#****#............."
Data ".............#****#****#****#............."
Data ".............################............."
Data ".............#**#****#****#*#............."
Data ".............#**#****#****#*#............."
Data ".............################............."
Data ".............#****#****#****#............."
Data ".............#****#****#****#............."
Data ".............################............."
Data ".............#**#****#****#*#............."
Data ".............#**#****#****#*#............."
Data "............##################............"
Data "...........#******************#..........."
Data "..........#********************#.........."
Data "..........#********************#.........."
Data ".........#**********************#........."
Data ".........########################........."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data "....................#....................."
Data "...................##....................."
Data "..................#**#...................."
Data "..................#**###.................."
Data ".................#******###..............."
Data "................#**********#.............."
Data "................#***********#............."
Data "...............#**#*********#............."
Data "..............#**##**********#............"
Data ".............#**##.#*********#............"
Data ".............#**###***********#..........."
Data "............#*****************#..........."
Data "............#*****************#..........."
Data "...........#******************#..........."
Data "...........#*******************#.........."
Data "..........#******#####*********#.........."
Data "..........#******#..#**********#.........."
Data ".........#*******#..#**********#.........."
Data ".........#******#..#***********#.........."
Data "..........#****#..#***********#..........."
Data "..........#####..#************#..........."
Data "................#*************#..........."
Data "...............#*************#............"
Data "...............#*************#............"
Data "..............#***************#..........."
Data "..............#***************#..........."
Data ".............#*****************#.........."
Data ".............#******************#........."
Data "............#*******************#........."
Data "............######################........"
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".....................##..................."
Data "....................#**#.................."
Data "...................#****#................."
Data "..................#******#................"
Data ".................#********#..............."
Data "................#**********#.............."
Data "...............#************#............."
Data "...............#************#............."
Data "..............##************##............"
Data "..............#**************#............"
Data "..............#**************#............"
Data "..............#******##******#............"
Data ".............#*******##*******#..........."
Data ".............#*******##*******#..........."
Data ".............#*******##*******#..........."
Data ".............#****########****#..........."
Data ".............#****########****#..........."
Data ".............#*******##*******#..........."
Data ".............#*******##*******#..........."
Data ".............#*******##*******#..........."
Data ".............#*******##*******#..........."
Data ".............#*******##*******#..........."
Data "..............#******##******#............"
Data "..............#******##******#............"
Data "...............#************#............."
Data "...............#************#............."
Data "...............##############............."
Data "...............##**********##............."
Data "................############.............."
Data "..............##************##............"
Data "............##*****######*****##.........."
Data "...........#*****##......##*****#........."
Data "...........######..........######........."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".....................#...................."
Data "....................#*#..................."
Data "...................#***#.................."
Data "..................#*****#................."
Data "..................#*****#................."
Data "..................#*****#................."
Data "............#.....#*****#.....#..........."
Data "...........#*#.....#***#.....#*#.........."
Data "..........#***#....#***#....#***#........."
Data "..........#***#....#***#....#***#........."
Data "...........#*#.....#***#.....#*#.........."
Data "...........#*#.....#***#.....#*#.........."
Data "..........#***#...#*****#...#***#........."
Data "..........#***#..#*******#..#***#........."
Data "..........#***#..#*******#..#***#........."
Data ".....##...#***#...#*****#...#***#...##...."
Data ".....#*#..#***#...#*****#...#***#..#*#...."
Data ".....#**#.#***#...#*****#...#***#.#**#...."
Data ".....#**#.#***#...#*****#...#***#.#**#...."
Data "......#*#.#***#...#*****#...#***#.#*#....."
Data "......#**##***#...#*****#...#***##**#....."
Data "......#**#*****#.#*******#.#*****#**#....."
Data ".......#*******##*********##*******#......"
Data ".......#***************************#......"
Data "........#*************************#......."
Data "........###########################......."
Data "........#*************************#......."
Data "........#*************************#......."
Data "........#*************************#......."
Data "........#*************************#......."
Data "........#*************************#......."
Data "........###########################......."
Data ".......#***************************#......"
Data ".......#***************************#......"
Data ".......#############################......"
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data ".........................................."
Data "....................###..................."
Data "....................#*#..................."
Data "..................###*###................."
Data "..................#*****#................."
Data "..................###*###................."
Data "....................#*#..................."
Data "....................#*#..................."
Data "...................#####.................."
Data "..................#*****#................."
Data "..................#*****#................."
Data "..................#*****#................."
Data "...................#***#.................."
Data "......#########.....#*#.....#########....."
Data ".....#.........######*######.........#...."
Data ".....#..............#*#..............#...."
Data ".....#....####......#*#......####....#...."
Data ".....#...#****###...#*#...###****#...#...."
Data ".....#.##********##.#*#.##********##.#...."
Data ".....#.#***********##*##***********#.#...."
Data ".....#.#************#*#************#.#...."
Data ".....#.#*****####***#*#***####*****#.#...."
Data ".....#.#****#****##*#*#*##****#****#.#...."
Data "......#*****#******##*##******#*****#....."
Data ".......#****#*******#*#*******#****#......"
Data ".......#****#*******#*#*******#****#......"
Data "........#****#******#*#******#****#......."
Data "........#****#******#*#******#****#......."
Data ".........#****#*****#*#*****#****#........"
Data ".........#*****#****#*#****#*****#........"
Data "..........#*****#***#*#***#*****#........."
Data "..........#######################........."
Data "..........#*********************#........."
Data "..........#######################........."
Data ".........##*********************##........"
Data "........##***********************##......."
Data "........###########################......."
Data ".........................................."
Data ".........................................."
Data ".........................................."