Skip to content

Commit aa0e87f

Browse files
Célande AdrienCDaffyJr
Célande Adrien
authored andcommitted
[11] AI does not shoot twice at the same position
1 parent c10e5a0 commit aa0e87f

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

tests/test_battleship.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import unittest
22

3-
from torpydo.battleship import parse_position, is_fleet_down
3+
from torpydo.battleship import parse_position, is_fleet_down, get_random_position
44
from torpydo.ship import Color, Letter, Position, Ship
5+
from unittest.mock import patch
56

67
class TestBattleship(unittest.TestCase):
78
def setUp(self):
@@ -16,6 +17,20 @@ def test_is_fleet_down(self):
1617
self.ships[0].is_sunk = True
1718
self.assertTrue(is_fleet_down(self.ships))
1819

20+
@patch("torpydo.battleship.NUMBER_ROWS", 2)
21+
@patch("torpydo.battleship.NUMBER_COL", 2)
22+
def test_random_duplicate_position(self):
23+
for i in range(10):
24+
board = []
25+
x = [
26+
get_random_position(board),
27+
get_random_position(board),
28+
get_random_position(board),
29+
get_random_position(board),
30+
]
31+
y = list(set(x))
32+
self.assertTrue(len(y) == 4)
33+
1934
def init_ship(ship: Ship, positions: list):
2035
ship.positions = positions
2136

torpydo/battleship.py

+28-8
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
from torpydo.telemetryclient import TelemetryClient
1111

1212
import time
13-
print("Starting")
1413

1514
myFleet = []
1615
enemyFleet = []
16+
board = []
1717
BAD_POSITION_INPUT_MSG = "The position is invalid. Please try enter a letter and a number like: 'A1'"
18+
NUMBER_ROWS = 8
19+
NUMBER_COL = 8
1820

21+
print("Starting")
1922

2023
def main():
2124
TelemetryClient.init()
@@ -88,6 +91,7 @@ def start_game():
8891
end_colouring()
8992

9093
is_hit = GameController.check_is_hit(enemyFleet, position)
94+
add_position_to_board(board, position, True)
9195

9296
start_colouring(right_colour(is_hit))
9397
print("Yeah ! Nice hit !" if is_hit else "Miss")
@@ -115,6 +119,7 @@ def start_game():
115119
# Computer
116120
position = get_random_position()
117121
is_hit = GameController.check_is_hit(myFleet, position)
122+
add_position_to_board(board, position, True)
118123
start_colouring(right_colour(is_hit))
119124

120125
print()
@@ -147,15 +152,30 @@ def parse_position(input: str):
147152

148153
return Position(letter, number)
149154

150-
def get_random_position():
151-
rows = 8
152-
lines = 8
155+
def add_position_to_board(board: list, position: Position, is_shot = None):
156+
if is_shot is True:
157+
position.is_shot = True
153158

154-
letter = Letter(random.randint(1, lines))
155-
number = random.randint(1, rows)
156-
position = Position(letter, number)
159+
if position not in board:
160+
board.append(position)
157161

158-
return position
162+
def get_random_position(board: list):
163+
rows = NUMBER_ROWS
164+
lines = NUMBER_COL
165+
166+
while True:
167+
letter = Letter(random.randint(1, lines))
168+
number = random.randint(1, rows)
169+
position = Position(letter, number)
170+
for pos in board:
171+
if position == pos:
172+
position = pos
173+
break
174+
175+
add_position_to_board(board, position)
176+
if not position.is_shot:
177+
position.is_shot = True
178+
return position
159179

160180
def initialize_game():
161181
initialize_myFleet()

torpydo/ship.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ def __init__(self, column: Letter, row: int):
2424
self.is_shot = False
2525

2626
def __eq__(self, other):
27-
return self.__dict__ == other.__dict__
27+
return self.column == other.column and self.row == other.row
28+
29+
def __hash__(self):
30+
return hash(f"{self.column}_{self.row}")
2831

2932
def __str__(self):
3033
return f"{self.column.name}{self.row}"

0 commit comments

Comments
 (0)