Beispiel #1
0
    def successors(game, state, check):
        state_copy = None
        pieces = []
        successors = []
        valid_moves = []
        move = None
        #JLabel label = new JLabel();

        if state.player.alliance == "Whites":
            pieces = state.white_pieces
        elif state.player.alliance == "Blacks":
            pieces = state.black_pieces

        successors = []

        for i in range(16):
            if pieces[i] != None:
                valid_moves = GameTools.valid_moves(game, pieces[i], check)

                for j in range(len(valid_moves)):
                    move = valid_moves[j]

                    state_copy = pgame.state.State(state)

                    if game.verify_and_play_move(state_copy, move[0][0], move[0][1], move[1][0], move[1][1], True):
                        game.state = state_copy
                        if state_copy.player.alliance == "Whites":
                            if GameTools.black_is_checkmate(game):
                                continue
                        elif state_copy.player.alliance == "Blacks":
                            if GameTools.white_is_checkmate(game):
                                continue

						# promotion
#						for (int k=0; k<16; k++) {
#							if (stateCopy.getWhite()[k]!=null && stateCopy.getWhite()[k].isPromoted())
#								stateCopy.getWhite()[k].setPromoted(false);
#							if (stateCopy.getBlack()[k]!=null && stateCopy.getBlack()[k].isPromoted())
#								stateCopy.getBlack()[k].setPromoted(false);
#						}
                        if (pieces[i].nom == "pwn" and
                            pieces[i].alliance == "Whites" and
                            move[1][1] == 0):

                            for k in range(4):
                                successors.append((state_copy, (move[0], move[1], [k])))
                            continue								
                        elif (pieces[i].nom == "pwn" and
                              pieces[i].alliance == "Blakcs" and
                              move[1][1] == 7):

                            for k in range(4):
                                successors.append((state_copy, (move[0], move[1], [k])))
                            continue						

                        successors.append((state_copy, move))
                game.state = state

        return successors
Beispiel #2
0
    def feval_stalemate(game, state):
        game_state = game.state
        res = 0.

        game.state = state

        if GameTools.white_is_stalemate(game):
            res = -10

        game.state = game_state

        return res
Beispiel #3
0
    def feval_checkmate(game, state):
        game_state = game.state
        res = 0.

        game.state = state

        if GameTools.white_is_checkmate(game):
            res = 100

        game.state = game_state

        return res
Beispiel #4
0
    def treat_draggable_piece(self, screen, draggable_piece, event):
        if event.type == pygame.MOUSEBUTTONDOWN:
            mx, my = event.pos
            if draggable_piece.x + piece_width >= mx and draggable_piece.y + piece_height >= my:
                if draggable_piece.x <= mx and draggable_piece.y <= my:
                    draggable_piece.click = True

            # Java import (begin) #
            if (self.game.state.player.ptype == "Artificial"):
                return

            self.begin = False
            self.xcurs = mx
            self.ycurs = my
            self.square1 = GameTools.square(self.xcurs - self.offset,
                                            self.ycurs - self.offset)

            if draggable_piece != None:
                if (self.xcurs > draggable_piece.x
                        and self.xcurs < draggable_piece.x + self.p_size
                        and self.ycurs > draggable_piece.y
                        and self.ycurs < draggable_piece.y + self.p_size):
                    self.select = True
                    draggable_piece.selected = True
                    self.selected_piece = draggable_piece

#                   if (positionLabel!=null)
#                       positionLabel.setText(GameUtil.square(xcurs-offset, ycurs-offset));
# Java import (end)
        elif event.type == pygame.MOUSEBUTTONUP:
            mx, my = event.pos
            draggable_piece.xshiftbegin = -1
            draggable_piece.yshiftbegin = -1
            draggable_piece.click = False

            # Java import (begin) #
            if self.game.state.player.ptype == "Human":
                self.play_move(screen, mx, my)
            # Java import (end) #
        elif event.type == pygame.QUIT:
            pygame.quit()
            sys.exit(0)
Beispiel #5
0
    def verify_and_play_move(self, state, x1, y1, x2, y2, test):
        board = state.board
        whites = state.white_pieces
        blacks = state.black_pieces
        current_player = state.player
        
        piece = board[y1][x1]
        
        if piece != None and (piece.is_valid_move(x1, y1, x2, y2, board, current_player, state.check) or
			(state.player.ptype == "Artificial" and piece.promoted)):

