def start_game(self): builtins.cfg = \ chess_config.Config(self.config_name.lower(), self.crazy_mode) self.game._reset() agent1_module = __import__(self.agent1 + "_ChessPlayer") white_opponent_class = getattr(agent1_module, self.agent1 + "_ChessPlayer") self.white_opponent = white_opponent_class(self.game.board, 'white') agent2_module = __import__(self.agent2 + "_ChessPlayer") black_opponent_class = getattr(agent2_module, self.agent2 + "_ChessPlayer") self.black_opponent = black_opponent_class(self.game.board, 'black') self.game.white_player = self.agent1 self.game.black_player = self.agent2 self.player_time = {'white': 0.0, 'black': 0.0} self.timer = time.perf_counter() move = self.take_player_turn() self.attempt_to_make_move(self.game.board[move[0]], *move) while self.game.started: move = self.switch_player_turn() self.attempt_to_make_move(self.game.board[move[0]], *move)
import logging import builtins import chess_config if len(sys.argv) > 4: print("Usage: main_chess.py [debugLevel] [configFile] [crazy=false].") sys.exit(1) if len(sys.argv) > 1: logging.getLogger().setLevel(sys.argv[1]) else: logging.getLogger().setLevel('DEBUG') if len(sys.argv) > 3: crazy = (sys.argv[3][sys.argv[3].index('=')+1:] if '=' in sys.argv[3] else sys.argv[3]) crazy = crazy.lower() == 'true' else: crazy = False config_file_basename = sys.argv[2] if len(sys.argv) > 2 else 'reg' builtins.cfg = chess_config.Config(config_file_basename, crazy) import chess_view root = tk.Tk() chess_view.View(root) root.mainloop()
def start_game(self, event=None): if self.do_replay: builtins.cfg = \ chess_config.Config(self.saved_game['CONFIG'].lower(), True) cfg.START_POSITION = self.saved_game['STARTING_POS'] else: builtins.cfg = \ chess_config.Config(self.board_type_var.get().lower(), self.crazy_mode_var.get()) self.game._reset() width = cfg.NUM_COLS * cfg.SQUARE_WIDTH height = cfg.NUM_ROWS * cfg.SQUARE_HEIGHT self.canvas.config(width=width, height=height) self.draw_board() self.draw_pieces() if self.do_replay: self.game.white_player = self.saved_game['WHITE'] self.game.black_player = self.saved_game['BLACK'] game_type_text = (self.game.white_player + ' versus ' + self.game.black_player) elif self.game_type_var.get() == 'Human v Human': game_type_text = 'Human versus Human' self.game.white_player = 'Human' self.game.black_player = 'Human' elif self.game_type_var.get() == 'Human v Computer': bot_module = __import__(self.opponent1_type_var.get()) opponent_class = getattr(bot_module, self.opponent1_type_var.get()) self.black_opponent = opponent_class(self.game.board, 'black') b_name = self.opponent1_type_var.get().replace('_ChessPlayer', '') game_type_text = 'Human versus ' + b_name self.game.white_player = 'Human' self.game.black_player = b_name else: # Computer v Computer bot1_module = __import__(self.opponent1_type_var.get()) white_opponent_class = getattr(bot1_module, self.opponent1_type_var.get()) self.white_opponent = white_opponent_class(self.game.board, 'white') bot2_module = __import__(self.opponent2_type_var.get()) black_opponent_class = getattr(bot2_module, self.opponent2_type_var.get()) self.black_opponent = black_opponent_class(self.game.board, 'black') w_name = self.opponent1_type_var.get().replace('_ChessPlayer', '') b_name = self.opponent2_type_var.get().replace('_ChessPlayer', '') game_type_text = w_name + ' versus ' + b_name self.game.white_player = w_name self.game.black_player = b_name [c.grid_forget() for c in self.player_bar.winfo_children()] ttk.Label(self.player_bar, text=game_type_text, font=self.custom_font, anchor=tk.CENTER).grid(row=0, column=0, columnspan=3, sticky='WE') self.progress_value = tk.DoubleVar() self.progress_value.set(0.) self.progress_bar = ttk.Progressbar(self.player_bar, mode='determinate', variable=self.progress_value, orient=tk.HORIZONTAL) self.progress_bar.value = self.progress_value self.progress_bar.grid(row=1, columnspan=3, sticky='WE') self.progress_text = tk.StringVar() ttk.Label(self.player_bar, textvariable=self.progress_text, font=self.custom_font, anchor=tk.CENTER).grid(row=2, column=0, columnspan=3, sticky='WE') self.progress_stuff = collections.namedtuple( 'ProgressStuff', ['bar', 'text'])(self.progress_bar, self.progress_text) self.player_bar.rowconfigure(2, weight=1) self.player_bar.columnconfigure(1, weight=1) self.player_time = {'white': 0.0, 'black': 0.0} self.player_time_label = {} self.player_time_label['white'] = ttk.Label(self.player_bar, text='0.0 sec', font=self.custom_font, anchor=tk.CENTER, foreground='black', background='white') self.player_time_label['white'].grid(row=3, column=0, sticky='W') ttk.Label(self.player_bar, text='', font=self.custom_font, anchor=tk.CENTER, foreground='black').grid(row=3, column=1, sticky='WE') self.player_time_label['black'] = ttk.Label(self.player_bar, text='0.0 sec', font=self.custom_font, anchor=tk.CENTER, foreground='white', background='black') self.player_time_label['black'].grid(row=3, column=2, sticky='E') self.turn_label['text'] = 'White' self.timer = time.perf_counter() self.center() if not self.do_replay: self.take_player_turn()
sys.exit(2) arg_name, arg_val = arg.split('=') if arg_name not in [ x for x,_ in params ]: print("Unknown argument '{}'.".format(arg_name)) print_usage() sys.exit(3) setattr(this_module, arg_name, arg_val) for (param,default) in params: if not hasattr(this_module, param): setattr(this_module, param, default) logging.getLogger().setLevel(debug_level) builtins.cfg = chess_config.Config(config_file, crazy) if replay: with open(replay,'r') as f: saved_game = json.load(fp=f) import chess_view import tkinter as tk root = tk.Tk() the_view = chess_view.View(root) if int(replay_speed) < 300: print("(replay_speed minimum = 300, sorry.)") replay_speed = 300 the_view.replay(saved_game, replay_speed) root.mainloop() elif agent1 and agent2: from chess_headless import HeadlessGame