another Chess project

User projects written in or related to FreeBASIC.
BasicCoder2
Posts: 3400
Joined: Jan 01, 2009 7:03

another Chess project

Postby BasicCoder2 » Dec 24, 2011 10:16

Inspired by Roland Chastain's working chess program I have begun my own attempt at writing a chess playing program.

This is my latest "working" stage for anyone interested in how I am doing it - comments welcome.

Must run this code to create images of chess pieces.

Code: Select all

'makeImages.bas
'create bitmap of chess pieces for chess program

screenres 840,573,32
dim as string s,v
for k as integer = 0 to 11
    for j as integer = 0 to 31
        read s
        for i as integer =1 to 32
            v = mid(s,i,1)
            if v = " " then
                pset (i+k*32-1,j),&HFFFF00FF  'transparent pink
            end if
            if v = "#" then
                pset (i+k*32-1,j),&HFF000000  'black
            end if
            if v = "+" then
                pset (i+k*32-1,j),&HFFFFFFFF  'white
            end if
        next i
    next j
next k

'*** NOTE:  CHANGE FILE PATH TO SUIT YOURSELF ****
'bsave "c:/FreeBasic/ChessMaster/cPieces.bmp",0
bsave "cPieces.bmp",0

sleep
end

DATA "                                "
DATA "                                "
DATA "                                "
DATA "                                "
DATA "                                "
DATA "                                "
DATA "                                "
DATA "                                "
DATA "              ###               "
DATA "             #+++#              "
DATA "            #+++++#             "
DATA "            #+++++#             "
DATA "        #####+++++#####         "
DATA "       #+++++++++++++++#        "
DATA "      #+++++++++++++++++#       "
DATA "     #+++++++++++++++++++#      "
DATA "     #+++++++++++++++++++#      "
DATA "     #+++++++++++++++++++#      "
DATA "      #+++++++++++++++++#       "
DATA "       ####+++++++++####        "
DATA "          #+++++++++#           "
DATA "          #+++++++++#           "
DATA "         #+++++++++++#          "
DATA "         #+++++++++++#          "
DATA "         #+++++++++++#          "
DATA "         ##+++++++++##          "
DATA "          #+++++++++#           "
DATA "      ####+++++++++++####       "
DATA "     #+++++++++++++++++++#      "
DATA "   ##+++++++++++++++++++++##    "
DATA "   #+++++++++++++++++++++++#    "
DATA "   #########################    "

DATA "                                "
DATA "             #                  "
DATA "      ##    #+#                 "
DATA "      #+#  #++#                 "
DATA "      #++##+++##                "
DATA "      #+++++++++####            "
DATA "       #++++++++++++##          "
DATA "       #++++++++++++++##        "
DATA "       #++++++++++++++++##      "
DATA "      #+++++++++++++++++++#     "
DATA "      #++##++++++++++++++++#    "
DATA "      #++##++++++++++++++++#    "
DATA "     #+++#++++++++++++++++++#   "
DATA "     #++++++++++++++++++++++#   "
DATA "    #++++++++++++++++++++++++#  "
DATA "    #++++++++++++++++++++++++#  "
DATA "   #+++++++++++++++++++++++++#  "
DATA "  #++++++++++++++++++++++++++#  "
DATA " #++++++++++++++##+++++++++++#  "
DATA " #++++++++++++## #+++++++++++#  "
DATA "#+++++++++++##  #++++++++++++#  "
DATA "#+#++++++###    #++++++++++++#  "
DATA "#+++++++#       #++++++++++++#  "
DATA " #+++++#        #++++++++++++#  "
DATA "  #####       ##+++++++++++++#  "
DATA "          ####+++++++++++++++#  "
DATA "         #+++++++++++++++++++#  "
DATA "         #+++++++++++++++++++#  "
DATA "        #++++++++++++++++++++#  "
DATA "        #++++++++++++++++++++#  "
DATA "        #++++++++++++++++++++#  "
DATA "        ######################  "

DATA "                                "
DATA "              ###               "
DATA "            ##+++##             "
DATA "           #+++++++#            "
DATA "          #+++++++++#           "
DATA "         #+++++++++++#          "
DATA "        #++++++#++++++#         "
DATA "       #+++++++#+++++++#        "
DATA "       #+++++++#+++++++#        "
DATA "       #+++++++#+++++++#        "
DATA "       #+++++#####+++++#        "
DATA "       #+++++#####+++++#        "
DATA "       #+++++++#+++++++#        "
DATA "       #+++++++#+++++++#        "
DATA "       #+++++++#+++++++#        "
DATA "       #+++++++#+++++++#        "
DATA "       #+++++++#+++++++#        "
DATA "        #+++++++++++++#         "
DATA "        #+++++++++++++#         "
DATA "         #+###+++###+#          "
DATA "          ##+++++++##           "
DATA "          #+++++++++#           "
DATA "          #+++++++++#           "
DATA "          #+++++++++#           "
DATA "          #+#######+#           "
DATA "           ##+++++##            "
DATA "       #####+++++++#####        "
DATA "     ##+++++++++++++++++##      "
DATA "  ###++++#############++++###   "
DATA " #+++++##             ##+++++#  "
DATA " #+++##                 ##+++#  "
DATA " ####                     ####  "

DATA "                                "
DATA "   ######   ######   ######     "
DATA "   #++++#   #++++#   #++++#     "
DATA "   #++++#   #++++#   #++++#     "
DATA "   #++++#   #++++#   #++++#     "
DATA "   #++++#####++++#####++++#     "
DATA "   #++++++++++++++++++++++#     "
DATA "    ######################      "
DATA "    #++++++++++++++++++++#      "
DATA "    ##++++++++++++++++++##      "
DATA "      #++++++++++++++++#        "
DATA "      ##################        "
DATA "      #++++++++++++++++#        "
DATA "      #++++++++++++++++#        "
DATA "      #++++++++++++++++#        "
DATA "      #++++++++++++++++#        "
DATA "      #++++++++++++++++#        "
DATA "      #++++++++++++++++#        "
DATA "      #++++++++++++++++#        "
DATA "      #++++++++++++++++#        "
DATA "      ##################        "
DATA "      #++++++++++++++++#        "
DATA "     #++++++++++++++++++#       "
DATA "    #++++++++++++++++++++#      "
DATA "    ######################      "
DATA "    #++++++++++++++++++++#      "
DATA "    #++++++++++++++++++++#      "
DATA "   ##++++++++++++++++++++###    "
DATA " ##+++++++++++++++++++++++++#   "
DATA " #++++++++++++++++++++++++++#   "
DATA " #++++++++++++++++++++++++++#   "
DATA " ############################   "

DATA "             ####               "
DATA "             #++#               "
DATA "             #++#               "
DATA "          ####++####            "
DATA "          #++++++++#            "
DATA "          ####++####            "
DATA "             #++#               "
DATA "             #++#               "
DATA "            ######              "
DATA "            #++++#              "
DATA "  #####   ###++++###   #####    "
DATA " ##++++#  #++++++++#  #++++##   "
DATA "##+++++## #++++++++# ##+++++##  "
DATA "#++++++++##++++++++##++++++++#  "
DATA "#+++++++++##++++++##+++++++++#  "
DATA "#+++++++++++#++++#+++++++++++#  "
DATA "#++++++++++++#++#++++++++++++#  "
DATA "#+++++++++++++##+++++++++++++#  "
DATA "#+++++++++++++##+++++++++++++#  "
DATA "#++++++++++++++++++++++++++++#  "
DATA " #++++++++++++++++++++++++++#   "
DATA "  #++++++############++++++#    "
DATA "   #+++##++++++++++++##+++#     "
DATA "    ####++++++++++++++####      "
DATA "    #++++++++++++++++++++#      "
DATA "    #++++++++++++++++++++#      "
DATA "    #+++++##########+++++#      "
DATA "    #++################++#      "
DATA "    #++##++++++++++++##++#      "
DATA "    ###++++++++++++++++###      "
DATA "     ##++++++++++++++++##       "
DATA "       ################         "