#            print("verify_and_play_move() player = " + current_player.alliance)
#            print("verify_and_play_move() piece =  : " + str(piece))
#            print("verify_and_play_move() x1, y1, x2, y2 =  : " + str((x1, y1, x2, y2)))
#            print("verify_and_play_move() state.check =  : " + str(state.check))
            
            # enlevement de la piece
            board[y1][x1] = None
            
            if board[y2][x2] == None:
                if current_player.alliance == "Whites":
                    if (x2 == x1+1 or x2 == x1-1) and y1 == 3 and y2 == 2:
                        if board[y2+1][x2] != None and board[y2+1][x2].en_passant_risk:
                            for i in range(16):
                                if blacks[i] != None and hash(blacks[i]) == hash(board[y2+1][x2]):
                                    blacks[i] = None
                                    state.add_capture(board[y2+1][x2])
                                    board[y2+1][x2] = None
                                    break
                    else: # annulation de l'eventuel pion en_passant_risk = true
                        for i in range(16):
                            if blacks[i] != None and blacks[i].en_passant_risk:
                                blacks[i].en_passant_risk = False
                                break
                elif current_player.alliance == "Blacks":
                    if (x2 == x1+1 or x2 == x1-1) and y1 == 4 and y2 == 5:
                        if board[y2-1][x2] != None and board[y2-1][x2].en_passant_risk:
                            for i in range(16):
                                if whites[i] != None and hash(whites[i]) == hash(board[y2-1][x2]):
                                    whites[i] = None
                                    state.add_capture(board[y2-1][x2]);
                                    board[y2-1][x2] = None
                                    break
                    else: # annulation de l'eventuel pion en_passant_risk = true
                        for i in range(16):
                            if whites[i] != None and whites[i].en_passant_risk:
                                whites[i].en_passant_risk = False
                                break
            elif board[y2][x2] != None:
                state.add_capture(board[y2][x2])
                if current_player.alliance == "Whites":
                    for i in range(16):
                        if blacks[i] != None and hash(blacks[i]) == hash(board[y2][x2]):
                            blacks[i] = None
                            break
                elif current_player.alliance == "Blacks":
                    for i in range(16):
                        if whites[i] != None and hash(whites[i]) == hash(board[y2][x2]):
                            whites[i] = None
                            break
						
            # placement de la nouvelle piece
            # promotion
            if current_player.ptype != "Artificial":
                for i in range(16):
                    if whites[i] != None and whites[i].promoted:
                        whites[i].promoted = False
                    if blacks[i] != None and blacks[i].promoted:
                        blacks[i].promoted = False

            if (not test and piece.nom == "pwn" and
                current_player.ptype != "Artificial" and
                piece.alliance == "Whites" and
                y2 == 0):
						
                self.listbox = tk.Listbox()
                for item in ["Queen", "Rook", "Bishop", "Knight"]:
                    self.listbox.insert(END, item)
                self.listbox.pack()
                
                self.listbox.bind("<<ListboxSelect>>", self.select)
                tk.mainloop()
				
                if self.selected_value == None:
                    return False
				
                if self.selected_value == 0:
                    board[y2][x2] = Queen("Whites")
                elif self.selected_value == 1:
                    board[y2][x2] = Rook("Whites")
                elif self.selected_value == 2:
                    board[y2][x2] = Bishop("Whites")
                elif self.selected_value == 3:
                    board[y2][x2] = Knight("Whites")
				
                board[y2][x2].x = x2
                board[y2][x2].y = y2
                board[y2][x2].promoted = True
                
                for i in range(16):
                    if whites[i] != None and hash(whites[i]) == hash(piece):
                        whites[i] = board[y2][x2]
                        break				
            elif (not test and piece.nom == "pwn" and
                  current_player.ptype != "Artificial" and
                  piece.alliance == "Blacks" and
                  y2 == 7):

                self.listbox = tk.Listbox()
                for item in ["Queen", "Rook", "Bishop", "Knight"]:
                    self.listbox.insert(END, item)
                self.listbox.pack()
                
                self.listbox.bind("<<ListboxSelect>>", self.select)
                tk.mainloop()

                if self.selected_value == None:
                    return False
				
                if self.selected_value == 0:
                    board[y2][x2] = Queen("Blacks")
                elif self.selected_value == 1:
                    board[y2][x2] = Rook("Blacks")
                elif self.selected_value == 2:
                    board[y2][x2] = Bishop("Blacks")
                elif self.selected_value == 3:
                    board[y2][x2] = Knight("Blacks")

                board[y2][x2].x = x2
                board[y2][x2].y = y2
                board[y2][x2].promoted = True
                
                for i in range(16):
                    if blacks[i] != None and hash(blacks[i]) == hash(piece):
                        blacks[i] = board[y2][x2]
                        break				
            else:
                board[y2][x2] = piece
                piece.x = x2
                piece.y = y2
                
                # castling
                if (not state.check and x1 == 4 and (x2 == 2 or x2 == 6) and
                    ((y1 == 0 and y2 == 0) or (y1 == 7 and y2 == 7))):
                    if board[y2][x2].nom == "kng":
                        if (x2 == 2 and board[y2][0] != None and
                            board[y2][0].nom == "rok" and
                            board[y2][0].castling_poss):
                            
                            board[y2][3] = board[y2][0]
                            board[y2][0] = None
                            board[y2][3].x = 3
                            board[y2][3].y = y2
                            board[y2][3].castling_poss = False
								
                        elif (x2 == 6 and board[y2][7] != None and
                              board[y2][7].nom == "rok" and
                              board[y2][7].castling_poss):

                            board[y2][5] = board[y2][7]
                            board[y2][7] = None
                            board[y2][5].x = 5
                            board[y2][5].y = y2
                            board[y2][5].castling_poss = False									

            # changement du joueur
            state.player = GameTools.change_player(current_player, self)

            # check
            if state.check:
                state.check = False

            return True
        else:
            return False
