From 23786afc4996b5aa5d3c7c23d1b505675ac69f59 Mon Sep 17 00:00:00 2001 From: BONADA Nathan Date: Thu, 13 Oct 2022 10:01:57 +0200 Subject: [PATCH] added comments --- AI.py | 12 +++++++++--- test_AI_perf.py | 14 +++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/AI.py b/AI.py index bef9487..14c002e 100644 --- a/AI.py +++ b/AI.py @@ -42,11 +42,13 @@ def gen_prob_map(self): # check where a ship will fit on the board for y in range(NB_TILE): for x in range(NB_TILE): + # set the probability for misses or explosion to zero if self.moves_made[get_index(x,y)] is Move.MISS or self.moves_made[get_index(x,y)] is Move.EXPLOSION: prob_map[y][x] = 0 elif self.moves_made[get_index(x,y)] is Move.UNKNOWN: + # get potential ship endpoints endpoints = [] @@ -59,9 +61,11 @@ def gen_prob_map(self): if x + use_size <= 9: endpoints.append(((y, x), (y, x + use_size))) - # add 1 to all endpoints to compensate for python indexing for (start_y, start_x), (end_y, end_x) in endpoints: if np.all(self.shot_map[start_y:end_y+1, start_x:end_x+1] == 0): + + # add 1 to all endpoints to compensate for python indexing + # increase probability of attacking tiles where a ship can fits in prob_map[start_y:end_y+1, start_x:end_x+1] += 1 # increase probability of attacking tiles near successful hits @@ -94,7 +98,8 @@ def gen_prob_map(self): self.prob_map = prob_map def make_move(self, opponent:Player): - """automatically makes the AI play + """ + Automatically makes the AI play Args: opponent (Player): current opponent @@ -136,7 +141,7 @@ def make_move(self, opponent:Player): def find_good_move(self): """ - Find the best move that can be done based on the matric of probability + Find the best move that can be done based on the matrix of probability Returns: tuple(int,int): coords of the best move @@ -144,6 +149,7 @@ def find_good_move(self): self.gen_prob_map() + # gets the index of the largest probability in the matrix best_prob = np.where(self.prob_map == np.amax(self.prob_map)) guess_y, guess_x = best_prob[0][0], best_prob[1][0] diff --git a/test_AI_perf.py b/test_AI_perf.py index d57ff3b..7a62b70 100644 --- a/test_AI_perf.py +++ b/test_AI_perf.py @@ -3,6 +3,11 @@ import time import click +# This program aims to test the performance of different types of AI +# on different types of game (diff settings) + + + # ==== Settings ==== NB_GAME = 1000 NB_MINE=0 @@ -18,24 +23,31 @@ print("Performance test in progress ...") +# this is a live progress bar with a "time left" counter with click.progressbar(range(NB_GAME),fill_char='█',bar_template="[%(bar)s] %(info)s") as bar: for i in bar: + + # init the i-th game game = Game() + # init the i-th players game.player1 = PlayerAI("AI(1)",SHIP_SIZES,NB_MINE) game.player2 = PlayerAI("AI(2)",SHIP_SIZES,NB_MINE) game.current_player = game.player1 game.current_opponent = game.player2 + # play until the end while not game.over: game.play() game.next_round() - + + # save stats of the i-th game game_rounds_list.append(game.rounds) game_winners_list.append(game.winner.name) global_end = time.perf_counter() +# write stats in the result file with open(RESULT_FILE, "a") as fichier: fichier.write("====================================\n") fichier.write("AI version : {}\n".format(AI_VERSION))