From 2de4a42301fc6b8c3e32512d0c68bd34e67be975 Mon Sep 17 00:00:00 2001 From: Vincent Poulailleau Date: Mon, 15 Jan 2024 17:51:05 +0100 Subject: [PATCH] limit speeds --- README.md | 6 ++++++ space_collector/game/player.py | 5 +++++ space_collector/game/spaceship.py | 8 ++++++++ 3 files changed, 19 insertions(+) diff --git a/README.md b/README.md index 24f7013..6f60944 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,12 @@ Space collector game Changes the speed and angle of the spaceship. +Maximum speed: + +- 1 000 kms/s for collectors +- 2 000 kms/s for explorers +- 3 000 kms/s for attackers + ### Fire `Fire {ship_id} {angle}` diff --git a/space_collector/game/player.py b/space_collector/game/player.py index 07dcfda..654c5d7 100644 --- a/space_collector/game/player.py +++ b/space_collector/game/player.py @@ -60,7 +60,12 @@ def spaceship_by_id(self, id_: int) -> Spaceship: def move(self, parameters: list[str]) -> str: ship_id, angle, speed = (int(param) for param in parameters) + angle %= 360 spaceship = self.spaceship_by_id(ship_id) + if speed > spaceship.MAX_SPEED: + raise ValueError(f"Max speed excedeed {speed}/{spaceship.MAX_SPEED}") + if speed < 0: + raise ValueError("Negative speed not allowed") spaceship.move(angle, speed) return "OK" diff --git a/space_collector/game/spaceship.py b/space_collector/game/spaceship.py index a48c7f4..182e600 100644 --- a/space_collector/game/spaceship.py +++ b/space_collector/game/spaceship.py @@ -1,11 +1,13 @@ import math from dataclasses import dataclass +from typing import ClassVar from space_collector.game.constants import MAP_DIMENSION @dataclass class Spaceship: + MAX_SPEED: ClassVar[input] id: int x: float y: float @@ -47,15 +49,21 @@ def state(self) -> dict: class Collector(Spaceship): + MAX_SPEED = 1000 + def __init__(self, id_: int, x: int, y: int, angle: int) -> None: super().__init__(id_, x, y, angle, 0, False, "collector") class Attacker(Spaceship): + MAX_SPEED = 3000 + def __init__(self, id_: int, x: int, y: int, angle: int) -> None: super().__init__(id_, x, y, angle, 0, False, "attacker") class Explorer(Spaceship): + MAX_SPEED = 2000 + def __init__(self, id_: int, x: int, y: int, angle: int) -> None: super().__init__(id_, x, y, angle, 0, False, "explorer")