|
10 | 10 | from torpydo.telemetryclient import TelemetryClient
|
11 | 11 |
|
12 | 12 | import time
|
13 |
| -print("Starting") |
14 | 13 |
|
15 | 14 | myFleet = []
|
16 | 15 | enemyFleet = []
|
| 16 | +board = [] |
17 | 17 | 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 |
18 | 20 |
|
| 21 | +print("Starting") |
19 | 22 |
|
20 | 23 | def main():
|
21 | 24 | TelemetryClient.init()
|
@@ -88,6 +91,7 @@ def start_game():
|
88 | 91 | end_colouring()
|
89 | 92 |
|
90 | 93 | is_hit = GameController.check_is_hit(enemyFleet, position)
|
| 94 | + add_position_to_board(board, position, True) |
91 | 95 |
|
92 | 96 | start_colouring(right_colour(is_hit))
|
93 | 97 | print("Yeah ! Nice hit !" if is_hit else "Miss")
|
@@ -115,6 +119,7 @@ def start_game():
|
115 | 119 | # Computer
|
116 | 120 | position = get_random_position()
|
117 | 121 | is_hit = GameController.check_is_hit(myFleet, position)
|
| 122 | + add_position_to_board(board, position, True) |
118 | 123 | start_colouring(right_colour(is_hit))
|
119 | 124 |
|
120 | 125 | print()
|
@@ -147,15 +152,30 @@ def parse_position(input: str):
|
147 | 152 |
|
148 | 153 | return Position(letter, number)
|
149 | 154 |
|
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 |
153 | 158 |
|
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) |
157 | 161 |
|
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 |
159 | 179 |
|
160 | 180 | def initialize_game():
|
161 | 181 | initialize_myFleet()
|
|
0 commit comments