Esempio n. 1
0
    def check_computer_placement_move(self, piece, respawn):
        if self.pieces[piece].availability:
            #temp_inter = [i for i in self.pieces[piece].inital_position if i not in self.occupied_position_on_board]

            anirudh_index = [
                (Helping_Class.convert_my_coordinate_to_anirudh(i[0], i[1]), i)
                for i in self.pieces[piece].inital_position
            ]
            print(piece, anirudh_index)
            temp_inter = [
                index[1] for index in anirudh_index
                if not self.game_state.board[index[0]]
            ]

            if temp_inter:
                index = randint(0, len(temp_inter) - 1)
                where = temp_inter[index]
                self.selected_position = Helping_Class.selection_bar_reverse_mapping[
                    piece]
                #self.occupied_position_on_board.append(where)

            if temp_inter:
                temp_game_state = CP.game_data()
                temp_game_state = copy.deepcopy(self.game_state)
                data_convert = CP.Conversion_of_postion_name(
                    piece, Helping_Class.selection_bar_reverse_mapping[piece],
                    (where[0], where[1]))
                temp_game_state.update(data_convert.piece,
                                       int(data_convert.i_pos_ani()),
                                       int(data_convert.f_pos_ani()))
                temp_game_state.active_color = not temp_game_state.active_color
                fen = temp_game_state.generate_fen()
                board2 = chess.Board(fen=fen)
                print(board2)
                print(fen)
                print('board2.is_check()', board2.is_check())

                if not board2.is_check():
                    self.selected_piece = piece
                    data_convert = CP.Conversion_of_postion_name(
                        self.selected_piece, self.selected_position,
                        (where[0], where[1]))
                    self.game_state.update(data_convert.piece,
                                           int(data_convert.i_pos_ani()),
                                           int(data_convert.f_pos_ani()))
                    self.selected_piece = None
                    self.selected_position = None
                    self.pieces[piece].availability -= 1
                    self.computer_turn = False
                    respawn = False
                    self.black_pieces_on_board += 1
                    self.whose_move == 'white'
                    self.computer_where, self.computer_to = None, None
                    return respawn
                else:
                    return respawn
        return respawn
Esempio n. 2
0
    def get_board_state(self):
        fen = self.game_state.generate_fen()

        board = chess.Board(fen=fen)
        #when check is their
        if board.is_check():
            #print('1')
            if board.is_checkmate():
                #print('2')
                checkmate = 'checkmate'
                if self.whose_move == 'white':
                    piece_list = [
                        'WRook', 'WQueen', 'WKnight', 'WBishop', 'WPawn'
                    ]
                else:
                    piece_list = [
                        'BRook', 'BQueen', 'BKnight', 'BBishop', 'BPawn'
                    ]
                for piece in piece_list:
                    if self.pieces[piece].availability:
                        for position in self.pieces[piece].inital_position:

                            if not self.game_state.board[
                                    Helping_Class.
                                    convert_my_coordinate_to_anirudh(
                                        position[0], position[1])]:
                                print('piece', piece, ' at position :',
                                      position)
                                temp_game_state = CP.game_data()
                                temp_game_state = copy.deepcopy(
                                    self.game_state)
                                data_convert = CP.Conversion_of_postion_name(
                                    piece, Helping_Class.
                                    selection_bar_reverse_mapping[piece],
                                    (position[0], position[1]))
                                temp_game_state.update(
                                    data_convert.piece,
                                    int(data_convert.i_pos_ani()),
                                    int(data_convert.f_pos_ani()))
                                temp_game_state.active_color = not temp_game_state.active_color
                                fen = temp_game_state.generate_fen()
                                board2 = chess.Board(fen=fen)
                                #print(board2)
                                #print(board2.is_check())
                                if not board2.is_check():
                                    checkmate = 'pseudo_check'
                                    return checkmate

                return checkmate

            else:
                #normal check
                return 'check'
        else:
            #normal game
            return 'normal'
