コード例 #1
0
def get_user_move(color=pieces.Piece.BLACK):
    print("Example Move: A2 A4")
    move_str = input("Your Move: ")
    move_str = move_str.replace(" ", "")

    try:
        if move_str == "o-o":
            if color == pieces.Piece.WHITE:
                return ai.Move(4, 0, 4 + 2, 0, True)
            else:
                return ai.Move(4, board.HEIGHT - 1, 4 + 2, board.HEIGHT - 1,
                               True)
        if move_str == "O-O" or move_str == "o-o-o":
            if color == pieces.Piece.WHITE:
                return ai.Move(4, 0, 4 - 2, 0, True)
            else:
                return ai.Move(4, board.HEIGHT - 1, 4 - 2, board.HEIGHT - 1,
                               True)

        xfrom = letter_to_xpos(move_str[0:1])
        yfrom = 8 - int(
            move_str[1:2]
        )  # The board is drawn "upside down", so flip the y coordinate.
        xto = letter_to_xpos(move_str[2:3])
        yto = 8 - int(
            move_str[3:4]
        )  # The board is drawn "upside down", so flip the y coordinate.
        return ai.Move(xfrom, yfrom, xto, yto, False)
    except ValueError:
        print("Invalid format. Example: A2 A4")
        return get_user_move(color)
コード例 #2
0
ファイル: pieces.py プロジェクト: davidepietrasanta/ChessAI
 def get_move(self, board, xto, yto):
     move = 0
     if (board.in_bounds(xto, yto)):
         piece = board.get_piece(xto, yto)
         if (piece != 0):
             if (piece.color != self.color):
                 move = ai.Move(self.x, self.y, xto, yto, False)
         else:
             move = ai.Move(self.x, self.y, xto, yto, False)
     return move
コード例 #3
0
 def is_legal_notation(self, text):
     if len(text) == 3:
         if (text == 'o-o'):
             return ai.Move(
                 ord(text[0]) - ord('a'), 8 - ord(text[1]) + ord('0'), -1,
                 0, True)
         else:
             return False
     elif len(text) == 4:
         if ord(text[0]) < ord('a') or ord(text[0]) > ord('h'):
             return False
         if ord(text[1]) < ord('1') or ord(text[1]) > ord('8'):
             return False
         if ord(text[2]) < ord('a') or ord(text[2]) > ord('h'):
             return False
         if ord(text[3]) < ord('1') or ord(text[3]) > ord('8'):
             return False
         return ai.Move(
             ord(text[0]) - ord('a'), 8 - ord(text[1]) + ord('0'),
             ord(text[2]) - ord('a'), 8 - ord(text[3]) + ord('0'), False)
     elif len(text) == 5:
         if (text == 'o-o-o'):
             return ai.Move(
                 ord(text[0]) - ord('a'), 8 - ord(text[1]) + ord('0'), -1,
                 1, True)
         else:
             return False
     elif len(text) == 6:
         if ord(text[0]) < ord('a') or ord(text[0]) > ord('h'):
             return False
         if ord(text[1]) < ord('1') or ord(text[1]) > ord('8'):
             return False
         if ord(text[2]) < ord('a') or ord(text[2]) > ord('h'):
             return Falsesrc[0]
         if ord(text[3]) < ord('1') or ord(text[3]) > ord('8'):
             return False
         if text[4] != '=':
             return False
         if text[5] == 'b' or text[5] == 'n' or text[5] == 'r' or text[
                 5] == 'q':
             mov = ai.Move(
                 ord(text[0]) - ord('a'), 8 - ord(text[1]) + ord('0'),
                 ord(text[2]) - ord('a'), 8 - ord(text[3]) + ord('0'),
                 False)
             mov.promotion = text[5]
             return mov
         return False
     else:
         return False
コード例 #4
0
ファイル: pieces.py プロジェクト: davidepietrasanta/ChessAI
    def get_long_castling_move(self, board):
        if (self.color == Piece.WHITE and board.white_king_moved):
            return 0
        if (self.color == Piece.BLACK and board.black_king_moved):
            return 0

        piece = board.get_piece(self.x - 4, self.y)
        if (piece != 0):
            if (piece.color == self.color
                    and piece.piece_type == Rook.PIECE_TYPE):
                if (board.get_piece(self.x - 1, self.y) == 0
                        and board.get_piece(self.x - 2, self.y) == 0
                        and board.get_piece(self.x - 3,
                                            self.y) == 0):  #1st condition
                    #The 2nd condition break the IA, if commented the IA will stop crushing
                    #This may be due to the amount of memory
                    #if( not board.is_check(self.color) ): #2nd condition
                    if (not (self.moved)
                            and not (piece.moved)):  #3nd condition
                        if (board.imaginary_check(self.color, self.x - 1,
                                                  self.y) == False
                                and board.imaginary_check(
                                    self.color, self.x - 2, self.y) == False
                                and board.imaginary_check(
                                    self.color, self.x - 3,
                                    self.y) == False):  #4th condition
                            #print("You can performe Long castling")
                            return ai.Move(self.x, self.y, self.x - 2, self.y,
                                           True)

        return 0