DATA "                                "
DATA "                                "
DATA "                                "
DATA "        #             #         "
DATA "       #+#           #+#        "
DATA "      #+++#         #+++#       "
DATA "       #+#           #+#        "
DATA "        #             #         "
DATA "        ##           ##         "
DATA "        #+#         #+#         "
DATA "  #     #+#         #+#     #   "
DATA " #+#    #++#       #++#    #+#  "
DATA "#+++#   #++#       #++#   #+++# "
DATA " #+#    #+++#     #+++#    #+#  "
DATA "  #     #++++#   #++++#     #   "
DATA "   #    #++++#   #++++#    #    "
DATA "  #+#   #+++++# #+++++#   #+#   "
DATA "  #+#   #+++++# #+++++#   #+#   "
DATA "  #++#  #++++++#++++++#  #++#   "
DATA "  #+++# #+++++++++++++# #+++#   "
DATA "  #++++##+++++++++++++##++++#   "
DATA "   #+++++++++++++++++++++++#    "
DATA "   #+++++++#########+++++++#    "
DATA "   #++++###############++++#    "
DATA "   #+###+++++++++++++++###+#    "
DATA "    ##+++++++++++++++++++##     "
DATA "    #++++++#########++++++#     "
DATA "    #+++###############+++#     "
DATA "    #+##+++++++++++++++##+#     "
DATA "     #+++++++++++++++++++#      "
DATA "     #+++++++++++++++++++#      "
DATA "      ###################       "

DATA "                                "
DATA "                                "
DATA "                                "
DATA "                                "
DATA "                                "
DATA "                                "
DATA "                                "
DATA "                                "
DATA "              ###               "
DATA "             #####              "
DATA "            #######             "
DATA "            #######             "
DATA "        ###############         "
DATA "       #################        "
DATA "      ###################       "
DATA "     #####################      "
DATA "     #####################      "
DATA "     #####################      "
DATA "      ###################       "
DATA "       #################        "
DATA "          ###########           "
DATA "          ###########           "
DATA "         #############          "
DATA "         #############          "
DATA "         #############          "
DATA "         #############          "
DATA "          ###########           "
DATA "      ###################       "
DATA "     #####################      "
DATA "   #########################    "
DATA "   #########################    "
DATA "   #########################    "

DATA "                                "
DATA "             #                  "
DATA "      ##    ###                 "
DATA "      ###  ####                 "
DATA "      ##########                "
DATA "      ##############            "
DATA "       ###############          "
DATA "       #################        "
DATA "       ###################      "
DATA "      #####################     "
DATA "      ###++#################    "
DATA "      ###++#################    "
DATA "     ####+###################   "
DATA "     ########################   "
DATA "    ##########################  "
DATA "    ##########################  "
DATA "   ###########################  "
DATA "  ############################  "
DATA " #############################  "
DATA " ############### #############  "
DATA "##############  ##############  "
DATA "##+#########    ##############  "
DATA "#########       ##############  "
DATA " #######        ##############  "
DATA "  #####       ################  "
DATA "          ####################  "
DATA "         #####################  "
DATA "         #####################  "
DATA "        ######################  "
DATA "        ######################  "
DATA "        ######################  "
DATA "        ######################  "

DATA "                                "
DATA "              ###               "
DATA "            #######             "
DATA "           #########            "
DATA "          ###########           "
DATA "         #############          "
DATA "        #######+#######         "
DATA "       ########+########        "
DATA "       ########+########        "
DATA "       ########+########        "
DATA "       ######+++++######        "
DATA "       ######+++++######        "
DATA "       ########+########        "
DATA "       ########+########        "
DATA "       ########+########        "
DATA "       ########+########        "
DATA "       ########+########        "
DATA "        ###############         "
DATA "        ###############         "
DATA "         ##+++###+++##          "
DATA "          ###########           "
DATA "          ###########           "
DATA "          ###########           "
DATA "          ###########           "
DATA "          ##+++++++##           "
DATA "           #########            "
DATA "       #################        "
DATA "     #####################      "
DATA "  ###########################   "
DATA " ########             ########  "
DATA " ######                 ######  "
DATA " ####                     ####  "

DATA "                                "
DATA "   ######   ######   ######     "
DATA "   ######   ######   ######     "
DATA "   ######   ######   ######     "
DATA "   ######   ######   ######     "
DATA "   ########################     "
DATA "   ########################     "
DATA "    ##++++++++++++++++++##      "
DATA "    ######################      "
DATA "    ######################      "
DATA "      ##################        "
DATA "      ##++++++++++++++##        "
DATA "      ##################        "
DATA "      ##################        "
DATA "      ##################        "
DATA "      ##################        "
DATA "      ##################        "
DATA "      ##################        "
DATA "      ##################        "
DATA "      ##################        "
DATA "      ##++++++++++++++##        "
DATA "      ##################        "
DATA "     ####################       "
DATA "    ######################      "
DATA "    ##++++++++++++++++++##      "
DATA "    ######################      "
DATA "    ######################      "
DATA "   #########################    "
DATA " ############################   "
DATA " ############################   "
DATA " ############################   "
DATA " ############################   "

DATA "             ####               "
DATA "             ####               "
DATA "             ####               "
DATA "          ##########            "
DATA "          ##########            "
DATA "          ##########            "
DATA "             ####               "
DATA "             #++#               "
DATA "            ######              "
DATA "            ######              "
DATA "  #####   ##########   #####    "
DATA " #######  ##########  #######   "
DATA "######### ########## #########  "
DATA "##############################  "
DATA "##########+########+##########  "
DATA "############+####+############  "
DATA "#############+##+#############  "
DATA "##############++##############  "
DATA "##############++##############  "
DATA "##############################  "
DATA " ############################   "
DATA "  #######++++++++++++#######    "
DATA "   ####++############++####     "
DATA "    ##++##############++##      "
DATA "    ######################      "
DATA "    ######################      "
DATA "    ######++++++++++######      "
DATA "    ###++++++++++++++++###      "
DATA "    ###++############++###      "
DATA "    ######################      "
DATA "     ####################       "
DATA "       ################         "

DATA "                                "
DATA "                                "
DATA "                                "
DATA "        #             #         "
DATA "       ###           ###        "
DATA "      #####         #####       "
DATA "       ###           ###        "
DATA "        #             #         "
DATA "        ##           ##         "
DATA "        ###         ###         "
DATA "  #     ###         ###     #   "
DATA " ###    ####       ####    ###  "
DATA "#####   ####       ####   ##### "
DATA " ###    #####     #####    ###  "
DATA "  #     ######   ######     #   "
DATA "   #    ######   ######    #    "
DATA "  ###   ####### #######   ###   "
DATA "  ###   ####### #######   ###   "
DATA "  ####  ###############  ####   "
DATA "  ##### ############### #####   "
DATA "  ###########################   "
DATA "   #########################    "
DATA "   ########+++++++++########    "
DATA "   #####+++++++++++++++#####    "
DATA "   ##+++###############+++##    "
DATA "    #######################     "
DATA "    #######+++++++++#######     "
DATA "    ####+++++++++++++++####     "
DATA "    ##++###############++##     "
DATA "     #####################      "
DATA "     #####################      "
DATA "      ###################       "



