Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implementing AI into already done battleship python code import itertools from . import game_config, player class Game(object): def __init__(self, game_config_file: str, num_players: int = 2)

Implementing AI into already done battleship python code

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

import itertools from . import game_config, player

class Game(object):

def __init__(self, game_config_file: str, num_players: int = 2) -> None: super().__init__() self.game_config = game_config.GameConfig(game_config_file) self.players = [] self.player_turn = 0 self.setup_players(num_players)

def setup_players(self, num_players: int) -> None: for player_num in range(1, num_players + 1): self.players.append(player.Player(player_num, self.game_config, self.players))

def play(self) -> None: active_player = self.players[0] for active_player in itertools.cycle(self.players): self.do_current_players_turn(active_player) if self.game_is_over(): break print(f'{active_player} won the game!')

def do_current_players_turn(self, cur_player: player.Player) -> None: self.display_gamestate(cur_player) while True: move = cur_player.get_move() move.make() if move.ends_turn(): break

@property def num_players(self) -> int: return len(self.players)

def get_active_player(self) -> player.Player: return self.players[self.player_turn]

def game_is_over(self) -> bool: return any(player_.all_ships_sunk() for player_ in self.players)

def display_gamestate(self, cur_player: player.Player) -> None: cur_player.display_scanning_boards() cur_player.display_firing_board()