コード例 #5
0
ファイル: pieces.py プロジェクト: davidepietrasanta/ChessAI
    def get_short_castling_move(self, board):
        if (self.color == Piece.WHITE and board.white_king_moved):
            return 0
        if (self.color == Piece.BLACK and board.black_king_moved):
            return 0

        piece = board.get_piece(self.x + 3, self.y)
        if (piece != 0):
            if (piece.color == self.color
                    and piece.piece_type == Rook.PIECE_TYPE):
                if (board.get_piece(self.x + 1, self.y) == 0
                        and board.get_piece(self.x + 2,
                                            self.y) == 0):  #1st condition
                    if (not board.is_check(self.color)):  #2nd condition
                        if (not (self.moved)
                                and not (piece.moved)):  #3nd condition
                            if (board.imaginary_check(self.color, self.x + 1,
                                                      self.y) == False
                                    and board.imaginary_check(
                                        self.color, self.x + 2,
                                        self.y) == False):  #4th condition
                                #print("You can performe Short castling")
                                return ai.Move(self.x, self.y, self.x + 2,
                                               self.y, True)

        return 0
コード例 #6
0
def playerTurn(color):
    while True:
        click1 = win1.getMouse()
        checker = findPiece(click1)
        if checker is None or model.board[
                int(checker[0]),
                int(checker[1])].checker is None or model.board[
                    int(checker[0]),
                    int(checker[1])].checker.black is not color:
            continue
        click2 = win1.getMouse()
        piece = findPiece(click2)
        if piece is None or (piece[0] == checker[0]
                             and piece[1] == checker[1]):
            continue
        partial_move = ai.Move(
            model.board[int(checker[0]), int(checker[1])].checker,
            model.board[int(piece[0]), int(piece[1])], "?")
        partial_move.checker.x = checker[0]
        partial_move.checker.y = checker[1]
        move = model.getFullMove(partial_move)
        if move is None:
            continue
        else:
            move.apply(model.board)
            redraw(win1)
            return
コード例 #7
0
ファイル: pieces.py プロジェクト: parul028/AI-Project
    def get_bottom_castling_move(self, board):
        if (self.color == Piece.WHITE and board.white_king_moved):
            return 0
        if (self.color == Piece.BLACK and board.black_king_moved):
            return 0

        piece = board.get_piece(self.x, self.y+4)
        if (piece != 0):
            if (piece.color == self.color and piece.piece_type == Rook.PIECE_TYPE):
                if (board.get_piece(self.x, self.y+1) == 0 and board.get_piece(self.x, self.y+2) == 0 and board.get_piece(self.x, self.y+3) == 0):
                    return ai.Move(self.x, self.y, self.x, self.y+2, True)

        return 0
コード例 #8
0
def get_user_move():
    move_str = input("Your Move: ")
    move_str = move_str.replace(" ", "")

    try:
        xfrom = letter_to_xpos(move_str[0:1])
        yfrom = 8 - int(move_str[1:2])
        xto = letter_to_xpos(move_str[2:3])
        yto = 8 - int(move_str[3:4])
        return ai.Move(xfrom, yfrom, xto, yto, False)
    except ValueError:
        print("Invalid format")
        return get_user_move()
コード例 #9
0
ファイル: main.py プロジェクト: amanpandey104/Chess-AI
def get_user_move():
    print("Format: xfrom,yfrom xto,yto")
    move_str = input("Your Move: ")
    move_str = move_str.replace(" ", "")

    try:
        xfrom = int(move_str[0:1])
        yfrom = int(move_str[2:3])
        xto = int(move_str[3:4])
        yto = int(move_str[5:6])
    except ValueError:
        print("Invalid syntax. Format: xfrom,yfrom xto,yto")
        return get_user_move()

    return ai.Move(xfrom, yfrom, xto, yto, False)