Code: Select all

#include "fbgfx.bi"
using fb

'screenres 576,576,32
screenres 840,573,32
windowtitle "CHESS"
' **********************  IMPORTANT NOTE !!!! *********************************
' FIRST RUN makeImages.bas to create chessPieces.bmp into current folder
' You may create your own chess images of 32x32 instead of using makeImages.bas
' *****************************************************************************

'***********  CREATE CHESS PIECE IMAGES ***********
' NOTE: CHANGE FILE PATH TO SAME AS USED IN makeImages.bas
' or a file path to your own chess piece images bitmap

'bload "c:\FreeBasic\ChessMaster\chessPieces.bmp"
bload "cPieces.bmp"

'insert test here for unsuccessful load error


dim shared as fb.image ptr wPawn
wPawn = imagecreate(32,32,0)
dim shared as fb.image ptr bPawn
bPawn = imagecreate(32,32,0)
dim shared as fb.image ptr wKnight
wKnight = imagecreate(32,32,0)
dim shared as fb.image ptr bKnight
bKnight = imagecreate(32,32,0)
dim shared as fb.image ptr wBishop
wBishop = imagecreate(32,32,0)
dim shared as fb.image ptr bBishop
bBishop = imagecreate(32,32,0)
dim shared as fb.image ptr wRook
wRook = imagecreate(32,32,0)
dim shared as fb.image ptr bRook
bRook = imagecreate(32,32,0)
dim shared as fb.image ptr wQueen
wQueen = imagecreate(32,32,0)
dim shared as fb.image ptr bQueen
bQueen = imagecreate(32,32,0)
dim shared as fb.image ptr wKing
wKing = imagecreate(32,32,0)
dim shared as fb.image ptr bKing
bKing = imagecreate(32,32,0)

get (0,0)-(31,31),wPawn
get (32,0)-(63,31),wKnight
get (64,0)-(95,31),wBishop
get (96,0)-(127,31),wRook
get (128,0)-(159,31),wKing
get (160,0)-(191,31),wQueen

get (192,0)-(223,31),bPawn
get (224,0)-(255,31),bKnight
get (256,0)-(287,31),bBishop
get (288,0)-(319,31),bRook
get (320,0)-(351,31),bKing
get (352,0)-(383,31),bQueen
'*** END  *****


dim shared as integer b1(12,12)  'hold pieces data
dim shared as integer pieceValue(1 to 6) 'weighted values of piece types




' 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
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,-4,-2,-3,-5,-6,-3,-2,-4, 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, 4, 2, 3, 5, 6, 3, 2, 4, 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


' used to calculate board balance of pieces
'    1 2 3 4 5 6    number ID of piece
data 1,3,3,5,9,500' weight of piece
'    P N B R Q K    type of piece

'GLOBAL VARIABLES
'mouse variables used in selectPiece()
dim shared as integer mx,my,btn,ox,oy
'x1,y1 FROM and x2,y2 TO board coordinates x,y working variables
dim shared as integer x,y,x1,y1,x2,y2   

dim shared as integer player    ' 1 = white or -1 = black  color of current player
dim shared as integer human     ' = 1 to play white or =-1 to play black
dim shared as integer buttonFlag  'button has been selected
dim shared as integer exitFlag  ' exit the game button
dim shared as integer backFlag  ' step back one move
dim shared as integer forwardFlag 'step forward one move if there
dim shared as integer resetFlag ' reset game to start
dim shared as integer saveFlag  ' save current game
dim shared as integer loadFlag  ' load last game saved

dim shared as integer enPasx,enPasy  'any current en passant take square
dim shared as integer enPassant      'flag this as possible move
dim shared as integer pawnQueen      'flag pawn was changed to queen

dim shared as integer check          'flag a king in check
dim shared as integer checkmate      'flag king in check mate

type chessMove
    as integer x1     'from x1
    as integer y1     'from y1
    as integer x2     'To x2
    as integer y2     'To y2
    as integer t1     'type of piece erased by move (including blank = 0)
    as integer t2     'type of piece moved
    as integer v      'value of state resulting from this move if computed
end type

dim shared as chessMove vMove1(100)  'list of possible moves for current piece
dim shared as integer   vCount1   'number of moves on list of possible moves
dim shared as chessMove vMove2(100)  'list of possible moves for current piece
dim shared as integer   vCount2   'number of moves on list of possible moves
dim shared as chessMove vMove3(100)  'list of possible moves for current piece
dim shared as integer   vCount3   'number of moves on list of possible moves

dim shared as string message  'displays check messages

dim shared as chessMove gameMoves(100)  'record actual moves made
dim shared as integer   rCount   'number of moves made in game
dim shared as integer   tCount   'total number of moves made in this game

declare sub playGame()  'start a new game

declare sub drawBoard()      'draws the chess board and exit button for user interface
declare sub selectPiece()    'allows human to use mouse to select square or exit button
declare sub computeMoves(player as integer,vMoves() as chessMove,byref vCount as integer,x as integer,y as integer)   'add valid moves of player piece to b1(x,y)
declare sub makeMove(vMoves() as chessMove,i as integer)       'make vMoves(i) where i is an item in vMoves()
declare sub recordMoves(vMoves() as chessMove,byref vCount as integer,x1 as integer, y1 as integer, x2 as integer, y2 as integer) 'records moves of game
declare sub HumanMove()      'human makes a move
declare sub ComputerMove()   'computer makes a move

declare sub backMove()  'reverse game back one move
declare sub forwardMove(vMoves() as chessMove) 'step game forward one step if such move exists

declare sub displayvMoves(vMoves() as chessMove,vCount as integer) 'box valid moves on chess board

declare function computeBalance() as integer
declare sub createValidMoveList(player as integer,vMoves() as chessMove,byref vCount as integer)
declare function checkCheck(player as integer) as integer

'initialize board with pieces
sub initializeBoard()
    restore
    for y as integer = 0 to 11
        for x as integer = 0 to 11
            read b1(x,y)
        next x
    next y
    'read piece values
    for i as integer = 1 to 6
        read pieceValue(i)
    next i
   
    message = ""
    rCount = 0  'clear recorded moves list
    tCount = 0  'clear maximum move made so far
    vCount1 = 0 'clear valid move lists
    vCount2 = 0
    vCount3 = 0
    enPassant = 0  'clear any passant move
    exitFlag =  0   'flag to exit program

    resetFlag = 0   'flag button selected
    backFlag = 0
    forwardFlag = 0

    player   =   1  'current player, white first
    human    =   1  'human first, -1 computer first move   
end sub

' ######################  MAIN LOOP ###########################



dim as string response
response = "y"
while response = "y"
    initializeBoard()
    playGame()
    cls
    input "NEW GAME y/n";response
wend


' ##################  END OF MAIN LOOP #########################

sub playGame()
    while exitFlag = 0
   
        drawBoard()

        HumanMove()
        drawBoard()
        if player = 1 then player = -1 else player = 1
        if exitFlag = 0 then
           
            HumanMove()
            drawBoard()
            if player = 1 then player = -1 else player = 1
        end if
       
    wend
end sub