Esempio n. 3
0
    def __init__(self, screen):

        self._where, self._to = None, None
        self.game_state = CP.game_data()

        self.screen = screen
        self.selection_bar_background = pygame.image.load('pieces/cover.png')
        self.selection_bar_effect = pygame.image.load(
            'pieces/cover_effect.png')
        self.board_effect = pygame.image.load('pieces/cover_effect2.png')
        self.selection_bar_not_available = pygame.image.load(
            'pieces/all_used.png')
        self.background = pygame.image.load('background.png')
        #variaables to update frequently
        self.whose_move = "white"
        self.selected_from_selection_bar = False
        self.selected_from_board = False
        self.selected_piece = None
        #current selected position
        self.selected_position = None  #(0,560)

        #self.occupied_position_on_board = [(320,560),(320,0)]
        """
		self.board_positions = []
		_temp__ = [None,None,None,None,None,None,None,None]
		for i in range(8):
			self.board_positions .append(list(_temp__))
		"""

        self.blit_piece = []  #0: location 1: piece
        """
		#just testing the program 
		self.board_positions[0][7] = 'BRook'
		self.board_positions[7][0] = 'WRook'
		print(self.board_positions)
		"""

        self.pieces = {
            'WRook': WRook(),
            'WKnight': WKnight(),
            "WBishop": WBishop(),
            'WQueen': WQueen(),
            "WKing": WKing(),
            "WPawn": WPawn(),
            "BRook": BRook(),
            "BKnight": BKnight(),
            "BBishop": BBishop(),
            "BQueen": BQueen(),
            "BKing": BKing(),
            "BPawn": BPawn()
        }

        self.anirudh_2_pritish = {
            "R": "WRook",
            "N": "WKnight",
            "B": "WBishop",
            "K": "WKing",
            "Q": "WQueen",
            "P": "WPawn",
            "r": "BRook",
            "n": "BKnight",
            "b": "BBishop",
            "k": "BKing",
            "q": "BQueen",
            "p": "BPawn",
            None: None
        }
