Skip to content

Commit

Permalink
added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
GalaX1us committed Oct 13, 2022
1 parent 0792da9 commit 23786af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
12 changes: 9 additions & 3 deletions AI.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -136,14 +141,15 @@ 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
"""

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]

Expand Down
14 changes: 13 additions & 1 deletion test_AI_perf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down

0 comments on commit 23786af

Please sign in to comment.