sub HumanMove()
    dim as integer valid  'flag valid move selected
    valid = -1
    check = 0
    checkmate = 0
    createValidMoveList(player,vMove1(),vCount1) 'hold valid moves for player for current board state
    createValidMoveList(-player,vMove2(),vCount2) 'hold valid moves for opponent for current board state
   
    message = ""
    'can opponent take player king?
    for i as integer = 0 to vCount2-1 'for each valid opponent move
        if vMove2(i).t2 = sgn(player)*6 then
            check = 1
            message = "  <<<  CHECK  >>> "
        end if
    next i
   
    if check = 1 then
       
        'does player have an escape move?

        for i as integer = 0 to vCount1-1 'for each valid player move
            makeMove(vMove1(),i)
            createValidMoveList(-player,vMove2(),vCount2) 'get opponents possible moves
            for j as integer = 0 to vCount2-1 'for each opponent response
                if vMove2(j).t2 = sgn(player)*6 then
                    vMove1(i).v = -9  'mark invalid
                end if
            next j
            backMove()
        next i
 
        checkmate = 1   'assume a check mate
        for i as integer = 0 to vCount1-1
            if vMove1(i).v <> -9 then
                checkmate = 0  'no checkmate as possible move found
            end if
        next i

        if checkmate = 1 then
            message = "<<< CHECK MATE >>>"
            drawBoard()
            print "HIT KEY TO EXIT"
            sleep
            end
        end if
       
    end if
   
   
    while valid = -1 and exitFlag = 0

        selectPiece()
        'wait until valid selection or exit button clicked
        while sgn(b1(x,y))<> player and exitFlag = 0
            selectPiece()   'position of piece in b1(x,y)
            'show valid moves
        wend
        x1 = x
        y1 = y
        drawBoard()
        displayvMoves(vMove1(),vCount1)
        if exitFlag = 0 then
            selectPiece()  'returns selected square coordinates x,y

            while sgn(b1(x,y))=player and exitFlag=0  'still choosing piece to move?
                x1 = x
                y1 = y
                drawBoard()
                displayvMoves(vMove1(),vCount1)
                selectPiece()
                'show valid moves
            wend
   
            x2 = x
            y2 = y
            valid = -1
            'was move on valid move list?
            for i as integer = 0 to vCount1-1
                if  vMove1(i).x1+2 = x1 and vMove1(i).y1+2 = y1 and vMove1(i).x2+2 = x2 and vMove1(i).y2+2 = y2 then
                    if vMove1(i).v <> -9 then 'not marked invalid?
                        valid = i  'save chosen valid move
                    end if
                end if
            next i
        end if
       
        '*********************************************
       
        if valid <> -1 then
            makeMove(vMove1(),valid)  'board changes state
            drawBoard()
           
            'did this move create a check condition?
            createValidMoveList(-player,vMove2(),vCount2) 'hold valid moves for opponent for current board state

            drawBoard()

            'can opponent take player king?
            for i as integer = 0 to vCount2-1 'for each valid opponent move
                if vMove2(i).t2 = sgn(player)*6 then
                    check = 1
                    message = "  <<<  CHECK  >>> "
                    valid = -1
                end if
            next i

            if valid = -1 then
                backMove() 'if check then take back move try again
            end if
           
        end if
       
    wend

end sub

sub createValidMoveList(player as integer,vMoves() as chessMove,byref vCount as integer)
    'find each piece and add its valid moves to vMoves() list
    vCount = 0
    for j as integer = 2 to 9
        for i as integer = 2 to 9
            if sgn(b1(i,j))=player and abs(b1(i,j))>0 and b1(i,j)<>7 then
                computeMoves(player,vMoves(),vCount,i,j)  'add valid moves for piece at b1(i,j)
            end if
        next i
    next j
   
end sub

sub drawBoard()
    dim as integer shade = 0
    for y as integer = 0 to 11
        for x as integer = 0 to 11
           
            'DRAW SQUARE
            if shade = 0 then shade = 1 else shade = 0
            if shade = 0 then
                line (x*48,y*48)-(x*48+47,y*48+47),rgb(128,128,128),bf
            else
                line (x*48,y*48)-(x*48+47,y*48+47),rgb(255,255,255),bf
            end if
            if x<2 or x>9 or y<2 or y>9 then
                line (x*48,y*48)-(x*48+47,y*48+47),rgb(0,120,120),bf
            end if
           
            'DRAW PIECE ON THIS SQUARE
            'any white piece on this square?
            if b1(x,y)=1 then
                put (x*48+9,y*48+9),wPawn,trans
            end if
            if b1(x,y)=2 then
                put (x*48+9,y*48+9),wKnight,trans
            end if
            if b1(x,y)=3 then
                put (x*48+9,y*48+9),wBishop,trans
            end if
            if b1(x,y)=4 then
                put (x*48+9,y*48+9),wRook,trans
            end if
            if b1(x,y)=5 then
                put (x*48+9,y*48+9),wQueen,trans
            end if
            if b1(x,y)=6 then
                put (x*48+9,y*48+9),wKing,trans
            end if
            'any black piece on this square?
            if b1(x,y)=-1 then
                put (x*48+9,y*48+9),bPawn,trans
            end if
            if b1(x,y)=-2 then
                put (x*48+9,y*48+9),bKnight,trans
            end if
            if b1(x,y)=-3 then
                put (x*48+9,y*48+9),bBishop,trans
            end if
            if b1(x,y)=-4 then
                put (x*48+9,y*48+9),bRook,trans
            end if
            if b1(x,y)=-5 then
                put (x*48+9,y*48+9),bQueen,trans
            end if
            if b1(x,y)=-6 then
                put (x*48+9,y*48+9),bKing,trans
            end if           
        next x
        if shade = 0 then shade = 1 else shade = 0
    next y
   
    color rgb(255,255,255),rgb(0,120,120)
    for x = 1 to 8
        locate 10,x*6+9
        print chr(x+64);
    next x
    for y = 1 to 8
        locate y*6+10,10
        print 9-y
    next y
   
    line (576,0)-(839,573),rgb(100,100,0),bf
   
   
    locate 1,1
    print "BALANCE OF BOARD =";computeBalance()
   
    'print any message like check or checkmate
    locate 70,3
    print message
   
    'print player to move next

    locate 3,20
    if player = 1 then
        line (130,8)-(290,30),rgb(0,0,0),bf
        color rgb(255,255,255),rgb(0,0,0)
        print " WHITE TO MOVE "
    else
        line (130,8)-(290,30),rgb(255,255,255),bf
        color rgb(0,0,0),rgb(255,255,255)
        print " BLACK TO MOVE "
    end if
   
    line (96,96)-(480,480),rgb(0,0,0),b  'draw board border
   
    'draw EXIT BUTTON
    locate 3,3
    line (8,8)-(60,30),rgb(0,0,0),bf
    color rgb(255,0,0),rgb(0,0,0)
    print "EXIT"
   
    'draw step back button
    locate 6,3
    line (8,34)-(60,54),rgb(0,0,0),bf
    color rgb(255,0,0),rgb(0,0,0)
    print " << "
   
    'draw step forward button
    locate 9,3
    line (8,58)-(60,78),rgb(0,0,0),bf
    color rgb(255,0,0),rgb(0,0,0)
    print " >> "
   
    'draw reset button
    locate 12,3
    line (8,82)-(60,102),rgb(0,0,0),bf
    color rgb(255,0,0),rgb(0,0,0)
    print "RESET"

    locate 15,3
    line (8,106)-(60,126),rgb(0,0,0),bf
    color rgb(255,0,0),rgb(0,0,0)
    print "SAVE"
   
    locate 18,3
    line (8,130)-(60,150),rgb(0,0,0),bf
    color rgb(255,0,0),rgb(0,0,0)
    print "LOAD"
   
    'DISPLAY MOVES MADE LIST
    color rgb(255,255,255),rgb(0,0,0)
    locate 1,94:print " GAME MOVES "
    locate 2,94:print "============"
    if rCount<>0 then
        for i as integer = 0 to rCount-1
            locate i+3,94
            print gameMoves(i).t2;" ";chr(gameMoves(i).x1+65);str(8-gameMoves(i).y1);chr(gameMoves(i).x2+65);str(8-gameMoves(i).y2);gameMoves(i).t1
        next i
    end if
 
    locate 65,3
    print "enPassant =";enPassant
    locate 66,3
    print "pawnQueen =";pawnQueen
   
    'print any message like check or checkmate
    locate 65,30
    line (215,500)-(385,532),rgb(0,0,255),bf
    print message
   