Esempio n. 4
0
    def main_board_maintenance(self, x_cor, y_cor, situation):
        """
										`		clicked on board
											        |
									---------------------------------
									|								|
								   yes                              No
								    |                               |
						   selected from where                nothing to do
						            |
					    --------------------------------------------------------------------------------------
						|                                         |                                          |
			      selection_bar                                 Board                                  no selection 
				        |                                         |                                          |
			is clicked on valid position               is clicked on valid position(Rajan)           clicked on empty slot 
			   |                    |                   |     (it returns pgn)    |                           |
			   |                    |                   |                         |                           |
			  yes                  no                  yes                        no              -----------------------------
			   |                    |                   |                         |               |                           |
		  place it         (if his piece)    does move contain 'x' (capture)    select piece      no                         yes 
		(if not check)     change selection        |                      |    (Ask & Discuss)    |                           |                          
		                                          yes                     no                  is it his piece                 pass
		                                           |                      |                         |
		                     capture and update both pieces state    just place piece       -----------------------
		                                                                                    |                     |
		                                                                                    yes                  no
		                                                                                    |                     |
		                                                                            select the piece             pass
	"""

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.display.quit()
                pygame.quit()
                quit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                #'check','normal','checkmate'

                x_adjusted, y_adjusted = Helping_Class.convert_coordinate(
                    x_cor, y_cor, from_where='board')
                #print(x_adjusted/80,y_adjusted/80)
                print('situation', situation)
                if self.selected_from_selection_bar:

                    x_adjusted, y_adjusted = Helping_Class.convert_coordinate(
                        x_cor, y_cor, from_where='board')

                    temp_game_state = CP.game_data()
                    temp_game_state = copy.deepcopy(self.game_state)
                    data_convert = CP.Conversion_of_postion_name(
                        self.selected_piece,
                        Helping_Class.selection_bar_reverse_mapping[
                            self.selected_piece], (x_adjusted, y_adjusted))
                    temp_game_state.update(data_convert.piece,
                                           int(data_convert.i_pos_ani()),
                                           int(data_convert.f_pos_ani()))
                    temp_game_state.active_color = not temp_game_state.active_color
                    fen = temp_game_state.generate_fen()
                    board2 = chess.Board(fen=fen)
                    print(board2)
                    print(fen)
                    print('board2.is_check()', board2.is_check())
                    #now we need to place the piece on board

                    if self.game_state.board[
                            Helping_Class.convert_my_coordinate_to_anirudh(
                                x_adjusted, y_adjusted)] == None:
                        #print(self.selected_position)
                        if not board2.is_check():
                            if self._check_valid_position_(
                                    x_adjusted, y_adjusted):
                                self.place_piece_on_board_from_selection_bar(
                                    x_adjusted, y_adjusted)
                                #rajan's
                                #print(self.selected_piece)
                                #print(self.selected_position)
                                data_convert = CP.Conversion_of_postion_name(
                                    self.selected_piece,
                                    self.selected_position,
                                    (x_adjusted, y_adjusted))
                                self.game_state.update(
                                    data_convert.piece,
                                    int(data_convert.i_pos_ani()),
                                    int(data_convert.f_pos_ani()))
                                #self._where,self._to = (x_adjusted,y_adjusted),self.selected_position
                                self.selected_piece = None
                                self.selected_position = None
                            else:
                                pass
                    #board position is filled then nothing to do
                    else:
                        #if his piece change selection
                        self.selected_from_selection_bar = False
                        self.selected_from_board = True
                        self.selected_piece = self.anirudh_2_pritish[
                            self.game_state.board[
                                Helping_Class.convert_my_coordinate_to_anirudh(
                                    x_adjusted, y_adjusted)]]
                        self.selected_position = (x_adjusted, y_adjusted)

                elif self.selected_from_board:
                    #print('inside selection bar board option')
                    x_adjusted, y_adjusted = Helping_Class.convert_coordinate(
                        x_cor, y_cor, from_where='board')

                    omega = True
                    if self.selected_position:
                        if self.selected_position == (x_adjusted, y_adjusted):
                            omega = False
                    #print(self.selected_position,(x_adjusted,y_adjusted))
                    if omega:
                        move = self._check_valid_move_(x_adjusted, y_adjusted)
                        #print(move)
                    if omega:
                        if move:
                            #if move contains x then we have update state of captured piece
                            #else just update selected piece
                            #print("correct move")
                            self.capture_piece_update_board_or_place_piece(
                                move, x_adjusted, y_adjusted)

                        else:
                            #select the piece
                            if self.game_state.board[
                                    Helping_Class.
                                    convert_my_coordinate_to_anirudh(
                                        x_adjusted, y_adjusted)]:
                                self.selected_piece = self.anirudh_2_pritish[
                                    self.game_state.board[
                                        Helping_Class.
                                        convert_my_coordinate_to_anirudh(
                                            x_adjusted, y_adjusted)]]
                                self.selected_position = (x_adjusted,
                                                          y_adjusted)
                                self.selected_from_board = True

                else:

                    x_adjusted, y_adjusted = Helping_Class.convert_coordinate(
                        x_cor, y_cor, from_where='board')
                    if self.game_state.board[
                            Helping_Class.convert_my_coordinate_to_anirudh(
                                x_adjusted, y_adjusted)]:
                        #select the piece
                        if self.whose_move == 'white':
                            if 'W' in self.anirudh_2_pritish[
                                    self.game_state.board[
                                        Helping_Class.
                                        convert_my_coordinate_to_anirudh(
                                            x_adjusted, y_adjusted)]]:
                                self.selected_piece = self.anirudh_2_pritish[
                                    self.game_state.board[
                                        Helping_Class.
                                        convert_my_coordinate_to_anirudh(
                                            x_adjusted, y_adjusted)]]
                                self.selected_from_board = True
                                self.selected_position = (x_adjusted,
                                                          y_adjusted)
                            else:
                                #nothing to do
                                pass
                        elif self.whose_move == 'black':
                            if 'B' in self.anirudh_2_pritish[
                                    self.game_state.board[
                                        Helping_Class.
                                        convert_my_coordinate_to_anirudh(
                                            x_adjusted, y_adjusted)]]:
                                self.selected_piece = self.anirudh_2_pritish[
                                    self.game_state.board[
                                        Helping_Class.
                                        convert_my_coordinate_to_anirudh(
                                            x_adjusted, y_adjusted)]]
                                self.selected_from_board = True
                                self.selected_position = (x_adjusted,
                                                          y_adjusted)
                            else:
                                #nothing to do
                                pass
                    else:
                        #it is none means nothing is their so nothing to do
                        pass

            else:
                #print("not_pressed")
                pass