Beispiel #6
0
    def play_move(self, screen, x, y):
        #print("play_move(" + str(x) + ", " + str(y) + ")")
        if self.game.state.player.ptype == "Artificial" and not self.move_entered:
            return
        else:
            self.move_entered = False

        for i in range(32):
            if self.pieces[i] != None and self.pieces[i].selected:
                break
            if i + 1 == 32:
                return

        game_state = self.game.state
        next_state = None
        self.xcurs = x
        self.ycurs = y
        y1 = abs(int(self.square1[1]) - 8)
        x1 = ord(self.square1[0]) - ord('a')
        y2 = None
        x2 = None
        current_player = self.game.state.player
        piece = self.game.state.board[y1][x1]

        #        print("play_move() : x1=" + str(x1) + ", y1=" + str(y1))
        #        print("play_move() : piece = " + str(piece))

        if piece == None:
            return

        next_state = State(game_state)

        if pygame.mouse.get_focused(
        ) == 0 and current_player.ptype != "Artificial":
            self.selected_piece.x = GameTools.convertX(self.square1,
                                                       self.p_size,
                                                       self.offset)
            self.selected_piece.y = GameTools.convertY(self.square1,
                                                       self.p_size,
                                                       self.offset)
            self.selected_piece.selected = False
            self.paint(screen)
            return

        self.select = False
        self.square2 = GameTools.square(self.xcurs - self.offset,
                                        self.ycurs - self.offset)
        y2 = abs(int(self.square2[1]) - 8)
        x2 = ord(self.square2[0]) - ord('a')

        if self.game.verify_and_play_move(next_state, x1, y1, x2, y2,
                                          False) and not self.end_of_game:
            #            print("play_move() : x1, y1 = " + str((x1, y1)))
            #            print("play_move() : x2, y2 = " + str((x2, y2)))
            self.game.state = next_state
            if not GameTools.is_end_of_game(self.game):
                # placement de la piece
                self.selected_piece.x = GameTools.convert_x(
                    self.square2, self.p_size, self.offset)
                self.selected_piece.y = GameTools.convert_y(
                    self.square2, self.p_size, self.offset)
                self.selected_piece.selected = False

                n = 0

                for i in range(8):
                    for j in range(8):
                        p = self.game.state.board[j][i]

                        if p != None:

                            for k in range(32):
                                if (self.pieces[k] != None
                                        and self.pieces[k].selected == False):

                                    self.change_image(self.pieces[k], p)

                                    self.pieces[k].x = GameTools.convert_x(
                                        str(chr(i + ord('a'))) +
                                        str(abs(j - 8)), self.p_size,
                                        self.offset)
                                    self.pieces[k].y = GameTools.convert_y(
                                        str(chr(i + ord('a'))) +
                                        str(abs(j - 8)), self.p_size,
                                        self.offset)
                                    self.pieces[k].selected = True
                                    n = k + 1
                                    break
                for k in range(n, 32):
                    if self.pieces[k] != None and self.pieces[
                            k].selected == False:
                        self.pieces[k] = None

                self.game.state.review.append((self.square1, self.square2))
            else:
                if (GameTools.message == "Whites will be check!"
                        or GameTools.message == "Blacks will be check!"):
                    self.game.state = game_state
                    for i in range(32):
                        if self.pieces[i] != None and self.pieces[i].selected:
                            self.pieces[i].selected = False
                            self.pieces[i].x = GameTools.convert_x(
                                self.square1, self.p_size, self.offset)
                            self.pieces[i].y = GameTools.convert_y(
                                self.square1, self.p_size, self.offset)
                            break
                elif (GameTools.message == "Checkmate! Whites win"
                      or GameTools.message == "Checkmate! Blacks win"
                      or GameTools.message == "Whites are stalemated!"
                      or GameTools.message == "Blacks are stalemated!"):
                    self.end_of_game = True
                    self.imax = 0

                    # capture et/ou placement du dernier coup
                    self.selected_piece.x = GameTools.convert_x(
                        self.square2, self.p_size, self.offset)
                    self.selected_piece.y = GameTools.convert_y(
                        self.square2, self.p_size, self.offset)
                    self.selected_piece.selected = False

                    n = 0

                    for i in range(8):
                        for j in range(8):
                            p = self.game.state.board[j][i]
                            if p != None:

                                for k in range(32):
                                    if (self.pieces[k] != None and
                                            self.pieces[k].selected == False):

                                        self.change_image(self.pieces[k], p)

                                        self.pieces[k].x = GameTools.convert_x(
                                            str(chr(i + ord('a'))) +
                                            str(abs(j - 8)), self.p_size,
                                            self.offset)
                                        self.pieces[k].y = GameTools.convert_y(
                                            str(chr(i + ord('a'))) +
                                            str(abs(j - 8)), self.p_size,
                                            self.offset)
                                        self.pieces[k].selected = True
                                        n = k + 1
                                        break
                    for k in range(n, 32):
                        if self.pieces[k] != None and self.pieces[
                                k].selected == False:
                            self.pieces[k] = None

                    self.game.state.review.append((self.square1, self.square2))
            self.paint(screen)
        else:
            if not self.end_of_game:
                self.game.state = next_state

            player = "Whites" if piece.alliance == "White" else "Black"

            if piece.alliance == current_player.alliance and not self.end_of_game:
                print("Move is not valid")
            elif not self.end_of_game:
                print("This is not " + player + " turn")

            self.selected_piece.selected = False
            self.selected_piece.x = GameTools.convert_x(
                self.square1, self.p_size, self.offset)
            self.selected_piece.y = GameTools.convert_y(
                self.square1, self.p_size, self.offset)

            self.paint(screen)

        for i in range(32):
            if self.pieces[i] != None:
                self.pieces[i].selected = False

        p = None

        if next_state.player.alliance == "Whites":
            p = self.game.blacks_player
        else:
            p = self.game.whites_player

        if (next_state.player.ptype == "Artificial" and p.ptype == "Human"
                and not self.end_of_game):
            self.play(screen)
