Buchempfehlung
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Umfassend, aber leicht verständlich führt dieses Buch in die Programmierung von ATMEL AVR Mikrocontrollern ein. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

fb:porticula NoPaste

Info
Info / Hilfe
Liste
Übersicht / Liste
Neu
Datei hochladen
Suche
Quellcode suchen
Download
Dateidownload

chess_gui_engine_vs_engine

Uploader:Mitgliedgrindstone
Datum/Zeit:16.11.2013 09:10:13

#Include "connect_engines.bi"
#Include "fbgfx.bi"

ScreenRes 640,480,32

Dim Shared As Integer mx,my,btn,ox,oy,dx,dy

Type chessMove
    x1 As Integer
    x2 As Integer
    y1 As Integer
    y2 As Integer
    t1 As Integer
    t2 As Integer
End Type

Dim Shared As Integer board(12,12) 'internal representation of board

Dim Shared As Integer exitFlag     'exit program
Dim Shared As Integer bx,by        'position top/left of board

Dim Shared As FB.Image Ptr sImage
sImage = ImageCreate( 42, 42, 0 )  'save screen data

'THIS DETERMINES WHERE TO PLACE BOARD IN WINDOW
bx = 130
by = 30

'read board
For j As Integer = 0 To 11
    For i As Integer = 0 To 11
        Read board(i,j)
    Next i
Next j

'12 images
Dim Shared As FB.Image Ptr iPiece(12)

'allocate for 12 images
For i As Integer = 0 To 11
    iPiece(i) = ImageCreate(42,42,0)
Next i

Dim As String datum

'copy images from data statements to screen
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

'get set of 12 images
For i As Integer = 0 To 11
    Get (i*42,0)-(i*42+41,41), iPiece(i)
Next i

Sub update()
    ScreenLock()
    'drawBoard
    Color RGB(255,255,255),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
        For x As Integer = 0 To 7
            '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
                    Put (bx+x*42,by+y*42),iPiece(Abs(board(x+2,y+2))-1),Trans
                Else
                    Put (bx+x*42,by+y*42),iPiece(Abs(board(x+2,y+2))+5),Trans
                End If
            End If
        Next x
    Next y

    ScreenUnlock()
End Sub

Sub makeMove(cMove As chessMove)
    Locate 1,1
    Dim As Integer choice  'number of selected piece
    Dim As Integer px1,py1,px2,py2,dx,dy  'moving data
    cMove.y1 = cMove.y1-1
    cMove.y2 = cMove.y2-1



    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)
    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 As Integer = 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
        If px1<>px2 Then
            px1 = px1+dx
        End If
        If py1<>py2 Then
            py1 = py1+dy
        End If

        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

    Wend
    'sleep
End Sub

'convert string chess moves to numbers
Function strMoveToNumMove(move As String) As chessMove
    Dim As chessMove cm
    move = UCase(move)
    cm.x1 = Asc(Mid(move,1,1))-65
    cm.y1 = Val(Mid(move,2,1))
    cm.x2 = Asc(Mid(move,3,1))-65
    cm.y2 = Val(Mid(move,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


update()
exitFlag = 0
Dim As chessMove cMove
Dim As String    move
Dim As String hod = Space(9999999)
hod = "position startpos moves"
Locate 52,30
Print "Wait loading engine!"
WriteEngineWhiteInfo("uci")
WriteEngineBlackInfo("uci")
Locate 52,30
Print "Calculate position..."
move = ReadEngineWhiteInfo()
move = ReadEngineBlackInfo()
WriteEngineWhiteInfo("go depth 5")
move = ReadEngineWhiteInfo()


'***************  MAIN LOOP ************************
Do
    '****** ENGINE WHITE ***********
    hod = hod & " " & move
    Locate 2,30
    Print engineWhite
    Locate 48,30
    Print engineBlack
    'Locate 54,1
    'Print Right(hod,200)
    Next_1:
    WriteEngineWhiteInfo(hod)
    WriteEngineWhiteInfo("go depth 5")
    If move  <> "err" Then
        'print move
        If Left(move,4) = "e1g1" Then
            cMove = strMoveToNumMove(Left(move,4))
            makeMove(cMove)
            cMove = strMoveToNumMove("h1f1")
            makeMove(cMove)
            update()
        ElseIf Left(move,4) = "e8g8" Then
            cMove = strMoveToNumMove(Left(move,4))
            makeMove(cMove)
            cMove = strMoveToNumMove("h8f8")
            makeMove(cMove)
            update()
        ElseIf Left(move,4) = "e1c1" Then
            cMove = strMoveToNumMove(Left(move,4))
            makeMove(cMove)
            cMove = strMoveToNumMove("a1d1")
            makeMove(cMove)
            update()
        ElseIf Left(move,4) = "e8c8" Then
            cMove = strMoveToNumMove(Left(move,4))
            makeMove(cMove)
            cMove = strMoveToNumMove("a8d8")
            makeMove(cMove)
            update()
        Else
            cMove = strMoveToNumMove(Left(move,4))
            makeMove(cMove)
            update()
        EndIf
    Else
        Sleep(1000)
    EndIf
    move = ReadEngineWhiteInfo()
    If move  = "err" Then
        Locate 52,30
        Print "Error, try again   "
        GoTo Next_1
    EndIf
    Locate 52,30
    Print move
    If Left(move,4) = "a1a1" Then
        Locate 52,30
        Print " << CHECK MATE >> "
        Sleep
        Exit Do
    EndIf
    Sleep(1)

    '****** ENGINE BLACK ***********
    hod = hod & " " & move
    Locate 2,30
    Print engineWhite
    Locate 48,30
    Print engineBlack
    'Locate 54,1
    'Print Right(hod,200)
    Next_2:
    WriteEngineBlackInfo(hod)
    WriteEngineBlackInfo("go depth 5")
    If move  <> "err" Then
        'print move
        If Left(move,4) = "e1g1" Then
            cMove = strMoveToNumMove(Left(move,4))
            makeMove(cMove)
            cMove = strMoveToNumMove("h1f1")
            makeMove(cMove)
            update()
        ElseIf Left(move,4) = "e8g8" Then
            cMove = strMoveToNumMove(Left(move,4))
            makeMove(cMove)
            cMove = strMoveToNumMove("h8f8")
            makeMove(cMove)
            update()
        ElseIf Left(move,4) = "e1c1" Then
            cMove = strMoveToNumMove(Left(move,4))
            makeMove(cMove)
            cMove = strMoveToNumMove("a1d1")
            makeMove(cMove)
            update()
        ElseIf Left(move,4) = "e8c8" Then
            cMove = strMoveToNumMove(Left(move,4))
            makeMove(cMove)
            cMove = strMoveToNumMove("a8d8")
            makeMove(cMove)
            update()
        Else
            cMove = strMoveToNumMove(Left(move,4))
            makeMove(cMove)
            update()
        EndIf
    Else
        Sleep(1000)
    EndIf
    move = ReadEngineBlackInfo()
    If move  = "err" Then
        Locate 52,30
        Print "Error, try again   "
        GoTo Next_2
    EndIf
    Locate 52,30
    Print move
    If Left(move,4) = "a1a1" Then
        Locate 52,30
        Print " << CHECK MATE >> "
        Sleep
        Exit Do
    EndIf
    Sleep(1)

Loop Until move = "err" Or MultiKey(&H01)


End


'***************************************************

' 1 = pawn, 2 = knight, 3 = bishop, 4 = rook, 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 ".........................................."