Problem You are to implement three different Als that can play the game of Battleship from the previous assignment. Starter Code You may start this project using either your code from the previous assignment or my solution. My solution will be available to download from the previous Battleship project on Mimir after the last late day has passed. Requirements Your output MUST match mine exactly to receive credit You must have at least the following classes o Player o HumanPlayer o AlPlayer o CheatingAI o SearchDestroyal o Randomai Each class must be in its own file All functions/methods must have type hints on their parameters and the return type You MUST use inheritance in a meaningful way in your solution. If you do not you will NOT receive any credit on this assignment You are of course allowed to have more than these classes but you must have the ones specified and they must be named exactly as above. Setting Up The Game Please read this section carefully. If you deviate at all from the instructions below your solution will not match the output solution. Initializing the random number generator Your program will now receive two command line parameters. The first will still be the path to the configuration file and the second will be the seed to the random number generator. The seed to the random number generator is used to initialize the random number generator and make sure that we both get the same random numbers. To seed the random number generator use random.seed with the second command-line argument. Before passing sys.argv[2] make sure to convert it to an integer by using int first. If you fail to convert it to an integer random.seedwill still accept it but you will get different values than mine Getting The Player's Type At the beginning of the game, you will ask each player what type of player they want to be. Valid options are prefixes of Human Cheatingai SearchDestroyAi Randomai Placing Ships All Als place their ships the same way. Here is their method 1. For each ship the player has 1. Choose its orientation by using random.choiceto pick from among the list ['horizontal', "vertical' ] 2. Based on the ships orientation pick a starting coordinate where the ship could fit onto the board 1. Choose a row randomly using random.randint 2. Choose a column randomly using random.randint 3. If the ship overlaps with another ship repeat the steps for placing the ship beginning at 1.1 I have provided an implementation of the above in the starter code. Please use it so you don't end up wasting your time implementing it incorrectly. Als Placing Ships All Als place their ships in the same way. Cheating AI The Cheating Al plays a perfect game of Battleship. It never misses and always hits its opponent's ships. Ai Name Cheating AI {player_number}. So if the Cheating Al is the first player its name will be Cheating Al 1. If it is the second player its name is Cheating Al 2. Getting Moves The Al selects positions that contain ships from the top of the board to the bottom and then left to right. So if the opponent's board looked like 0 1 2 3 O * * SB 1 AASB 2 * * * B 3 * * * B The ai would fire at these locations in this order: (0,2), (0.3 (1.0), (1,1), (1,2), (1,3), (2,3), (3,3) Random AI The Random Al guesses coordinates to fire at randomly. Ai Name Random AI {player_number}. So if the Cheating Al is the first player its name will be Random Al 1. If it is the second player its name is Random Al 2. On Creation When the random Al is generated you should generate a list of possible locations to fire at from the upper left- hand corner to the bottom right-hand corner. For example, if your board was Getting Moves Choose a location to fire at from your list of firing locations using random.choice and then remove that item. Search Destroy AI The Search Destroy Al operates in one of two modes: Search or Destroy. While in search mode it randomly guesses to fire at just like the Random Al does. When it gets a hit however it switches to Destroy mode. In Destroy Mode it will shoot to the left, above, right, and below the position that it hit. If it got any hits doing this it will repeat the process. It will switch back to Search Mode after it has finished shooting around all of the locations it has hit and hasn't found any more hits. Ai Name Seach Destroy AI {player number}. So if the Seach Destroy Al is the first player its name will be Seach Destroy Al 1. If it is the second player its name is Search Destroy Al 2. On Creation Like the Random Ai create a list of all possible locations you can fire at Getting a Move If you are in Search mode you behave like a Random Also follow the same directions as for Random Al. If a hit is scored, you add the locations to the left, up, right, down of the location hit to the destroy list and switch to destroy mode. If you are in destroy mode you shoot at the first location in the destroy list and if a hit is scored you add the surrounding locations to the destroy list. Here is an example of a Search Destroy Al attacking Problem You are to implement three different Als that can play the game of Battleship from the previous assignment. Starter Code You may start this project using either your code from the previous assignment or my solution. My solution will be available to download from the previous Battleship project on Mimir after the last late day has passed. Requirements Your output MUST match mine exactly to receive credit You must have at least the following classes o Player o HumanPlayer o AlPlayer o CheatingAI o SearchDestroyal o Randomai Each class must be in its own file All functions/methods must have type hints on their parameters and the return type You MUST use inheritance in a meaningful way in your solution. If you do not you will NOT receive any credit on this assignment You are of course allowed to have more than these classes but you must have the ones specified and they must be named exactly as above. Setting Up The Game Please read this section carefully. If you deviate at all from the instructions below your solution will not match the output solution. Initializing the random number generator Your program will now receive two command line parameters. The first will still be the path to the configuration file and the second will be the seed to the random number generator. The seed to the random number generator is used to initialize the random number generator and make sure that we both get the same random numbers. To seed the random number generator use random.seed with the second command-line argument. Before passing sys.argv[2] make sure to convert it to an integer by using int first. If you fail to convert it to an integer random.seedwill still accept it but you will get different values than mine Getting The Player's Type At the beginning of the game, you will ask each player what type of player they want to be. Valid options are prefixes of Human Cheatingai SearchDestroyAi Randomai Placing Ships All Als place their ships the same way. Here is their method 1. For each ship the player has 1. Choose its orientation by using random.choiceto pick from among the list ['horizontal', "vertical' ] 2. Based on the ships orientation pick a starting coordinate where the ship could fit onto the board 1. Choose a row randomly using random.randint 2. Choose a column randomly using random.randint 3. If the ship overlaps with another ship repeat the steps for placing the ship beginning at 1.1 I have provided an implementation of the above in the starter code. Please use it so you don't end up wasting your time implementing it incorrectly. Als Placing Ships All Als place their ships in the same way. Cheating AI The Cheating Al plays a perfect game of Battleship. It never misses and always hits its opponent's ships. Ai Name Cheating AI {player_number}. So if the Cheating Al is the first player its name will be Cheating Al 1. If it is the second player its name is Cheating Al 2. Getting Moves The Al selects positions that contain ships from the top of the board to the bottom and then left to right. So if the opponent's board looked like 0 1 2 3 O * * SB 1 AASB 2 * * * B 3 * * * B The ai would fire at these locations in this order: (0,2), (0.3 (1.0), (1,1), (1,2), (1,3), (2,3), (3,3) Random AI The Random Al guesses coordinates to fire at randomly. Ai Name Random AI {player_number}. So if the Cheating Al is the first player its name will be Random Al 1. If it is the second player its name is Random Al 2. On Creation When the random Al is generated you should generate a list of possible locations to fire at from the upper left- hand corner to the bottom right-hand corner. For example, if your board was Getting Moves Choose a location to fire at from your list of firing locations using random.choice and then remove that item. Search Destroy AI The Search Destroy Al operates in one of two modes: Search or Destroy. While in search mode it randomly guesses to fire at just like the Random Al does. When it gets a hit however it switches to Destroy mode. In Destroy Mode it will shoot to the left, above, right, and below the position that it hit. If it got any hits doing this it will repeat the process. It will switch back to Search Mode after it has finished shooting around all of the locations it has hit and hasn't found any more hits. Ai Name Seach Destroy AI {player number}. So if the Seach Destroy Al is the first player its name will be Seach Destroy Al 1. If it is the second player its name is Search Destroy Al 2. On Creation Like the Random Ai create a list of all possible locations you can fire at Getting a Move If you are in Search mode you behave like a Random Also follow the same directions as for Random Al. If a hit is scored, you add the locations to the left, up, right, down of the location hit to the destroy list and switch to destroy mode. If you are in destroy mode you shoot at the first location in the destroy list and if a hit is scored you add the surrounding locations to the destroy list. Here is an example of a Search Destroy Al attacking

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems Uropean Conference Adbis 2020 Lyon France August 25 27 2020 Proceedings Lncs 12245

Authors: Jerome Darmont ,Boris Novikov ,Robert Wrembel

1st Edition

3030548317, 978-3030548315

More Books

Students also viewed these Databases questions

Question

1. Identify three approaches to culture.

Answered: 1 week ago

Question

3. Identify and describe nine cultural value orientations.

Answered: 1 week ago

Question

4. Describe how cultural values influence communication.

Answered: 1 week ago