|
13 | 13 |
|
14 | 14 | myFleet = []
|
15 | 15 | enemyFleet = []
|
| 16 | +BAD_POSITION_INPUT_MSG = "The position is invalid. Please try enter a letter and a number like: 'A1'" |
16 | 17 |
|
17 | 18 |
|
18 | 19 | def main():
|
@@ -78,7 +79,11 @@ def start_game():
|
78 | 79 | start_colouring(Fore.GREEN)
|
79 | 80 | print("Player, it's your turn")
|
80 | 81 | print("Coordinates should be written in the following format 'LetterNumber' as in C1, F4")
|
81 |
| - position = parse_position(input("Enter coordinates (A-H, 1-8) for your shot :")) |
| 82 | + try: |
| 83 | + position = parse_position(check_position_input("Enter coordinates (A-H, 1-8) for your shot :")) |
| 84 | + except Exception: |
| 85 | + print(BAD_POSITION_INPUT_MSG) |
| 86 | + continue |
82 | 87 | end_colouring()
|
83 | 88 |
|
84 | 89 | is_hit = GameController.check_is_hit(enemyFleet, position)
|
@@ -188,9 +193,16 @@ def initialize_myFleet():
|
188 | 193 | print()
|
189 | 194 | print(f"Please enter the positions for the {ship.name} (size: {ship.size})")
|
190 | 195 |
|
191 |
| - for i in range(ship.size): |
192 |
| - position_input = input(f"Enter position {i+1} of {ship.size} (i.e A3):") |
| 196 | + i = 0 |
| 197 | + while i < ship.size: |
| 198 | + try: |
| 199 | + position_input = check_position_input(f"Enter position {i+1} of {ship.size} (i.e A3):") |
| 200 | + except Exception: |
| 201 | + print(BAD_POSITION_INPUT_MSG) |
| 202 | + continue |
| 203 | + else: |
193 | 204 | ship.add_position(position_input)
|
| 205 | + i += 1 |
194 | 206 | TelemetryClient.trackEvent('Player_PlaceShipPosition', {'custom_dimensions': {'Position': position_input, 'Ship': ship.name, 'PositionInShip': i}})
|
195 | 207 |
|
196 | 208 | def initialize_enemyFleet():
|
@@ -220,5 +232,11 @@ def initialize_enemyFleet():
|
220 | 232 | enemyFleet[4].positions.append(Position(Letter.C, 5))
|
221 | 233 | enemyFleet[4].positions.append(Position(Letter.C, 6))
|
222 | 234 |
|
| 235 | +def check_position_input(msg: str): |
| 236 | + string = input(msg) |
| 237 | + assert len(string) == 2 and string[0].isalpha() and string[1:].isnumeric() |
| 238 | + return string |
| 239 | + |
| 240 | + |
223 | 241 | if __name__ == '__main__':
|
224 | 242 | main()
|
0 commit comments