class Controller(Tk): """ Top level GUI class, catches inputs from the user and dispatches the appropriate requests to the model and vies classes """ def __init__(self, *args, **kwargs): Tk.__init__(self, *args, **kwargs) self.bind('<Key>', self.key_event) self.view = BoardCanvas(self) self.minsize(400,400) self.view.place(relwidth=1.0, relheight=1.0) self.model = ChessGame() def move_submit(self, o, d): self.model.play_move(o, d) self.view.draw_position() def key_event(self, event): if event.char == ' ': self.model.setup_problem() self.view.board = self.model.board self.view.draw_position() else: do_depth_two(self.model) def run(self): self.view.board = self.model.board self.mainloop()
def __init__(self, *args, **kwargs): Tk.__init__(self, *args, **kwargs) self.bind('<Key>', self.key_event) self.view = BoardCanvas(self) self.minsize(400,400) self.view.place(relwidth=1.0, relheight=1.0) self.model = ChessGame()
class AppWindow(pyglet.window.Window): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.game = ChessGame("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR", 0, "KQkq", []) pyglet.clock.schedule_interval(self.update, 1.0 / 120.0) def update(self, dt): pass def on_mouse_motion(self, x, y, button, modifiers): pass def on_draw(self): app_window.clear() self.game.on_draw() def on_mouse_press(self, x, y, button, modifiers): self.game.on_click(x, y)
def crunch_data(relative_data_file_name, relative_training_data_file): opening_book = {} data = PGNParser(BASE_DIR + relative_data_file_name) game_list = data.get_games() for game in game_list: if game is not None: first_moves = game[0], game[1], game[2] if opening_book.get(first_moves) is None: tmp = [game] opening_book[first_moves] = tmp else: opening_book[first_moves].append(game) response_map = {} correct_move_map = {} data_file = open(BASE_DIR + relative_training_data_file, 'wb') field_names = ['position', 'response', 'correct_move'] writer = csv.DictWriter(data_file, fieldnames=field_names) writer.writeheader() for move_list in game_list: new_game = ChessGame() for move_number in range(0, len(move_list), 2): try: move = move_list[move_number] new_game.try_move(move) position = new_game.get_position() response = move_list[move_number + 1] correct_move = move_list[move_number + 2] if response_map.get(position) is None: tmp = [response] response_map[position] = tmp tmp2 = [correct_move] correct_move_map[position] = tmp2 else: response_map.get(position).append(response) correct_move_map.get(position).append(correct_move) response = move_list[move_number + 1] new_game.try_move(response) except IndexError: print "Finished list with last choice", move_list[move_number - 1] except: return for position in response_map.keys(): writer.writerow({'position': position, 'response': ", ".join(response_map.get(position)), 'correct_move': ", ".join(correct_move_map.get(position))})
def startGUIMode(debug=False): root = tkinter.Tk() root.title('Chess') gui = ChessGUI(8, root) chess = ChessGame() terminalObserver = TerminalChessObserver(chess._board) chess.addObserver(gui) chess.addObserver(terminalObserver) chess._board.addObserver(gui.boardCanvas) chess._board.addObserver(terminalObserver) game = GUIChessController(gui, chess, root, debug) game.play() root.resizable(False, False) root.mainloop()
#!/usr/bin/env python # -*- coding: utf-8 -*- from chessgame import ChessGame if __name__ == '__main__': game = ChessGame() game.start(computerstarts=True) # position = (0,1) # print game.chessboard.piece(position).possible_moves(game.chessboard, position, 'black') # print game.chessboard.evaluate('black') # game.chessboard.all_possible_new_boards('white') # print game.chessboard.getadjecentposition( position, 'SW' ) # print game.chessboard.piece(position).possible_moves(position, game.chessboard.color(position))
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.game = ChessGame("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR", 0, "KQkq", []) pyglet.clock.schedule_interval(self.update, 1.0 / 120.0)
"""Main entry point into the game""" from chessgame import ChessGame if __name__ == '__main__': current_game = ChessGame(3) print("Lets play chess!!! Here is the board:\n") current_game.see_board() print('\n') while not current_game.game_over: print("Your turn: ") start_point = raw_input("Enter starting point coordinate: ") end_point = raw_input("Enter ending point coordinate: ") current_game.make_move(start_point, end_point) print("You have made a move!\n") current_game.see_board() print('\n') if current_game.current_turn == 'b': print("AI is thinking...") current_game.make_move_ai(3) print('AI has made a move!\n\n') current_game.see_board() print('\n')
from chessgame import ChessGame game = ChessGame(start_pos="8/5R1p/6R1/8/8/8/7k/8 w - - 0 1") print(game.current_position_score()) print(game.recommend_move()) game.make_move("f7", "h7") print(game.current_position_score())
#!/usr/bin/env python # -*- coding: utf-8 -*- from chessgame import ChessGame if __name__ == '__main__': game = ChessGame() game.start(computerstarts = True) # position = (0,1) # print game.chessboard.piece(position).possible_moves(game.chessboard, position, 'black') # print game.chessboard.evaluate('black') # game.chessboard.all_possible_new_boards('white') # print game.chessboard.getadjecentposition( position, 'SW' ) # print game.chessboard.piece(position).possible_moves(position, game.chessboard.color(position))
"""Main entry point into the game""" from chessgame import ChessGame if __name__ == '__main__': new_game = ChessGame()
def startTerminalMode(debug=False): chess = ChessGame() chess.addObserver(TerminalChessObserver(chess._board)) game = TerminalChessGame(chess, debug) game.play()
"""Main entry point into the game""" import json import sys from chessgame import ChessGame if __name__ == '__main__': cont = True invalidPQ = [] payload = json.loads(sys.argv[1]) while cont: current_game = ChessGame(payload, invalidPQ) json_data = current_game.make_move_ai(payload['level']) if json_data[0] == '{': print(json_data) cont = False else: invalidPQ = json_data
class ChessApp: sprites = {} sounds = {} def __init__(self, theme="Theme1", winstyle=0): mixer.pre_init(44100, -16, 1, 512) pygame.init() best_depth = pygame.display.mode_ok(SCREENRECT.size, winstyle, 32) self.screen = pygame.display.set_mode(SCREENRECT.size, winstyle, best_depth) with open("img/themes.json", 'r') as fh: theme_data = json.load(fh) theme_data = theme_data[theme] # chess board self.sprites['board'] = load_image('img/' + theme_data['filename']) # white pieces self.sprites['K_white'] = load_image('img/king_white.png') self.sprites['Q_white'] = load_image('img/queen_white.png') self.sprites['B_white'] = load_image('img/bishop_white.png') self.sprites['N_white'] = load_image('img/knight_white.png') self.sprites['R_white'] = load_image('img/rook_white.png') self.sprites['p_white'] = load_image('img/pawn_white.png') self.sprites['DK_white'] = load_image('img/king_white_dead.png') # black pieces self.sprites['K_black'] = load_image('img/king_black.png') self.sprites['Q_black'] = load_image('img/queen_black.png') self.sprites['B_black'] = load_image('img/bishop_black.png') self.sprites['N_black'] = load_image('img/knight_black.png') self.sprites['R_black'] = load_image('img/rook_black.png') self.sprites['p_black'] = load_image('img/pawn_black.png') self.sprites['DK_black'] = load_image('img/king_black_dead.png') self.sprites['Check'] = load_image('img/check.png') self.sprites['Promotion'] = load_image('img/promotion.png') self.white_highlight = theme_data['highlight_color_light'] self.black_highlight = theme_data['highlight_color_dark'] self.overlay = pygame.Surface((SCREENRECT.size), pygame.SRCALPHA) self.check = 0 self.turn_board = False self.promotion_pieces = { 0: 'Queen', 1: 'Rook', 2: 'Bishop', 3: 'Knight' } self.selected_promotion_piece = 0 self.sounds['move'] = mixer.Sound('snd/move.wav') def get_rect(self, position): row, col = position if self.turn_board and self.game.current_player.color == 'black': pos_rect = Rect(560 - col * 80, row * 80, 80, 80) else: pos_rect = Rect(col * 80, 560 - row * 80, 80, 80) return pos_rect def draw_board(self, highlight_fields=None): # get current setup board = self.game.get_board() # show checkerboard self.screen.fill((255, 64, 64)) self.screen.blit(self.sprites['board'], Rect(0, 0, 640, 640)) self.overlay.fill((0, 0, 0, 0)) promotion = board.promotion if promotion: promotion_pos = promotion[1] pos_rect = self.get_rect(promotion_pos) self.overlay.blit(self.sprites['Promotion'], pos_rect) if self.check: # get current king position king_position = board.king_positions[ self.game.current_player.color] else: king_position = None # fill in highlighted fields if highlight_fields is not None: for field_pos in highlight_fields: row, col = field_pos field = board.get((row, col)) pos_rect = self.get_rect((row, col)) if field.color == 'white': self.overlay.fill(self.white_highlight, pos_rect) else: self.overlay.fill(self.black_highlight, pos_rect) self.screen.blit(self.overlay, (0, 0)) for row in range(board.col_size): for col in range(board.row_size): field = board.get((row, col)) if field.occupied: piece = field.get() if self.turn_board and self.game.current_player.color == 'black': pos_rect = Rect(560 - col * 80, row * 80, 80, 80) else: pos_rect = Rect(col * 80, 560 - row * 80, 80, 80) if (row, col) == king_position: if self.check == 2: self.screen.blit(self.sprites['Check'], pos_rect) elif self.check == 3: self.screen.blit(self.sprites['DK_' + piece.color], pos_rect) continue self.screen.blit( self.sprites[piece.short_name + '_' + piece.color], pos_rect) pygame.display.flip() def run(self, winstyle=0): """Initialize game.""" self.game = ChessGame() self.draw_board() # show on screen self.wait_for_input() def wait_for_input(self): """Get user input.""" board = self.game.get_board() highlighted_fields = [] current_mode = 0 state = 0 while True: ev = pygame.event.get() # proceed events for event in ev: # handle MOUSEBUTTONUP if event.type == pygame.MOUSEBUTTONDOWN: pos = pygame.mouse.get_pos() if current_mode == 2: if event.button == 4: self.selected_promotion_piece = ( self.selected_promotion_piece + 1) % 4 self.game.choose_promotion(self.promotion_pieces[ self.selected_promotion_piece]) elif event.button == 5: self.selected_promotion_piece = ( self.selected_promotion_piece - 1) % 4 self.game.choose_promotion(self.promotion_pieces[ self.selected_promotion_piece]) elif event.button == 1: self.check = self.game.choose_promotion( self.promotion_pieces[ self.selected_promotion_piece], final=True) current_mode = 0 self.draw_board() self.draw_board() elif event.button == 1: inverted = self.turn_board and self.game.current_player.color == 'black' if event.type == pygame.MOUSEBUTTONUP and event.button == 1: clicked_row, clicked_col = self.position_to_field( pos, inverted) field = board.get((clicked_row, clicked_col)) if current_mode == 0: if field.occupied: piece = field.get() if piece.color == self.game.current_player.color: from_row = clicked_row from_col = clicked_col highlighted_fields.append( (clicked_row, clicked_col)) self.draw_board(highlighted_fields) current_mode = 1 elif current_mode == 1: # deselect piece? if not (clicked_row == from_row and clicked_col == from_col): state = self.game.move((from_row, from_col), (clicked_row, clicked_col)) if state == 0: continue else: self.sounds['move'].play() if state == 1: self.check = 0 elif state == 4: # choose promotion current_mode = 2 self.selected_promotion_piece = 0 self.game.choose_promotion( self.promotion_pieces[ self.selected_promotion_piece]) highlighted_fields = [] self.draw_board(highlighted_fields) continue else: self.check = state highlighted_fields = [] current_mode = 0 self.draw_board(highlighted_fields) if event.type == pygame.QUIT: pygame.quit() sys.exit() @staticmethod def position_to_field(pos, inverted): """Determine field based on position.""" if inverted: row = int(pos[1] / 80) col = int((640 - pos[0]) / 80) else: row = int((640 - pos[1]) / 80) col = int(pos[0] / 80) return (row, col)
def run(self, winstyle=0): """Initialize game.""" self.game = ChessGame() self.draw_board() # show on screen self.wait_for_input()