end sub

sub displayvMoves(vMoves() as chessMove,vCount as integer)
    if vCount<>0 then
        for i as integer = 0 to vCount-1
            if vMoves(i).x1+2 = x1 and vMoves(i).y1+2 = y1 then
                line ( (vMoves(i).x1+2) *48, (vMoves(i).y1+2) *48)-( (vMoves(i).x1+2) *48+46, (vMoves(i).y1+2) *48+46),rgb(255,0,255),b
                line ( (vMoves(i).x1+2) *48+1, (vMoves(i).y1+2) *48+1)-( (vMoves(i).x1+2) *48+47, (vMoves(i).y1+2) *48+47),rgb(255,0,255),b
                line ( (vMoves(i).x2+2) *48, (vMoves(i).y2+2) *48)-( (vMoves(i).x2+2) *48+46, (vMoves(i).y2+2) *48+46),rgb(0,0,255),b
                line ( (vMoves(i).x2+2) *48+1, (vMoves(i).y2+2) *48+1)-( (vMoves(i).x2+2) *48+47, (vMoves(i).y2+2) *48+47),rgb(0,0,255),b
            end if
        next i
    end if
end sub


sub selectPiece()

    buttonflag = 0  'flag when button is pressed
   
    do
        getmouse ox,oy,,btn

        'wait for button press
        while btn<>1
            getmouse mx,my,,btn
            if mx<>ox or my<>oy then           
                ox = mx
                oy = my
               
                drawboard()  'clears any highlights
                displayvMoves(vMove1(),vCount1)
                'over one of the chess board squares?
                if mx>96 and my>96 and mx<480 and my<480 then
                    line (int(mx/48)*48,int(my/48)*48)-(int(mx/48)*48+47,int(my/48)*48+47),rgb(0,255,0),b
                    line (int(mx/48)*48+1,int(my/48)*48+1)-(int(mx/48)*48+46,int(my/48)*48+46),rgb(0,255,0),b
                end if
           
                'over exit button?
                if mx>8 and my>8 and mx<60 and my<30 then
                    line (8,8)-(60,30),rgb(255,255,255),b
                end if
               
                'over step back button
                if mx>8 and my>34 and mx<60 and my<54 then
                    line (8,34)-(60,54),rgb(255,255,255),b
                end if
               
                'over step forward button?
                if mx>8 and my>58 and mx<60 and my<78 then
                    line (8,58)-(60,78),rgb(255,255,255),b
                end if
               
                'over reset button?
                if mx>8 and my>82 and mx<60 and my<102 then
                    line (8,82)-(60,102),rgb(255,255,255),b
                end if
               
                'over save button?
                if mx>8 and my>106 and mx<60 and my<126 then
                    line (8,106)-(60,126),rgb(255,255,255),b
                end if
               
                'over load buttons?
                if mx>8 and my>130 and mx<60 and my<150 then
                    line (8,130)-(60,150),rgb(255,255,255),b
                end if
               
            end if
        wend

        x = mx\48
        y = my\48

        if mx>8 and mx<60 and my>8  and my<30  then exitFlag=1
       
        if mx>8 and mx<60 and my>8  and my<150 then buttonFlag = 1
        if mx>8 and mx<60 and my>82 and my<102 then resetFlag = 1
        if mx>8 and mx<60 and my>34 and my<54  then backFlag = 1
        if mx>8 and mx<60 and my>58 and my<78  then forwardFlag = 1
        if mx>8 and mx<60 and my>106 and my<126 then saveFlag = 1
        if mx>8 and mx<60 and my>130 and my<150 then loadFlag = 1

        'HANDLE BUTTON EVENTS
        if buttonFlag = 1 then  'deal with button commands
            if resetFlag = 1 then
                exitFlag = 1
            end if
     
            if backFlag = 1 then
                backMove()  'remove opponent move
                'backMove()  'remove player move
                backFlag = 0
            end if
         
            if forwardFlag = 1 then
                forwardMove(vMove1())  'restore player move
                'forwardMove(vMove1())  'restore opponent move
                forwardFlag = 0
            end if
           
            if saveFlag = 1 then
                OPEN "c:\FreeBasic\ChessMasterA\game.txt" FOR OUTPUT AS #1
                for i as integer = 0 to tCount-1
                    print i;
                    write #1, gameMoves(i).t1,gameMoves(i).x1,gameMoves(i).y1,gameMoves(i).x2,gameMoves(i).y2,gameMoves(i).t2
                next i
                close #1
                saveFlag = 0
            end if
           
            if loadFlag = 1 then
                tCount = 0
                rCount = 0
                OPEN "c:\FreeBasic\ChessMasterA\game.txt" FOR INPUT AS #1
                do until eof(1)
                    print tCount;
                    input #1, gameMoves(tCount).t1,gameMoves(tCount).x1,gameMoves(tCount).y1,gameMoves(tCount).x2,gameMoves(tCount).y2,gameMoves(tCount).t2
                    tCount = tCount+1
                loop
                close #1
                loadFlag = 0
            end if
           
            'drawBoard()
            buttonFlag=0
         
        end if
     
        '    wait for button release
        while btn=1
            getmouse mx,my,,btn
        wend
   
    loop until (x>1 and x<10) and (y>1 and y<10) or exitFlag<>0

end sub

