Skip to content

Commit

Permalink
Merge pull request #3 from Klavionik/add-fleet-ship-count
Browse files Browse the repository at this point in the history
Add ship count to the header of the Fleet widget
  • Loading branch information
Klavionik authored Sep 14, 2024
2 parents b8232d0 + 3f6f096 commit feaa29e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- The Fleet widget displays the number of ships `[alive/total]` in its header.

## [0.24.1] - 2024-08-30
### Fixed
Expand Down
4 changes: 2 additions & 2 deletions battleship/tui/screens/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,19 @@ def __init__(

self.player_fleet = Fleet(
id="player_fleet",
title="You",
roster=strategy.roster,
cell_factory=player_cell_factory,
classes="player",
)
self.player_fleet.border_title = "Your fleet"
self.enemy_fleet = Fleet(
id="enemy_fleet",
title="Enemy",
roster=strategy.roster,
cell_factory=enemy_cell_factory,
allow_placing=False,
classes="enemy",
)
self.enemy_fleet.border_title = "Enemy fleet"

self.board_map: dict[str, Board] = {
self._player_name: self.player_board,
Expand Down
1 change: 1 addition & 0 deletions battleship/tui/styles.tcss
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Fleet {
scrollbar-size-vertical: 1;
padding: 1 0;
border: wide $accent;
border-title-align: center;
}

Fleet.player {
Expand Down
18 changes: 16 additions & 2 deletions battleship/tui/widgets/fleet.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from textual.app import ComposeResult
from textual.events import Mount
from textual.message import Message
from textual.reactive import var
from textual.reactive import reactive, var
from textual.widget import Widget
from textual.widgets import Static

Expand Down Expand Up @@ -87,10 +87,12 @@ def action_preview(self) -> None:

class Fleet(Widget):
previewing_id: var[str] = var("")
ships_alive: reactive[int] = reactive(0)

def __init__(
self,
*args: Any,
title: str,
roster: Roster,
cell_factory: CellFactory,
allow_placing: bool = True,
Expand All @@ -107,7 +109,6 @@ def ship_factory(hp: int, damage: int, destroyed: bool) -> Text:
damage_parts = [cell_factory.damaged().render() for _ in range(damage)]
return Text.assemble(*(hp_parts + damage_parts), "\n")

self._roster = roster
self._allow_placing = allow_placing
self._previewing_id = ""
self._ships: dict[str, Ship] = {
Expand All @@ -121,6 +122,10 @@ def ship_factory(hp: int, damage: int, destroyed: bool) -> Text:
)
for ship in roster
}
self._title = title

self.ships_total = len(self._ships)
self.ships_alive = self.ships_total

def watch_previewing_id(self, old: str, new: str) -> None:
if old:
Expand All @@ -129,6 +134,9 @@ def watch_previewing_id(self, old: str, new: str) -> None:
if new:
self._ships[new].previewing = True

def watch_ships_alive(self) -> None:
self.update_title()

def compose(self) -> ComposeResult:
yield from self._ships.values()

Expand All @@ -144,3 +152,9 @@ def place(self, ship_id: str) -> None:
def damage(self, ship_id: str) -> None:
ship = self._ships[ship_id]
ship.damage()

if ship.destroyed:
self.ships_alive -= 1

def update_title(self) -> None:
self.border_title = f"[{self.ships_alive}/{self.ships_total}] {self._title}"

0 comments on commit feaa29e

Please sign in to comment.