Skip to content

Commit

Permalink
Implement player subclass that takes human input
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper-Guo committed Jun 1, 2024
1 parent 410f409 commit 153dbfa
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
33 changes: 33 additions & 0 deletions royal_game/players/human.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""An example player implementation that also serves as a dummy for testing."""

from typing import Iterable

from royal_game.modules.board import Board
from royal_game.modules.move import Move
from royal_game.modules.player import Player


class Human(Player):
"""You must implement the select_move method!""" # noqa: D400

def __init__(self, name: str):
"""Initialize the human player with a name."""
super().__init__(name)

def select_move(
self, board: Board, available_moves: Iterable[Move], white_turn: bool
) -> Move:
"""Print available moves and take player input."""
for i, move in enumerate(available_moves):
print(f"{i+1}: {move.__repr__()}")

while True:
player_selection = input("Please select a move: ")
try:
player_selection = int(player_selection)
except ValueError:
# catch non-int input strings
continue

if 1 <= player_selection <= len(available_moves):
return available_moves[player_selection - 1]
6 changes: 6 additions & 0 deletions tournament.py
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
"""CLI for benchmarking player agents against each other."""

from royal_game.modules.game import Game
from royal_game.players.dummy import Dummy

game = Game(Dummy(), Dummy())
game.play()

0 comments on commit 153dbfa

Please sign in to comment.