' COMPUTES VALID MOVES FOR PIECE AT POSITION x,y adds them to valid moves list vMove()
sub computeMoves(player as integer,vMove1() as chessMove,byref vCount1 as integer,x as integer,y as integer)

    'WHITE PAWN
    if b1(x,y)= 1 then
           
        if b1(x,y-1)=0 then
            recordMoves(vMove1(),vCount1,x,y,x,y-1)
        end if
           
        if y=8 then
            if b1(x,y-2)=0 and b1(x,y-1)=0 then
                recordMoves(vMove1(),vCount1,x,y,x,y-2)
            end if
        end if
           
        if b1(x+1,y-1)<0 and b1(x+1,y-1)<>7 then
            recordMoves(vMove1(),vCount1,x,y,x+1,y-1)
        end if
           
        if b1(x-1,y-1)<0 and b1(x-1,y-1)<>7 then
            recordMoves(vMove1(),vCount1,x,y,x-1,y-1)
        end if
       
        if enPassant = 1 then
            if enPasy = y-1 then
                if enPasx = x-1 or enPasx = x+1 then
                    recordMoves(vMove1(),vCount1,x,y,enPasx,enPasy)
                end if
            end if
        end if
       
           
    end if
       
    'BLACK PAWN
    if b1(x,y)= -1 then

        if b1(x,y+1)=0 then
            recordMoves(vMove1(),vCount1,x,y,x,y+1)               
        end if
           
        'double move forward?
        if y=3 then
            if b1(x,y+2)=0 and b1(x,y+1)=0 then
                recordMoves(vMove1(),vCount1,x,y,x,y+2)
            end if
        end if
           
        'diagonal take
        if b1(x+1,y+1)>0 and b1(x+1,y+1)<>7 then
            recordMoves(vMove1(),vCount1,x,y,x+1,y+1)
        end if
        if b1(x-1,y+1)>0 and b1(x-1,y+1)<>7 then
            recordMoves(vMove1(),vCount1,x,y,x-1,y+1)
        end if
       
        if enPassant = 1 then
            if enPasy = y+1 then
                if enPasx = x-1 or enPasx = x+1 then
                    recordMoves(vMove1(),vCount1,x,y,enPasx,enPasy)
                end if
            end if
        end if
       
    end if
       
    'KNIGHT
    if b1(x,y)=2 * player then
        if sgn(b1(x-1,y-2))<> player and b1(x-1,y-2)<>7 then
            recordMoves(vMove1(),vCount1,x,y,x-1,y-2)
        end if
        if sgn(b1(x+1,y-2))<> player and b1(x+1,y-2)<>7 then
            recordMoves(vMove1(),vCount1,x,y,x+1,y-2)
        end if
        if sgn(b1(x-2,y-1))<> player and b1(x-2,y-1)<>7 then
            recordMoves(vMove1(),vCount1,x,y,x-2,y-1)
        end if
        if sgn(b1(x+2,y-1))<> player and b1(x+2,y-1)<>7 then
            recordMoves(vMove1(),vCount1,x,y,x+2,y-1)
        end if
        if sgn(b1(x-2,y+1))<> player and b1(x-2,y+1)<>7 then
            recordMoves(vMove1(),vCount1,x,y,x-2,y+1)
        end if
        if sgn(b1(x+2,y+1))<> player and b1(x+2,y+1)<>7 then
            recordMoves(vMove1(),vCount1,x,y,x+2,y+1)
        end if
        if sgn(b1(x-1,y+2))<> player and b1(x-1,y+2)<>7 then
            recordMoves(vMove1(),vCount1,x,y,x-1,y+2)
        end if
        if sgn(b1(x+1,y+2))<> player and b1(x+1,y+2)<>7 then
            recordMoves(vMove1(),vCount1,x,y,x+1,y+2)
        end if
           
    end if
       
    'BISHOP
    if b1(x,y)=3 * player then

        dim as integer t
           
        t = 0
        do
            t = t + 1
            if sgn(b1(x+t,y+t))<> player and b1(x+t,y+t)<>7 then
                recordMoves(vMove1(),vCount1,x,y,x+t,y+t)
            end if
        loop while b1(x+t,y+t)=0
           
        t = 0
        do
            t = t + 1
            if sgn(b1(x-t,y+t))<> player and b1(x-t,y+t)<>7 then
                recordMoves(vMove1(),vCount1,x,y,x-t,y+t)
            end if
        loop while b1(x-t,y+t)=0
           
        t = 0
        do
            t = t + 1
            if sgn(b1(x-t,y-t))<> player and b1(x-t,y-t)<>7 then
                recordMoves(vMove1(),vCount1,x,y,x-t,y-t)
            end if
        loop while b1(x-t,y-t)=0
           
        t = 0
        do
            t = t + 1
            if sgn(b1(x+t,y-t))<> player and b1(x+t,y-t)<>7 then
                recordMoves(vMove1(),vCount1,x,y,x+t,y-t)
            end if
        loop while b1(x+t,y-t)=0
           
    end if
       
    'ROOK
    if b1(x,y)=4 * player then

        dim as integer t
           
        t = 0
        do
            t = t + 1
            if sgn(b1(x+t,y))<> player and b1(x+t,y)<>7 then
                recordMoves(vMove1(),vCount1,x,y,x+t,y)
            end if
        loop while b1(x+t,y)=0
       
        t = 0
        do
            t = t + 1
            if sgn(b1(x-t,y))<> player and b1(x-t,y)<>7 then
                recordMoves(vMove1(),vCount1,x,y,x-t,y)
            end if
        loop while b1(x-t,y)=0
           
        t = 0
        do
            t = t + 1
            if sgn(b1(x,y-t))<> player and b1(x,y-t)<>7 then
                recordMoves(vMove1(),vCount1,x,y,x,y-t)
            end if
        loop while b1(x,y-t)=0
           
        t = 0
        do
            t = t + 1
            if sgn(b1(x,y+t))<> player and b1(x,y+t)<>7 then
                recordMoves(vMove1(),vCount1,x,y,x,y+t)
            end if
        loop while b1(x,y+t)=0
           
    end if
       
    'QUEEN
    if b1(x,y)=5 * player then

        dim as integer t
        t = 0
        do
            t = t + 1
            if sgn(b1(x+t,y+t))<> player and b1(x+t,y+t)<>7 then
                recordMoves(vMove1(),vCount1,x,y,x+t,y+t)
            end if
        loop while b1(x+t,y+t)=0
           
        t = 0
        do
            t = t + 1
            if sgn(b1(x-t,y+t))<> player and b1(x-t,y+t)<>7 then
                recordMoves(vMove1(),vCount1,x,y,x-t,y+t)
            end if
        loop while b1(x-t,y+t)=0
           
        t = 0
        do
            t = t + 1
            if sgn(b1(x-t,y-t))<> player and b1(x-t,y-t)<>7 then
                recordMoves(vMove1(),vCount1,x,y,x-t,y-t)
            end if
        loop while b1(x-t,y-t)=0
           
        t = 0
        do
            t = t + 1
            if sgn(b1(x+t,y-t))<> player and b1(x+t,y-t)<>7 then
                recordMoves(vMove1(),vCount1,x,y,x+t,y-t)
            end if
        loop while b1(x+t,y-t)=0
           
        '========================
           
        t = 0
        do
            t = t + 1
            if sgn(b1(x+t,y))<> player and b1(x+t,y)<>7 then
                recordMoves(vMove1(),vCount1,x,y,x+t,y)
            end if
        loop while b1(x+t,y)=0
           
        t = 0
        do
            t = t + 1
            if sgn(b1(x-t,y))<> player and b1(x-t,y)<>7 then
                recordMoves(vMove1(),vCount1,x,y,x-t,y)
            end if
        loop while b1(x-t,y)=0
           
        t = 0
        do
            t = t + 1
            if sgn(b1(x,y-t))<> player and b1(x,y-t)<>7 then
                recordMoves(vMove1(),vCount1,x,y,x,y-t)
            end if
        loop while b1(x,y-t)=0
           
        t = 0
        do
            t = t + 1
            if sgn(b1(x,y+t))<> player and b1(x,y+t)<>7 then
                recordMoves(vMove1(),vCount1,x,y,x,y+t)
            end if
        loop while b1(x,y+t)=0
           
    end if
       
    'KING
    if b1(x,y)=6 * player then
        if sgn(b1(x+1,y+1))<> player and b1(x+1,y+1)<>7 then
            recordMoves(vMove1(),vCount1,x,y,x+1,y+1)
        end if
        if sgn(b1(x-1,y+1))<> player and b1(x-1,y+1)<>7 then
            recordMoves(vMove1(),vCount1,x,y,x-1,y+1)
        end if
        if sgn(b1(x-1,y-1))<> player and b1(x-1,y-1)<>7 then
            recordMoves(vMove1(),vCount1,x,y,x-1,y-1)
        end if
        if sgn(b1(x+1,y-1))<> player and b1(x+1,y-1)<>7 then
            recordMoves(vMove1(),vCount1,x,y,x+1,y-1)
        end if
        if sgn(b1(x,y-1))<> player and b1(x,y-1)<>7 then
            recordMoves(vMove1(),vCount1,x,y,x,y-1)
        end if
        if sgn(b1(x,y+1))<> player and b1(x,y+1)<>7 then
            recordMoves(vMove1(),vCount1,x,y,x,y+1)
        end if
        if sgn(b1(x+1,y))<> player and b1(x+1,y)<>7 then
            recordMoves(vMove1(),vCount1,x,y,x+1,y)
        end if
        if sgn(b1(x-1,y))<> player and b1(x-1,y)<>7 then
            recordMoves(vMove1(),vCount1,x,y,x-1,y)
        end if
           
        if player = 1 then
            ' white R***K
            if b1(2,9)=4 and b1(3,9)=0 and b1(4,9)=0 and b1(5,9)= 0 and b1(6,9)=6 then
                recordMoves(vMove1(),vCount1,6,9,4,9)
                recordMoves(vMove1(),vCount1,2,9,5,9)
            end if
            ' white K**R
            if b1(6,9)=6 and b1(7,9)=0 and b1(8,9)=0 and b1(9,9)=4 then
                recordMoves(vMove1(),vCount1,6,9,8,9)
                recordMoves(vMove1(),vCount1,9,9,7,9)
            end if
        else
            ' black R***K
            if b1(2,2)= -4 and b1(3,2)=0 and b1(4,2)=0 and b1(5,2)= 0 and b1(6,2)= -6 then
                recordMoves(vMove1(),vCount1,6,2,4,2)
                recordMoves(vMove1(),vCount1,2,2,5,2)
            end if
            ' black K**R
            if b1(6,2)= -6 and b1(7,2)=0 and b1(8,2)=0 and b1(9,2)= -4 then
                recordMoves(vMove1(),vCount1,6,2,8,2)
                recordMoves(vMove1(),vCount1,9,2,7,2)
            end if
        end if
           
    end if
