Skip to content

Commit 39802e0

Browse files
Célande AdrienCDaffyJr
authored andcommitted
[3] correct infinite game
check that all a player boats are sunk
1 parent bf5991d commit 39802e0

File tree

7 files changed

+61
-6
lines changed

7 files changed

+61
-6
lines changed

tests/test_battleship.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
import unittest
22

3-
from torpydo.battleship import parse_position
3+
from torpydo.battleship import parse_position, is_fleet_down
4+
from torpydo.ship import Color, Letter, Position, Ship
45

56
class TestBattleship(unittest.TestCase):
7+
def setUp(self):
8+
self.ships = []
9+
self.ships.append(init_ship(Ship("Test", 2, Color.RED), [Position(Letter.A, 1), Position(Letter.A, 2)]))
10+
611
def test_parse_position_true(self):
712
self.assertTrue(parse_position("A1"))
813

14+
def test_is_fleet_down(self):
15+
self.assertFalse(is_fleet_down(self.ships))
16+
self.ships[0].is_sunk = True
17+
self.assertTrue(is_fleet_down(self.ships))
18+
19+
def init_ship(ship: Ship, positions: list):
20+
ship.positions = positions
21+
22+
return ship
23+
924
if '__main__' == __name__:
1025
unittest.main()

tests/test_game_controller.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def setUp(self):
1010

1111
def test_check_is_hit_true(self):
1212
self.assertTrue(GameController.check_is_hit(self.ships, Position(Letter.A, 1)))
13+
self.assertTrue(self.ships[0].positions[0].is_shot)
1314

1415
def test_check_is_hit_false(self):
1516
self.assertFalse(GameController.check_is_hit(self.ships, Position(Letter.B, 1)))

tests/test_ship.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import unittest
2+
3+
from torpydo.ship import Color, Letter, Position, Ship
4+
from torpydo.game_controller import GameController
5+
6+
class TestBattleship(unittest.TestCase):
7+
def setUp(self):
8+
self.ships = []
9+
self.ships.append(init_ship(Ship("Test", 2, Color.RED), [Position(Letter.A, 1), Position(Letter.A, 2)]))
10+
11+
def test_check_sunk(self):
12+
self.assertFalse(self.ships[0].is_sunk)
13+
GameController.check_is_hit(self.ships, Position(Letter.A, 1))
14+
self.assertFalse(self.ships[0].is_sunk)
15+
GameController.check_is_hit(self.ships, Position(Letter.A, 2))
16+
self.assertTrue(self.ships[0].is_sunk)
17+
18+
def init_ship(ship: Ship, positions: list):
19+
ship.positions = positions
20+
21+
return ship
22+
if '__main__' == __name__:
23+
unittest.main()

torpydo/__init__.py

Whitespace-only changes.

torpydo/battleship.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def start_game():
7474
while True:
7575
print()
7676

77+
# Player
7778
start_colouring(Fore.GREEN)
7879
print("Player, it's your turn")
7980
print("Coordinates should be written in the following format 'LetterNumber' as in C1, F4")
@@ -85,6 +86,9 @@ def start_game():
8586
start_colouring(right_colour(is_hit))
8687
print("Yeah ! Nice hit !" if is_hit else "Miss")
8788
TelemetryClient.trackEvent('Player_ShootPosition', {'custom_dimensions': {'Position': str(position), 'IsHit': is_hit}})
89+
if is_fleet_down(enemyFleet):
90+
print("Congratulations! You are the winner \o/")
91+
break
8892

8993
print( r'''
9094
\ . ./
@@ -100,15 +104,14 @@ def start_game():
100104

101105
print("\n\nComputer is thinking...")
102106
time.sleep(3)
107+
# Computer
103108
position = get_random_position()
104109
is_hit = GameController.check_is_hit(myFleet, position)
105110
start_colouring(right_colour(is_hit))
106111

107112
print()
108113
print(f"Computer shoot in {str(position)} and {'hit your ship!' if is_hit else 'miss'}")
109114
TelemetryClient.trackEvent('Computer_ShootPosition', {'custom_dimensions': {'Position': str(position), 'IsHit': is_hit}})
110-
111-
112115
print(r'''
113116
\ . ./
114117
\ .:"";'.:.."" /
@@ -119,11 +122,18 @@ def start_game():
119122
-\ \ / /-
120123
\ \ / /''')
121124
end_colouring()
125+
if is_fleet_down(myFleet):
126+
print("Sorry, you lost...")
127+
break
128+
129+
print("Thank you for playing!")
130+
131+
def is_fleet_down(fleet):
132+
return all(ship.is_sunk for ship in fleet)
122133

123134
def parse_position(input: str):
124135
letter = Letter[input.upper()[:1]]
125136
number = int(input[1:])
126-
position = Position(letter, number)
127137

128138
return Position(letter, number)
129139

torpydo/game_controller.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ def check_is_hit(ships: list, shot: Position):
1313
for ship in ships:
1414
for position in ship.positions:
1515
if position == shot:
16+
position.is_shot = True
17+
ship.check_sunk()
1618
return True
1719

1820
return False
@@ -27,7 +29,7 @@ def initialize_ships():
2729

2830
def is_ship_valid(ship: Ship):
2931
is_valid = len(ship.positions) == ship.size
30-
32+
3133
return is_valid
3234

3335
def get_random_position(size: int):

torpydo/ship.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Position(object):
2121
def __init__(self, column: Letter, row: int):
2222
self.column = column
2323
self.row = row
24+
self.is_shot = False
2425

2526
def __eq__(self, other):
2627
return self.__dict__ == other.__dict__
@@ -36,14 +37,17 @@ def __init__(self, name: str, size: int, color: Color):
3637
self.size = size
3738
self.color = color
3839
self.positions = []
40+
self.is_sunk = False
3941

4042
def add_position(self, input: str):
4143
letter = Letter[input.upper()[:1]]
4244
number = int(input[1:])
43-
position = Position(letter, number)
4445

4546
self.positions.append(Position(letter, number))
4647

48+
def check_sunk(self):
49+
self.is_sunk = all(position.is_shot for position in self.positions)
50+
4751
def __str__(self):
4852
return f"{self.color.name} {self.name} ({self.size}): {self.positions}"
4953

0 commit comments

Comments
 (0)