Esempio n. 1
0
def play_game():
  player_color = raw_input('Chose your color (w/b): ') == 'w'
  
  if(raw_input('Use default settings? (y/n): ') == 'n'):
    depth = int(raw_input('Max Depth: '))
    threads = int(raw_input('Max Threads: '))
    chess_ai.set_meta_vals(depth, threads)
  else: reload(chess_ai)  # otherwise reload defaults
  
  # inital board state
  board = [['R','N','B','Q','K','B','N','R'],
    ['P','P','P','P','P','P','P','P'],
    ['1','1','1','1','1','1','1','1'],
    ['1','1','1','1','1','1','1','1'],        
    ['1','1','1','1','1','1','1','1'],
    ['1','1','1','1','1','1','1','1'],
    ['p','p','p','p','p','p','p','p'],
    ['r','n','b','q','k','b','n','r']]                    
    
  white_turn = True # white goes first
  ep = '-' # no En Passant, ever
  castle = 0 # players are not allowed to castle (too hard)
  
  print 'Enter moves like this: Ka1-a2'
  print 'Irregardless of color, all pieces are capitol, all ranks are lowercase'
  print 'For captures, use Ka1xa2'
  print 'Dont use P for pawns, ie a2-a4'
  print 'Append C or M for check/mate: a2-a4C'
    
  while(True):  # loop literally forever
    if(white_turn == player_color): # player turn
      print_board(board, player_color)
      move = raw_input('Enter a move: ')
    else: # ai turn
      long_move = chess_ai.get_chess_move(get_fen(board, white_turn, ep, castle),not player_color)
      print long_move # Debug
      check_check = long_move['check'] # check condition is second return value
      move = long_move['move'] # first return is move
    
    # preform the move
    if(move[0] >= 'A' and move[0] <= 'Z'):  # not a pawn
      board[ord(move[2])-ord('0')-1][ord(move[1])-ord('a')] = '1' # remove old piece
      if(white_turn): # is white, upper is fine
        board[ord(move[5])-ord('0')-1][ord(move[4])-ord('a')] = move[0]
      else: # is black, convert to lower case
        board[ord(move[5])-ord('0')-1][ord(move[4])-ord('a')] = chr(ord(move[0]) - ord('A') + ord('a'))
    else: # piece is pawn
      board[ord(move[1])-ord('0')-1][ord(move[0])-ord('a')] = '1' # remove old piece
      if(white_turn): # is white, upper is fine
        board[ord(move[4])-ord('0')-1][ord(move[3])-ord('a')] = 'P'
      else: # is black, convert to lower case
        board[ord(move[4])-ord('0')-1][ord(move[3])-ord('a')] = 'p'
        
    white_turn = not white_turn   # take turns
def makeMove(finalFE):
	ai_move = chess_ai.get_chess_move(finalFE, self.ROBOTCOLOR)  
