Skip to content

Commit

Permalink
limit speeds
Browse files Browse the repository at this point in the history
  • Loading branch information
vpoulailleau committed Jan 15, 2024
1 parent 74df0b3 commit 2de4a42
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down
5 changes: 5 additions & 0 deletions space_collector/game/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
8 changes: 8 additions & 0 deletions space_collector/game/spaceship.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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")

0 comments on commit 2de4a42

Please sign in to comment.