end sub


sub recordMoves(vMoves() as chessMove,byref vCount1 as integer, x1 as integer, y1 as integer, x2 as integer, y2 as integer)
    ' -2 converts to display coordinates
    vMoves(vCount1).t1 = b1(x1,y1)
    vMoves(vCount1).t2 = b1(x2,y2)
    vMoves(vCount1).x1 = x1-2
    vMoves(vCount1).y1 = y1-2
    vMoves(vCount1).x2 = x2-2
    vMoves(vCount1).y2 = y2-2
    vMoves(vCount1).v  = 0    'clear old values
    vCount1 = vCount1 + 1
end sub

sub makeMove(vMoves() as chessMove, i as integer)
    ' **** TODO record any piece removed here ****
    dim as integer x1,y1,x2,y2
    ' +2 converts back to internal board coordinates
    x1 = vMoves(i).x1+2
    x2 = vMoves(i).x2+2
    y1 = vMoves(i).y1+2
    y2 = vMoves(i).y2+2
 
    'record move made
    ' -2 converts back to display board coordinates
    gameMoves(rCount).x1 = x1-2
    gameMoves(rCount).y1 = y1-2
    gameMoves(rCount).x2 = x2-2
    gameMoves(rCount).y2 = y2-2
    gameMoves(rCount).t1 = b1(x2,y2)  'save square type at destination
    gameMoves(rCount).t2 = b1(x1,y1)  'save type of piece moved
    b1(x2,y2)=b1(x1,y1)
    b1(x1,y1)=0
    rCount = rCount + 1
    if rCount>tCount then tCount = rCount
   
    'check if this was an en passant move
    if b1(x2,y2)=1 then  'was it a white pawn move
        if x2<>x1 then 'was it a diagonal move?
            if gameMoves(rCount-1).t1 = 0 then 'to a blank square?
                'remove item below
                b1(x2,y2+1)=0
            end if
        end if
    end if
    if b1(x2,y2)=-1 then  'was it a black pawn move
        if x2<>x1 then 'was it a diagonal move?
            if gameMoves(rCount-1).t1 = 0 then 'to a blank square?
                'remove item above
                b1(x2,y2-1)=0
            end if
        end if
    end if

    enPassant = 0  '
   
    'check if en passant situation arises from this move
    if b1(x2,y2)=1  and y1 = 8 then 'WHITE pawn double move from row 8?
        if b1(x2+1,y2)=-1 or b1(x2-1,y2)= -1 then 'now next to BLACK pawn?
            enPasx = x2
            enPasy = 7
            enPassant = 1  'flag possible move
        end if
    end if
    if b1(x2,y2)=-1  and y1 = 3 then 'BLACK pawn double move from row 8?
        if b1(x2+1,y2)=1 or b1(x2-1,y2)=1 then 'now next to WHITE pawn?
            enPasx = x2
            enPasy = 4
            enPassant = 1  'flag possible move
        end if
    end if   

    '*** CHECK IF PAWN REACHED END IF SO CONVERT TO QUEEN ***
    if abs(b1(x2,y2))=1 then  'was it a pawn?
        if y2 = 2 then  'must have been white pawn
            b1(x2,y2)=5 'make white queen
        end if
        if y2 = 9 then  'must have been black pawn
            b1(x2,y2)=-5 'make black queen
        end if
    end if
   
    'was it a castle move, test for a king moving more than one step left or right
    if abs(b1(x2,y2))=6 and abs(x2-x1)=2 then
        x1 = vMoves(i+1).x1+2
        x2 = vMoves(i+1).x2+2
        y1 = vMoves(i+1).y1+2
        y2 = vMoves(i+1).y2+2

        'record move made
        gameMoves(rCount).x1 = x1-2
        gameMoves(rCount).y1 = y1-2
        gameMoves(rCount).x2 = x2-2
        gameMoves(rCount).y2 = y2-2
       
        gameMoves(rCount).t1 = b1(x2,y2) 'save square type at destination
        gameMoves(rCount).t2 = b1(x1,y1) 'save type of piece moved
       
        b1(x2,y2)=b1(x1,y1)   'move rook
        b1(x1,y1)=0
        rCount = rCount + 1
        if rCount>tCount then tCount = rCount
    end if

end sub

sub backMove()
    'vCount = 0
    drawBoard()  'clears any highlighted squares
    if rCount <> 0 then
        rCount = rCount - 1
        x1 = gameMoves(rCount).x1 + 2
        y1 = gameMoves(rCount).y1 + 2
        x2 = gameMoves(rCount).x2 + 2
        y2 = gameMoves(rCount).y2 + 2
        b1(x1,y1)=gameMoves(rCount).t2 'restore piece moved
        b1(x2,y2)=gameMoves(rCount).t1 'restore square value

        '*** CHECK FOR EN PASSANT MOVE ***
        'if it was a white pawn and moved diagonally and restored a blank it must have been en passant
        if b1(x1,y1)=1 and x2<>x1 and gameMoves(rCount).t1 = 0 then
            'restore black pawn
            b1(x1+1,y1)=-1
        end if
        'if it was a BLACK pawn and moved diagonally and restored a blank it must have been en passant
        if b1(x1,y1)=-1 and x2<>x1 and gameMoves(rCount).t1 = 0 then
            'restore WHITE pawn
            b1(x1-1,y1)=1
        end if
    end if
   
    'was it a castle move?
    if rCount <> 0 then
        'get move coordinates of previous
        rCount = rCount - 1
        x1 = gameMoves(rCount).x1 + 2
        y1 = gameMoves(rCount).y1 + 2
        x2 = gameMoves(rCount).x2 + 2
        y2 = gameMoves(rCount).y2 + 2
        rCount = rCount + 1  'assume no back move
       
        if abs(b1(x2,y2))=6 then 'was it a king?
            if abs(x2-x1)=2 then 'was it a castle move?
                b1(x1,y1)=b1(x2,y2)
                b1(x2,y2)=gameMoves(rCount).t1 'restore square value
                rCount = rCount - 1  'was a back move
            end if
        end if
    end if
end sub