Esempio n. 3
0
def main():
    #######################initialize##############################
    #Take in image file
    '''
    take in file img to initialize
    then run it throught the same
    '''

    filename = 'lib/chessanglesmall.jpg'
    img = cv2.imread(filename)
    #newimage = cv2.resize(img, (640,480))
    showImage('img', img)
    cv2.waitKey(0)

    #gray = cv2.imread(filename, 0)

    #Get the angle of rotation needed and rotate
    theta = baseLine.findAngle(filename)
    rotatedImage = Rotate.rotateImage(theta, img)

    showImage('rotated image', rotatedImage)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    #Save the rotated image
    cv2.imwrite('lib/rotated.jpg', rotatedImage)

    #Find all the lines of the squares
    imgWithEdges = edgechesshc.addEdges('lib/rotated.jpg')

    #Get all the coordinates of the keypoints
    coordinates = findCoordinates.getCoordinates(imgWithEdges)

    #Pass coordinates on to Squares

    squares = Squares.populateSquares(coordinates)

    #Load squares with their starting state
    Squares.loadPieces(squares, new_piece_attribute, pieceTypeArray)

    ########################repeatedProcess############################

    #at the end of all repeated processes we need to shift all the "new"
    #stuff into the "old" stuff we are using for reference and comparison
    #Find where the peices lie on the board

    #replace with updated image
    newImage = cv2.imread('lib/rotated.jpg')

    squaresCropped = Squares.getSquaresCropped(squares, newImage)

    print type(squaresCropped)

    piece_attribute_array, centerOfPiece = FindPiece.main(squaresCropped)
    #the arrayOfBooleans is T or F if there is a piece in a square
    #centerofpeice is an array of coordinates for the cetner of each piece (catch is it has to be perfectly centered on top of the piece)
    #print arrayOfBooleans
    #print centerOfPiece
    #print array_of_colors
    print piece_attribute_array
    print pieceTypeArray
    #Manage old and new images first
    #pieceTypeArray = findDifferencesArrays(oldBool, newBool, pieceType)

    ####################################finalProcess to start up again####################################3
    ####We have to come up with a way to determine who moved, white or balck so that we can send it to AI
    ###########we will have to constantly update piecetype array as well and boolAray
    finalFE = Squares.giveForsythEdwardsNotation(pieceTypeArray, w_or_b_turn)

    ai_move = chess_ai.get_chess_move(finalFE, ROBOTCOLOR)

    '''
    ##############How to do this#####################################
    if (w_or_b_turn == 'w'):
        w_or_b_turn == 'b'
    elif(w_or_b_turn == 'b'):
        w_or_b_turn == 'w'
        '''

    print ai_move
    ############################Testing##############################
    #Show all the important images
    showImage('original', img)
    showImage('rotated', rotatedImage)
    showImage('image with edges', imgWithEdges)

    #Show all the cropped images
    #Squares.printCroppedSquares(squaresCropped)

    cv2.waitKey(0)
    cv2.destroyAllWindows()
Esempio n. 4
0
def makeMove(finalFE):
	ai_move = chess_ai.get_chess_move(finalFE, ROBOTCOLOR)  
Esempio n. 5
0
def main():
    #######################initialize##############################
    #Take in image file
    '''
    take in file img to initialize
    then run it throught the same
    '''

    filename = 'lib/chessanglesmall.jpg'
    img = cv2.imread(filename)
    #newimage = cv2.resize(img, (640,480))
    showImage('img', img)
    cv2.waitKey(0)

    #gray = cv2.imread(filename, 0)

    #Get the angle of rotation needed and rotate
    theta = baseLine.findAngle(filename)
    rotatedImage = Rotate.rotateImage(theta, img)

    showImage('rotated image', rotatedImage)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    #Save the rotated image
    cv2.imwrite('lib/rotated.jpg', rotatedImage)

    #Find all the lines of the squares
    imgWithEdges = edgechesshc.addEdges('lib/rotated.jpg')

    #Get all the coordinates of the keypoints
    coordinates = findCoordinates.getCoordinates(imgWithEdges)

    #Pass coordinates on to Squares

    squares = Squares.populateSquares(coordinates)

    #Load squares with their starting state
    Squares.loadPieces(squares, new_piece_attribute, pieceTypeArray)

    ########################repeatedProcess############################

    #at the end of all repeated processes we need to shift all the "new"
    #stuff into the "old" stuff we are using for reference and comparison
    #Find where the peices lie on the board

    #replace with updated image
    newImage = cv2.imread('lib/rotated.jpg')

    squaresCropped = Squares.getSquaresCropped(squares, newImage)

    print type(squaresCropped)

    piece_attribute_array, centerOfPiece = FindPiece.main(squaresCropped)
    #the arrayOfBooleans is T or F if there is a piece in a square
    #centerofpeice is an array of coordinates for the cetner of each piece (catch is it has to be perfectly centered on top of the piece)
    #print arrayOfBooleans
    #print centerOfPiece
    #print array_of_colors
    print piece_attribute_array
    print pieceTypeArray
    #Manage old and new images first
    #pieceTypeArray = findDifferencesArrays(oldBool, newBool, pieceType)

    ####################################finalProcess to start up again####################################3
    ####We have to come up with a way to determine who moved, white or balck so that we can send it to AI
    ###########we will have to constantly update piecetype array as well and boolAray
    finalFE = Squares.giveForsythEdwardsNotation(pieceTypeArray, w_or_b_turn)

    ai_move = chess_ai.get_chess_move(finalFE, ROBOTCOLOR)
    '''
    ##############How to do this#####################################
    if (w_or_b_turn == 'w'):
        w_or_b_turn == 'b'
    elif(w_or_b_turn == 'b'):
        w_or_b_turn == 'w'
        '''

    print ai_move
    ############################Testing##############################
    #Show all the important images
    showImage('original', img)
    showImage('rotated', rotatedImage)
    showImage('image with edges', imgWithEdges)

    #Show all the cropped images
    #Squares.printCroppedSquares(squaresCropped)

    cv2.waitKey(0)
    cv2.destroyAllWindows()
