Python: Add type annotations

This commit is contained in:
Martin Thoma
2022-03-19 22:10:26 +01:00
committed by Alexander Wunschik
parent b95a514e84
commit 83b3dc402c
79 changed files with 509 additions and 461 deletions

View File

@@ -82,12 +82,12 @@ def get_next_target(sea: SeaType) -> PointType:
while True:
try:
guess = input("? ")
point = guess.split(",")
point_str_list = guess.split(",")
if len(point) != 2:
if len(point_str_list) != 2:
raise ValueError()
point = (int(point[0]), int(point[1]))
point = (int(point_str_list[0]), int(point_str_list[1]))
if not is_within_sea(point, sea):
raise ValueError()
@@ -99,7 +99,7 @@ def get_next_target(sea: SeaType) -> PointType:
)
def setup_ships(sea: SeaType):
def setup_ships(sea: SeaType) -> None:
place_ship(sea, DESTROYER_LENGTH, 1)
place_ship(sea, DESTROYER_LENGTH, 2)
place_ship(sea, CRUISER_LENGTH, 3)

View File

@@ -36,7 +36,7 @@ class Vector:
class Sea:
WIDTH = 6
def __init__(self):
def __init__(self) -> None:
self._graph = tuple([0 for _ in range(self.WIDTH)] for _ in range(self.WIDTH))
def _validate_item_indices(self, point: Point) -> None:
@@ -67,7 +67,7 @@ class Sea:
return True
# Redefines how python will render this object when asked as a str
def __str__(self):
def __str__(self) -> str:
# Display it encoded
return "\n".join(
[
@@ -145,7 +145,7 @@ class Battle:
break
def loop(self):
def loop(self) -> None:
while True:
target = self._next_target()
target_value = self.sea[target]
@@ -174,7 +174,7 @@ class Battle:
print(f"YOUR CURRENT SPLASH/HIT RATIO IS {self.splash_hit_ratio}")
def _display_sunk_report(self):
def _display_sunk_report(self) -> None:
print(
"SO FAR, THE BAD GUYS HAVE LOST",
f"{self.sea.count_sunk(1, 2)} DESTROYER(S),",
@@ -182,7 +182,7 @@ class Battle:
f"AND {self.sea.count_sunk(5, 6)} AIRCRAFT CARRIER(S).",
)
def _display_game_end(self):
def _display_game_end(self) -> None:
print(
"YOU HAVE TOTALLY WIPED OUT THE BAD GUYS' FLEET "
f"WITH A FINAL SPLASH/HIT RATIO OF {self.splash_hit_ratio}"