Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 34ea397

Browse files
committed
feat(Battleship): added player module
different implementation as the board is attached to the opponent player
1 parent acd6b20 commit 34ea397

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

projects/Battleship/player.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from dataclasses import dataclass
2+
3+
from utils import generate_random_ships_arrangements
4+
from board import Board
5+
6+
7+
@dataclass
8+
class Player:
9+
"""
10+
Represents a player in the Battleship game.
11+
12+
Attributes:
13+
name (str): The name of the player.
14+
enemy_board (Board): The board representing the opponent's board.
15+
"""
16+
17+
name: str
18+
enemy_board: Board
19+
20+
def attack(self, row: int, col: int):
21+
"""
22+
Attack a specific location on the opponent's board.
23+
24+
Args:
25+
row (int): The row index of the target location.
26+
col (int): The column index of the target location.
27+
"""
28+
self.enemy_board.enemy_move(row, col)
29+
30+
def generate_random_ship_arrangements(self) -> None:
31+
"""Generate random ship arrangements for the opponent's board and place them."""
32+
ships = generate_random_ships_arrangements(self.enemy_board.board_size)
33+
for ship in ships:
34+
self.enemy_board.place_ship(ship.coordinates)
35+
36+
def place_ship(self, ship_coordinates):
37+
"""
38+
Place a ship on the opponent's board.
39+
40+
Args:
41+
ship_coordinates: Coordinates representing the ship's position.
42+
"""
43+
# Remind that this is for the enemy's ships not this player.
44+
self.enemy_board.place_ship(ship_coordinates)

0 commit comments

Comments
 (0)