Beispiel #7
0
    def play(self, screen):
        state = self.game.state
        player = state.player
        board = state.board
        move = None
        sq2 = ""
        piece = None

        if player.ptype == "Human":
            return

        move = player.enter_move(self.game,
                                 state.check)  # ((x1, y1), (x2, y2))

        if move == None:
            return

        self.move_entered = True
        self.square1 = "" + (chr(ord("a") + move[0][0])) + str(
            abs(move[0][1] - 8))
        sq2 = "" + (chr(ord("a") + move[1][0])) + str(abs(move[1][1] - 8))
        print("CPU tries " + sq2)

        for i in range(32):
            if (self.pieces[i] != None
                    and self.pieces[i].x == GameTools.convert_x(
                        self.square1, self.p_size, self.offset)
                    and self.pieces[i].y == GameTools.convert_y(
                        self.square1, self.p_size, self.offset)):
                self.pieces[i].selected = True
                self.selected_piece = self.pieces[i]
                # promotion
                if len(move) == 3:
                    for j in range(16):
                        if state.white_pieces[
                                j] != None and state.white_pieces[j].promoted:
                            state.white_pieces[j].promoted = False
                        if state.black_pieces[
                                j] != None and state.black_pieces[j].promoted:
                            state.black_pieces[j].promoted = False

                    piece = board[move[0][1]][move[0][0]]
                    board[move[0][1]][move[0][0]] = None

                    if move[2][0] == 0:
                        board[move[0][1]][move[0][0]] = Queen(player.alliance)
                    elif move[2][0] == 1:
                        board[move[0][1]][move[0][0]] = Rook(player.alliance)
                    elif move[2][0] == 2:
                        board[move[0][1]][move[0][0]] = Bishop(player.alliance)
                    elif move[2][0] == 3:
                        board[move[0][1]][move[0][0]] = Knight(player.alliance)
                    else:
                        board[move[0][1]][move[0][0]] = Queen(player.alliance)

                    board[move[0][1]][move[0][0]].promoted = True

                    if player.alliance == "Whites":
                        for j in range(16):
                            if (state.white_pieces[j] != None and hash(
                                    state.white_pieces[j]) == hash(piece)):
                                state.white_pieces[j] = board[move[0][1]][
                                    move[0][0]]
                                break
                    elif player.alliance == "Blacks":
                        for j in range(16):
                            if (state.black_pieces[j] != None and hash(
                                    state.black_pieces[j]) == hash(piece)):
                                state.black_pieces[j] = board[move[0][1]][
                                    move[0][0]]
                                break
                break
            if i + 1 == 32:
                return

        self.play_move(screen,
                       GameTools.convert_x(sq2, self.p_size, self.offset),
                       GameTools.convert_y(sq2, self.p_size, self.offset))
Beispiel #8
0
 def is_end_of_exploration(game, node, PMAX):
     return node.depth == PMAX or GameTools.is_end_of_game(game)