コード例 #10
0
def get_user_move(api, uid, move_str):
    #move_str = move_str[0] + str(9 - int(move_str[1])) + move_str[2] + str(9 - int(move_str[3]))
    move_str = move_str.replace(" ", "")
    try:
        xfrom = letter_to_xpos(move_str[0:1])
        yfrom = 8 - int(
            move_str[1:2]
        )  # The board is drawn "upside down", so flip the y coordinate.
        xto = letter_to_xpos(move_str[2:3])
        yto = 8 - int(
            move_str[3:4]
        )  # The board is drawn "upside down", so flip the y coordinate.
        return ai.Move(xfrom, yfrom, xto, yto, False)
    except ValueError:
        print("Invalid format. Example: A2 A4")
        return 1
コード例 #11
0
ファイル: main.py プロジェクト: Starkultra/ChessAI
def get_user_move():
    print("Example Move: A2 A4")
    move_str = input("Your Move: ")
    move_str = move_str.replace(" ", "")

    try:
        xfrom = letter_to_xpos(move_str[0:1])
        yfrom = 8 - int(
            move_str[1:2]
        )  # The board is drawn "upside down", so flip the y coordinate.
        xto = letter_to_xpos(move_str[2:3])
        yto = 8 - int(
            move_str[3:4]
        )  # The board is drawn "upside down", so flip the y coordinate.
        return ai.Move(xfrom, yfrom, xto, yto, False)
    except ValueError:
        print("Invalid format. Example: A2 A4")
        return get_user_move()
コード例 #12
0
    def run(self):
        print 'new websocket client joined!'
        data = self.connection.recv(1024)
        headers = self.parse_headers(data)
        token = self.generate_token(headers['Sec-WebSocket-Key'])
        self.connection.send('\
HTTP/1.1 101 WebSocket Protocol Hybi-10\r\n\
Upgrade: WebSocket\r\n\
Connection: Upgrade\r\n\
Sec-WebSocket-Accept: %s\r\n\r\n' % token)
        while True:
            try:
                data = self.connection.recv(1024)
            except socket.error, e:
                print "unexpected error: ", e
                clients.pop(self.username)
                break
            data = self.parse_data(data)
            if len(data) == 0:
                continue
            for i in range(0,8):
                if(data[9:10]==list[i]):
                    y_start=i
                    x_start=int(data[10:11])-1
            for i in range(0,8):
                if(data[19:20]==list[i]):
                    y_end=i
                    x_end=int(data[20:21])-1
            move = ai.Move(x_start, y_start, x_end, y_end, False)
            board.perform_move(move)
            print "User move: " + move.to_string()
            ai_move = ai.AI.get_ai_move(board, [])
            board.perform_move(ai_move)
            print "AI move: " + ai_move.to_string()
            ai_move=ai_move.to_string()
            ai_x_start=list[int(ai_move[4:5])]
            ai_y_start=str(int(ai_move[1:2])+1)
            ai_x_end=list[int(ai_move[14:15])]
            ai_y_end=str(int(ai_move[11:12])+1)
            message='{"from"'+':'+'"'+ai_x_start+ai_y_start+'"'+','+'"to"'+':'+'"'+ai_x_end+ai_y_end+'"'+','+'"promotion":"q"}'
            #message='{"from":"a7","to":"a6","promotion":"q"}'
            print message
            notify(message)
コード例 #13
0
ファイル: aitest.py プロジェクト: uwanabtheguy/chess-ai
def get_user_move():
    print "Format: xfrom,yfrom xto,yto"
    return ai.Move(x_start,y_start,x_end,y_end, False)
コード例 #14
0
ファイル: aitest.py プロジェクト: uwanabtheguy/chess-ai
def get_user_move():
    print "Format: xfrom,yfrom xto,yto"
    return ai.Move(3, 1, 3, 2, False)
コード例 #15
0
ファイル: aitest.py プロジェクト: uwanabtheguy/chess-ai
import ai,chess,pieces
def get_user_move():
    print "Format: xfrom,yfrom xto,yto"
    return ai.Move(3, 1, 3, 2, False)
def get_valid_user_move(board):
    move = get_user_move()
    return move
board = chess.Board.new()
move = ai.Move(3, 1, 3, 2, False)
print "User move: " + move.to_string()
ai_move = ai.AI.get_ai_move(board, [])
board.perform_move(ai_move)
print "AI move: " + ai_move.to_string()


###
def get_user_move():
    print "Format: xfrom,yfrom xto,yto"
    return ai.Move(x_start,y_start,x_end,y_end, False)

def get_valid_user_move(board):
    move = get_user_move()
    return move
while True:
    if (x_start!=0 and y_start!=0 and x_end!=0 and y_end!=0):
        print 'test'
        move = get_valid_user_move(board)
        print "User move: " + move.to_string()
        ai_move = ai.AI.get_ai_move(board, [])
        board.perform_move(ai_move)
        print "AI move: " + ai_move.to_string()