sub forwardMove(vMoves() as chessMove)
    drawBoard()
    'place any pre made moves into vMoves for makeMove() to do again
    if tCount>rCount then   'if total moves made is greater than current position
       'place move into valid move list
        vMoves(0).x1 = gameMoves(rCount).x1
        vMoves(0).x2 = gameMoves(rCount).x2
        vMoves(0).y1 = gameMoves(rCount).y1
        vMoves(0).y2 = gameMoves(rCount).y2
        'was it a king move two places?
        if abs(vMoves(0).x1-vMoves(0).x2)=2 then
            'get rook move
            vMoves(1).x1 = gameMoves(rCount+1).x1
            vMoves(1).x2 = gameMoves(rCount+1).x2
            vMoves(1).y1 = gameMoves(rCount+1).y1
            vMoves(1).y2 = gameMoves(rCount+1).y2
        end if
       
        makeMove(vMoves(),0) 'this will bump rCount
       
    end if
end sub

function computeBalance() as integer
    dim as integer sum
    sum = 0
    for j as integer = 2 to 9
        for i as integer = 2 to 9
            if b1(i,j)<>0 and b1(i,j)<>7 then 'not border or blank square?
                sum = sum + pieceValue(abs(b1(i,j))) * sgn(b1(i,j))
            end if
        next i
    next j
    return sum
end function
Last edited by BasicCoder2 on Feb 21, 2012 8:42, edited 7 times in total.
VANYA
Posts: 1323
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Postby VANYA » Dec 24, 2011 10:50

1) Management of excellent!
2) Ability to his pieces in bmp files - too well

I hope, AI will also be a good

----------------------------------------------

Image
Roland Chastain
Posts: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Postby Roland Chastain » Dec 24, 2011 14:46

What a good new ! I copy it at once. The code looks very nice and simple. Impressive !
Your pieces are beautiful too. I am sure I will learn many things by reading this program. Thanks a lot !
Roland Chastain
Posts: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Postby Roland Chastain » Dec 24, 2011 15:15

It is a good work that you made ! It works perfectly. And I can, as VANYA wishes, select a piece and immediately select another. I still believe that it isn't a very regular manner to order moves, but I recognize it is handy. Anyway, from a technical point of view, it is very well done.
fxm
Posts: 9178
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Postby fxm » Dec 24, 2011 15:28

BasicCoder2 wrote:Any suggestions or comments welcome.

IMHO, just a small global overview remark:
- too much 'Dim Shared ...',
- a single data structure 'Type ...',
- many procedures without passed parameters,
all that is linked and does not make very easier a quick and fine understanding of your program.

Try to more group your variables in different data structures (UDT or array for example), and pass them to your procedures as objects or pointers to objects, using far fewer of elementary shared variables.

A well structured program (that it seems to be) must handle well structured data too.
BasicCoder2
Posts: 3400
Joined: Jan 01, 2009 7:03

Postby BasicCoder2 » Dec 24, 2011 20:51

@VANYA -

step forward isn't implemented yet.

The printing of variable values is just one way I debug. I have removed it.

I am working on the AI that for me is the interesting but difficult part.



@fxm

Thank you for your suggestions. Old habits are hard to break even if the extra time put into design pays off in the long run. Readability along with ease of bug free modifications is very important. I will attempt a rewrite now I have a feel for what is required.
BasicCoder2
Posts: 3400
Joined: Jan 01, 2009 7:03

Postby BasicCoder2 » Dec 24, 2011 20:52

BasicCoder2 wrote:@VANYA -

step forward isn't implemented yet.

The printing of variable values is just one way I debug. I have removed it.

I am working on the AI. For me is the interesting but difficult part.



@fxm

Thank you for your suggestions. Old habits are hard to break even if the extra time put into design pays off in the long run. Readability along with ease of bug free modifications is very important. I will attempt a rewrite now I have a feel for what is required.
Roland Chastain
Posts: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Postby Roland Chastain » Dec 24, 2011 21:17

BasicCoder2 wrote:I am working on the AI that for me is the interesting but difficult part.


Since I have seen your program, I'm thinking to a way to make two programs play one against another. It would be pleasant and instructive, isn't it ? If I manage to find how to do it I will say to you.
Anyway I hope we will continue to exchange ideas and observations.
BasicCoder2
Posts: 3400
Joined: Jan 01, 2009 7:03

Postby BasicCoder2 » Dec 24, 2011 23:37

Roland Chastain wrote:
BasicCoder2 wrote:I am working on the AI that for me is the interesting but difficult part.


Since I have seen your program, I'm thinking to a way to make two programs play one against another. It would be pleasant and instructive, isn't it ? If I manage to find how to do it I will say to you.
Anyway I hope we will continue to exchange ideas and observations.


I would like that as it will motivate me to keep going.
BasicCoder2
Posts: 3400
Joined: Jan 01, 2009 7:03

Postby BasicCoder2 » Dec 27, 2011 0:53

Edited code in first post.

The step forward button [>>] implemented and some bugs fixed.

AI evaluation routine is work in progress ...
Roland Chastain
Posts: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Postby Roland Chastain » Dec 27, 2011 1:16

BasicCoder2 wrote:
Roland Chastain wrote:Anyway I hope we will continue to exchange ideas and observations.

I would like that as it will motivate me to keep going.


Hello !

I hope your work progress as you want.

As an technical exercise, I began a board with the pieces of a chess program whose name is Warlord. As you will see, I have a new manner to store pictures, since I know how to open files... By this simple mean I earned 40 ko ! Just by removing the commas ! I was very happy of this "discovery".
BasicCoder2
Posts: 3400
Joined: Jan 01, 2009 7:03

Postby BasicCoder2 » Dec 27, 2011 21:22

I hope your work progress as you want.


At the moment I have the computer playing against itself rather than me having to play every game. Ultimately I will have to pit it against an advanced program, maybe the Chess Titan that came with windows 7.

As an technical exercise, I began a board with the pieces of a chess program whose name is Warlord. As you will see, I have a new manner to store pictures, since I know how to open files... By this simple mean I earned 40 ko ! Just by removing the commas ! I was very happy of this "discovery".


I used letters to represent rgb values in the data statements,

http://www.freebasic.net/forum/viewtopi ... ht=frogger

The advantage in sprites is you can have them move across the board, you can pick and place them with the mouse select button, and you can have them animated.
Roland Chastain
Posts: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Postby Roland Chastain » Dec 28, 2011 8:07

BasicCoder2 wrote:I used letters to represent rgb values in the data statements,

http://www.freebasic.net/forum/viewtopi ... ht=frogger

The advantage in sprites is you can have them move across the board, you can pick and place them with the mouse select button, and you can have them animated.


I 've just read your post. Very interesting link about Frogger. I read all the thread and copied the code.
Roland Chastain
Posts: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: Another CHESS project

Postby Roland Chastain » Dec 30, 2011 17:33

@BasicCoder2

By trying your board, I noticed something. Black pieces on white squares : the shape (I hope it's the right word) is white, as the square. It doesn't look very nice. Did you notice too ?

I believe you will need one more color.
BasicCoder2
Posts: 3400
Joined: Jan 01, 2009 7:03

Re: Another CHESS project

Postby BasicCoder2 » Jan 01, 2012 22:21

Roland Chastain wrote:@BasicCoder2

By trying your board, I noticed something. Black pieces on white squares : the shape (I hope it's the right word) is white, as the square. It doesn't look very nice. Did you notice too ?

I believe you will need one more color.


The user interface can be made as fancy as you like. The black pieces are just an inversion of the white pieces to reduce the number of data statements. I can fix that anytime but there will be no point unless I can get a reasonable AI up and running.

Return to “Projects”

Who is online

Users browsing this forum: No registered users and 7 guests