class BattleshipCLI(Cmd): prompt = 'battleship> ' intro = "{} \n Welcome to Battleship CLI! Type ? to list commands \n ".format(HEADER) logger = get_logger() ALL_SHIPS = list(Ship.get_ship_map().keys()) ALL_ALIGMENTS = ShipPosition.ALL ALL_COLS = GameBoard.COLS_NAMES ALL_ROWS = GameBoard.ROWS_NAMES def do_exit(self, inp): '''exit the application.''' self.logger.info("Bye! Thanks for playing!") return True def do_basic(self, _): print(GAMEPLAY) def help_basic(self, _): print("Battleship Gameplay") def do_intro(self, _): print(INSTRUCTIONS) def help_intro(self, _): print("Battleship CLI Instructions.") def do_p1(self, name): if not name: self.logger.error("Name cannot be empty.".format(name)) return None try: if str(self.player_2.name).lower() == str(name).lower(): self.logger.error("Name '{}' already taken for the Player 2".format(name)) return None except AttributeError: pass self.player_1 = Player(name=name) def help_p1(self): print("Set Player 1") def do_p2(self, name): if not name: self.logger.error("Name cannot be empty.".format(name)) return None try: if str(self.player_1.name).lower() == str(name).lower(): self.logger.error("Name '{}' already taken for the Player 1".format(name)) return None except AttributeError: pass self.player_2 = Player(name=name) def help_p2(self): print("Set Player 2") def do_start(self, _): try: self.game = Game( player_1=self.player_1, player_2=self.player_2, ) except AttributeError as exc: if 'player_1' in str(exc): self.logger.error("Game cannot start: You need to create the Player 1: p1 <name> .") elif 'player_2' in str(exc): self.logger.error("Game cannot start: You need to create the Player 2: p2 <name> .") except Exception as exc: self.logger.error("Error: {}".format(str(exc))) def help_start(self): print("Start game") def do_place_ship_p1(self, row_col_ship_aligment): row, col, ship, aligment = row_col_ship_aligment.split(' ') try: self.game.place_ships_for_player_1([ ShipPosition( row=row, col=col, ship=Ship.get_ship(ship), aligment=ShipPosition.get_aligment(aligment) ), ]) except AttributeError as exc: self.logger.error("Error: Game not started. Details: {}".format(str(exc))) except Exception as exc: self.logger.error("Error: {}".format(str(exc))) def help_place_ship_p1(self): print( ( "Place ships for Player 1: Format: row col ship aligment " "Example: place_ship_p1 A 1 carrier h" ) ) def do_place_ship_p2(self, row_col_ship_aligment): row, col, ship, aligment = row_col_ship_aligment.split(' ') try: self.game.place_ships_for_player_2([ ShipPosition( row=row, col=col, ship=Ship.get_ship(ship), aligment=ShipPosition.get_aligment(aligment) ), ]) except AttributeError as exc: self.logger.error("Error: Game not started. Details: {}".format(str(exc))) except Exception as exc: self.logger.error("Error: {}".format(str(exc))) def help_place_ship_p2(self): print( ( "Place ships for Player 2: Format: row col ship aligment " "Example: place_ship_p1 A 1 carrier h" ) ) def do_play(self, _): try: self.game.play() except AttributeError as exc: self.logger.error("Error: Game not started. Details: {}".format(str(exc))) except Exception as exc: self.logger.error("Error: {}".format(str(exc))) def help_play(self): print("Start shooting!") def do_shoot(self, row_col): row, col = row_col.split(' ') try: self.game.shoot(row, col) except AttributeError as exc: self.logger.error("Error: Game not started. Details: {}".format(str(exc))) except Exception as exc: self.logger.error("Error: {}".format(str(exc))) def help_shoot(self): print( ( "Shoot ships according to the turn. Format: row col " "Example: shoot A 1" ) ) def do_ls(self, resource): resource_map = { 'ships': self.ALL_SHIPS, 's': self.ALL_SHIPS, 'aligments': self.ALL_ALIGMENTS, 'a': self.ALL_ALIGMENTS, 'rows': self.ALL_ROWS, 'r': self.ALL_ROWS, 'cols': self.ALL_COLS, 'c': self.ALL_COLS, } try: print(resource_map[str(resource).lower()]) except KeyError as exc: self.logger.error("Resource not found: {}".format(str(exc))) except Exception as exc: self.logger.error("Error: {}".format(str(exc))) def help_ls(self): print( ( "List resource: Format: ls <resource> " "Example: ls ships" ) ) def do_stats(self, _): try: self.game.display_stats() except AttributeError as exc: self.logger.error("Error: Game not started. Details: {}".format(str(exc))) except Exception as exc: self.logger.error("Error: {}".format(str(exc))) def help_stats(self): print("Display game stats") def do_restart(self, _): try: self.game.restart() except AttributeError as exc: self.logger.error("Error: Game not started. Details: {}".format(str(exc))) except Exception as exc: self.logger.error("Error: {}".format(str(exc))) def help_restart(self): print("Restart the game, Using the same players.")
def main(): print("Starting the game") game = Game() game.play() print("Game Over")
def select_function(s_function): ''' input: s_function (string) output: [optional] usually a GameLog but really completely dynamic This string choses a way to: init Game(), param's for play(), possible return value.''' if s_function == "baseline": game = Game(s_instructions=ss) game.play(king_in_check_on=False) if s_function == "naive_check": game = Game(s_instructions=ss) game.play() if s_function == "test_copy": game = Game(s_instructions=ss) game.play(king_in_check_on=False, king_in_check_test_copy=True) if s_function == "test_copy_apply": game = Game(s_instructions=ss) game.play(king_in_check_on=False, king_in_check_test_copy_apply=True) if s_function == "test_c_apply_2": game = Game(s_instructions=ss) game.play(king_in_check_on=False, king_in_check_test_copy_apply_2=True) if s_function == "test_c_apply_3": game = Game(s_instructions=ss) game.play(king_in_check_on=False, king_in_check_test_copy_apply_3=True) if s_function == "test_c_apply_4": game = Game(s_instructions=ss) game.play(king_in_check_on=False, king_in_check_test_copy_apply_4=True) if s_function == "baseline_tt": game = Game(s_instructions=ss, b_log_turn_time=True, b_log_num_available=True) game.play(king_in_check_on=False) return game.get_gamelog() if s_function == "naive_check_tt": game = Game(s_instructions=ss, b_log_turn_time=True, b_log_num_available=True) game.play(king_in_check_on=True) return game.get_gamelog() if s_function == "baseline_long": game = Game(s_instructions=ss_long, b_log_turn_time=True, b_log_num_available=True) game.play(king_in_check_on=False) return game.get_gamelog() if s_function == "naive_long": game = Game(s_instructions=ss_long, b_log_turn_time=True, b_log_num_available=True) game.play(king_in_check_on=True) return game.get_gamelog() if s_function == "optimal1_long": game = Game(s_instructions=ss_long, b_log_turn_time=True, b_log_num_available=True) game.play(king_in_check_on=False, king_in_check_optimal=True) return game.get_gamelog() if s_function == "optimal2_long": game = Game(s_instructions=ss_long, b_log_turn_time=True, b_log_num_available=True) game.play(king_in_check_on=False, king_in_check_optimal_2=True) return game.get_gamelog() if s_function == "check_optimal": game = Game(s_instructions=ss) game.play(king_in_check_on=False, king_in_check_optimal=True) if s_function == "check_optimal_2": game = Game(s_instructions=ss) game.play(king_in_check_on=False, king_in_check_optimal_2=True) return None #to show that the function is no returning a test exit data
lose_count = 0 draw_count = 0 x_axis = [] y_axis = [] # Training iterations for i in range(0, iterations): if adjust_learn_rate == 1: p1.v.set_learn_rate(1 - i / iterations) printIteration(i, p1.v.get_est_function(), p1.v.get_learn_rate()) # Step 1: Create new game, p1 VS p2 g = Game() boards = g.play(op, False, p1, p2) board_features = [] # Step 2: Generate features for every board in the game for board in boards: board_features.append(f.get_features(board)) # Step 3: Using current Vop from p, estimates values for every board in the game train_values = p1.v.assign_train_value(board_features) # Step Aux: Assign values to statistics (b, vtrain) = train_values[-1] if vtrain == 1: win_count = win_count + 1 elif vtrain == -1: lose_count = lose_count + 1
run_type = str(sys.argv[1]) print '\nRunning: ', run_type, '\n' # run_type = 'randomplay2' #for debugging # run_type = 'replay' #for debugging # run_type = 'manyrandom' #for debugging if run_type == "interactive": ''' Interactive Mode ''' game = Game(manual_control = (1,0) ,b_display_show_opponent = True ,b_log_move = True ) game.play() elif run_type == "randomplay": game = Game(manual_control = () ,b_display_never_print = True ,b_log_move = False ,test_exit_moves = 1000 ) ret = game.play() if game.i_turn == 1000: print '1000 turn game' else:
class Rummy(tk.Tk): def __init__(self): super(Rummy, self).__init__() # Initialise a game self.game = Game() # Member Variables self.number = tk.IntVar(self) self.number.set(VALID_NUMBERS[0]) self.colour = tk.StringVar(self) self.colour.set(VALID_COLOURS[0]) self.title_size = 14 # window dimensions self.geometry("1000x500") # Create title self.title("Rummy-O Calculator") # Create Menus root_menu = tk.Menu(self) self.config(menu=root_menu) file_menu = tk.Menu(root_menu) root_menu.add_cascade(label="File", menu=file_menu) file_menu.add_command(label="Reset Game", command=self.reset_game) file_menu.add_command(label="Refresh Screen", command=self.refresh) file_menu.add_command(label="Test Mode", command=self.test_mode) file_menu.add_command(label="Exit", command=self.quit) help_menu = tk.Menu(root_menu) root_menu.add_cascade(label="Help", menu=help_menu) help_menu.add_command(label="Open Instructions", command=self.open_instructions) # Create Main Frames self.left_frame = tk.Frame(self, height=500) self.left_frame.pack(side=tk.LEFT, fill=tk.Y) self.right_frame = tk.Frame(self) self.right_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) ## Left side of the screen self.l1 = tk.Frame(self.left_frame, height=150, width=500, highlightbackground="black", highlightthickness=2) self.l1.pack(side=tk.TOP, anchor=tk.N, fill=tk.X) self.l2 = tk.Frame(self.left_frame, height=150, width=500) self.l2.pack(side=tk.TOP, anchor=tk.N, expand=True, fill=tk.BOTH) self.l3 = tk.Frame(self.left_frame, height=200, width=500) self.l3.pack(fill=tk.Y, expand=True) self.l1_left = tk.Frame(self.l1, height=150, width=300) self.l1_left.pack(side=tk.LEFT, fill=tk.X) self.l1_right = tk.Frame(self.l1, height=150, width=200) self.l1_right.pack(side=tk.LEFT, fill=tk.X, padx=10) # Frames for the Labels self.l1_left_left = tk.Frame(self.l1_left, height=150, width=100, padx=20) self.l1_left_left.pack(side=tk.LEFT) self.number_label = tk.Label(self.l1_left_left, text="Tile Number", font=("Helvetica", self.title_size)) self.number_label.pack(side=tk.TOP) self.choose_number = tk.OptionMenu(self.l1_left_left, self.number, *VALID_NUMBERS) self.choose_number.config(width=10) self.choose_number.pack(side=tk.TOP) self.l1_left_right = tk.Frame(self.l1_left, height=150, width=100, padx=20) self.l1_left_right.pack(side=tk.LEFT) self.number_label = tk.Label(self.l1_left_right, text="Tile Colour", font=("Helvetica", self.title_size)) self.number_label.pack(side=tk.TOP) self.choose_number = tk.OptionMenu(self.l1_left_right, self.colour, *VALID_COLOURS) self.choose_number.config(width=10) self.choose_number.pack(side=tk.LEFT) # Add the buttons to the right side of l1 self.add_board_button = tk.Button(self.l1_right, text="Add to Board", font=("Helvetica", 12), command=self.add_to_board) self.add_board_button.pack(fill=tk.X, pady=5, expand=True) self.draw_button = tk.Button(self.l1_right, text="Add to Hand", command=self.draw, font=("Helvetica", 12)) self.draw_button.pack(fill=tk.X, expand=True) self.play_button = tk.Button(self.l1_right, text="Play From Hand", command=self.play, font=("Helvetica", 12)) self.play_button.pack(fill=tk.X, pady=5, expand=True) # Add row for remove from board self.l21 = tk.Frame(self.l1) self.l21.pack(side=tk.LEFT) self.remove_from_board_button = tk.Button( self.l21, text="Remove from Board", command=self.remove_from_board, font=("Helvetica", 12)) self.remove_from_board_button.pack(padx=10, pady=10) self.remove_from_hand_button = tk.Button(self.l21, text="Remove from " "Hand", command=self.remove_from_hand, font=("Helvetica", 12)) self.remove_from_hand_button.pack(padx=10, pady=10) # Players Hand Text self.players_hand_title = tk.Label(self.l2, text="Player's Hand", font=("Helvetica", self.title_size)) self.players_hand_title.pack(fill=tk.X) self.players_hand = tk.Text(self.l2, height=3) self.players_hand.pack(fill=tk.BOTH, expand=True) # Board Text self.board_title = tk.Label(self.l3, text="Tiles on the Board", font=("Helvetica", self.title_size)) self.board_title.pack(fill=tk.X) self.board = tk.Text(self.l3, height=5) self.board.pack(fill=tk.BOTH, expand=True) # Right side of the board self.r1 = tk.Frame(self.right_frame) self.r1.pack(fill=tk.X) self.r11 = tk.Frame(self.r1) self.r11.pack() # Best Move and possible moves self.best_move_button = tk.Button(self.r11, text="Best Move", command=self.best_move, font=("Helvetica", 12)) self.best_move_button.pack(side=tk.LEFT, padx=10, pady=10) self.possible_sets_button = tk.Button(self.r11, text="All Possible " "Sets", command=self.best_move, font=("Helvetica", 12)) self.possible_sets_button.pack(side=tk.LEFT, padx=10, pady=10) # Set Instructions self.set_instructions_title = tk.Label(self.right_frame, text="Set " "Instructions", font=("Helvetica", self.title_size)) self.set_instructions_title.pack(fill=tk.X) self.set_instructions = tk.Text(self.right_frame, height=5) self.set_instructions.pack(fill=tk.BOTH, expand=True) # Test mode buttons self.random_board_tile = tk.Button(self.l21, text="Rnd Tile Board", command=self.add_random_tile_board, font=("Helvetica", 12)) self.random_player_tile = tk.Button( self.l21, text="Rnd Tile Player", command=self.add_random_tile_player, font=("Helvetica", 12)) # Refresh all the screens for any pre-existing tiles self.refresh() def reset_game(self): """Resets the game. Nothing on the board and nothing in the players hand""" self.game = Game() self.refresh() @staticmethod def open_instructions(): """Opens up the github page with the instructions for the program""" webbrowser.open('https://github.com/sbrn3/Rummy-O-Calculator') def add_to_board(self): """Adds the currently selected number and colour as a tile to the board""" self.game.add_to_board(self.colour.get(), self.number.get()) self.refresh() def draw(self): """Adds the currently input tile to the player's hand""" self.game.draw(self.colour.get(), self.number.get()) self.refresh() def play(self): """Removes the tile from the players hand and adds it to the board""" self.game.play(self.colour.get(), self.number.get()) self.refresh() def remove_from_board(self): """Removes the specified tile from the board""" self.game.remove_from_board(self.colour.get(), self.number.get()) self.refresh() def remove_from_hand(self): self.game.remove_from_hand(self.colour.get(), self.number.get()) self.refresh() def update_hand(self): hand = str(self.game.view_hand()).replace("[", "").replace("]", "") print("Hand: {0}".format(hand)) self.players_hand.config(state=tk.NORMAL) self.players_hand.delete(1.0, tk.END) self.players_hand.insert(tk.END, hand) self.players_hand.config(state=tk.DISABLED) def update_board(self): hand = str(self.game.view_board()).replace("[", "").replace("]", "") self.board.config(state=tk.NORMAL) self.board.delete(1.0, tk.END) self.board.insert(tk.END, hand) self.board.config(state=tk.DISABLED) def refresh(self): print("Screen refreshed") self.update_board() self.update_hand() def loading(self): self.set_instructions.config(state=tk.NORMAL) self.set_instructions.delete(1.0, tk.END) self.set_instructions.insert(tk.END, "Loading.....") self.set_instructions.config(state=tk.DISABLED) def best_move(self): self.loading() try: hand, sets = self.game.best_move() output = "Tiles from hand:\n{0}\n".format(hand) output += "Form the following sets:\n" for i in range(len(sets)): output += str(sets[i]) if i == len(sets) - 1: continue else: output += "\n" except TypeError: output = "There are no options. Draw a piece from the deck." self.set_instructions.config(state=tk.NORMAL) self.set_instructions.delete(1.0, tk.END) self.set_instructions.insert(tk.END, output) self.set_instructions.config(state=tk.DISABLED) def possible_sets(self): pass def test_mode(self): self.random_board_tile.pack() self.random_player_tile.pack() # Add test tiles to board self.game.add_to_board("black", 1) self.game.add_to_board("black", 2) self.game.add_to_board("black", 3) self.game.add_to_board("black", 7) self.game.add_to_board("red", 7) self.game.add_to_board("blue", 7) self.refresh() def add_random_tile_board(self): self.game.add_to_board(self.game.generate_random_tile()) self.refresh() def add_random_tile_player(self): self.game.draw(self.game.generate_random_tile()) self.refresh()