def test_hit_check_miss(self): player = fy.Players("Player") ship = fy.Ship("Ship", 1, [(1, 2)]) player.add_ship(ship) hit, kill = fy.hit_check(1, 1, player) self.assertFalse(hit) self.assertFalse(kill)
def test_hit_check_hit_no_kill(self): player = fy.Players("Player") ship = fy.Ship("Ship", 2, [(1, 2), (2, 2)]) player.add_ship(ship) hit, kill = fy.hit_check(1, 2, player) self.assertTrue(hit) self.assertFalse(kill)
def shot(column: int, row: int): try: column_num = int(column) row_num = int(row) except ValueError: # Catches if the value given is not a number return None # Decides which players ships to look through if 0 <= column_num < 10: player = player1 else: player = player2 # Creating the button to to show if a hit or miss if 0 <= column_num <= 20 and 1 <= row_num <= 10 and column_num != 10: hit, kill = fy.hit_check(column_num, row_num, player) if hit: relief_new, color_new = gui.button_state_all("hit") # Updating the score box if the shot results in a kill if player == player1: gui.score_box(player.get_score(), player2.get_score(), player1.name, player2.name) else: gui.score_box(player1.get_score(), player.get_score(), player1.name, player2.name) if kill: gui.shot_label("kill") else: gui.shot_label("hit") else: relief_new, color_new = gui.button_state_all("miss") gui.shot_label("miss") # Adding the button so that it shows if the shot hit or missed hit_button = gui.tkinter.Button(gui.mainWindow, text=f"({column},{row})", highlightbackground=color_new, relief=relief_new, state="disabled") hit_button.grid(row=row, column=column, sticky="nsew") # Resetting the entry widgets to be blank gui.resultRow.delete(0, 'end') gui.resultCol.delete(0, 'end') check_lost(player)
def play(player_a: fy.AutoPlayers, player_b: fy.AutoPlayers, ships: int): global turns_a global turns_b global turns_list build_player_ships(player_a, player_b, ships) player_turn = "a" # Bounds for the player to shoot player_a_bounds = (11, 20, 1, 10) player_b_bounds = (0, 9, 1, 10) player_a_prev = "Miss" player_b_prev = "Miss" player_a_list_to_use = list() player_b_list_to_use = list() # Shoots a shot, based off strategy. Checks if the shot is a hit, then checks if the other player lost. # If the other player lost, winning players turns get added to turns list # The ratio of ships alive to total ships of the winning player gets added to ratio list while True: if player_turn == "a": first = player_a.get_init_hit() second = player_a.get_second_hit() if first: if not second: player_a_list_to_use = player_a.around_init_hit() elif second and player_a_prev == 'Hit': player_a_list_to_use = player_a.second_hit_direction() elif second and player_a_prev == 'Miss': player_a_list_to_use = player_a.second_hit_anti_direction() player_a.set_direction_list(player_a_list_to_use) x, y = strat.strategy_choose(player_a.get_strategy(), player_a_bounds, player_a_list_to_use) h, k = fy.hit_check(x, y, player_b) if h and not k: player_a_prev = "Hit" if not first: player_a.an_init_hit((x, y)) if first and not second: player_a.a_second_hit((x, y)) elif k: player_a_prev = "Miss" player_a.reset_auto() player_a_list_to_use = list() else: player_a_prev = "Miss" if lost(player_b): add_stats(player_a, ships) break player_turn = "b" else: first = player_b.get_init_hit() second = player_b.get_second_hit() if first: if not second: player_b_list_to_use = player_b.around_init_hit() elif second and player_b_prev == 'Hit': player_b_list_to_use = player_b.second_hit_direction() elif second and player_b_prev == 'Miss': player_b_list_to_use = player_b.second_hit_anti_direction() x, y = strat.strategy_choose(player_b.get_strategy(), player_b_bounds, player_b_list_to_use) h, k = fy.hit_check(x, y, player_a) if h and not k: player_b_prev = "Hit" if not first: player_b.an_init_hit((x, y)) if first and not second: player_b.a_second_hit((x, y)) elif h and k: player_b_prev = "Miss" player_b.reset_auto() player_b_list_to_use = list() else: player_b_prev = "Miss" if lost(player_a): add_stats(player_b, ships) break player_turn = "a" # Resets these values so for bookkeeping turns_a = 0 turns_b = 0 strat.coordinates_shot = list()