Esempio n. 6
0
def play_game():
    player_color = raw_input('Chose your color (w/b): ') == 'w'

    if (raw_input('Use default settings? (y/n): ') == 'n'):
        depth = int(raw_input('Max Depth: '))
        threads = int(raw_input('Max Threads: '))
        chess_ai.set_meta_vals(depth, threads)
    else:
        reload(chess_ai)  # otherwise reload defaults

    # inital board state
    board = [['R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'],
             ['P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'],
             ['1', '1', '1', '1', '1', '1', '1', '1'],
             ['1', '1', '1', '1', '1', '1', '1', '1'],
             ['1', '1', '1', '1', '1', '1', '1', '1'],
             ['1', '1', '1', '1', '1', '1', '1', '1'],
             ['p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'],
             ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r']]

    white_turn = True  # white goes first
    ep = '-'  # no En Passant, ever
    castle = 0  # players are not allowed to castle (too hard)

    print 'Enter moves like this: Ka1-a2'
    print 'Irregardless of color, all pieces are capitol, all ranks are lowercase'
    print 'For captures, use Ka1xa2'
    print 'Dont use P for pawns, ie a2-a4'
    print 'Append C or M for check/mate: a2-a4C'

    while (True):  # loop literally forever
        if (white_turn == player_color):  # player turn
            print_board(board, player_color)
            move = raw_input('Enter a move: ')
        else:  # ai turn
            long_move = chess_ai.get_chess_move(
                get_fen(board, white_turn, ep, castle), not player_color)
            print long_move  # Debug
            check_check = long_move[
                'check']  # check condition is second return value
            move = long_move['move']  # first return is move

        # preform the move
        if (move[0] >= 'A' and move[0] <= 'Z'):  # not a pawn
            board[ord(move[2]) - ord('0') -
                  1][ord(move[1]) - ord('a')] = '1'  # remove old piece
            if (white_turn):  # is white, upper is fine
                board[ord(move[5]) - ord('0') - 1][ord(move[4]) -
                                                   ord('a')] = move[0]
            else:  # is black, convert to lower case
                board[ord(move[5]) - ord('0') -
                      1][ord(move[4]) -
                         ord('a')] = chr(ord(move[0]) - ord('A') + ord('a'))
        else:  # piece is pawn
            board[ord(move[1]) - ord('0') -
                  1][ord(move[0]) - ord('a')] = '1'  # remove old piece
            if (white_turn):  # is white, upper is fine
                board[ord(move[4]) - ord('0') - 1][ord(move[3]) -
                                                   ord('a')] = 'P'
            else:  # is black, convert to lower case
                board[ord(move[4]) - ord('0') - 1][ord(move[3]) -
                                                   ord('a')] = 'p'

        white_turn = not white_turn  # take turns