Skip to content

Commit

Permalink
add more checks
Browse files Browse the repository at this point in the history
  • Loading branch information
madcpf committed Oct 6, 2023
1 parent 68be945 commit 89b6c6e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions unitary/examples/quantum_chinese_chess/chess.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def play(self) -> None:
# If the game continues, switch the player.
if game_over == -1:
self.current_player = 1 - self.current_player
self.board.current_player = self.current_player
continue
elif game_over == 0:
print(f"{self.players_name[0]} wins! Game is over.")
Expand Down
6 changes: 3 additions & 3 deletions unitary/examples/quantum_chinese_chess/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ class MoveVariant(enum.Enum):


class Color(enum.Enum):
NA = 0
RED = 1
BLACK = 2
NA = -1
RED = 0
BLACK = 1


class Type(enum.Enum):
Expand Down
8 changes: 7 additions & 1 deletion unitary/examples/quantum_chinese_chess/move.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from typing import Optional, List, Tuple
from unitary.alpha.quantum_effect import QuantumEffect
from unitary.examples.quantum_chinese_chess.board import Board
from unitary.examples.quantum_chinese_chess.enums import MoveType, MoveVariant
from unitary.examples.quantum_chinese_chess.enums import MoveType, MoveVariant, Type


def parse_input_string(str_to_parse: str) -> Tuple[List[str], List[str]]:
Expand Down Expand Up @@ -72,6 +72,12 @@ def get_move_from_string(str_to_parse: str, board: Board) -> "Move":
sources, targets = parse_input_string(str_to_parse)
except ValueError as e:
raise e
# Additional checks based on the current board.
for source in sources:
if board.board[source].type_ == Type.EMPTY:
raise ValueError("Could not move empty piece.")
if board.board[source].color.value != board.current_player:
raise ValueError("Could not move the other player's piece.")
# TODO(): add analysis to determine move type and variant.
move_type = MoveType.UNSPECIFIED_STANDARD
move_variant = MoveVariant.UNSPECIFIED
Expand Down
12 changes: 10 additions & 2 deletions unitary/examples/quantum_chinese_chess/move_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import pytest


def test_parse_success():
def test_parse_input_string_success():
assert parse_input_string("a1b1") == (["a1"], ["b1"])
assert parse_input_string("a1b1^c2") == (["a1", "b1"], ["c2"])
assert parse_input_string("a1^b1c2") == (["a1"], ["b1", "c2"])


def test_parse_fail():
def test_parse_input_string_fail():
with pytest.raises(ValueError, match="Invalid sources/targets string "):
parse_input_string("a1^b1")
with pytest.raises(ValueError, match="Invalid sources/targets string "):
Expand All @@ -44,6 +44,14 @@ def test_parse_fail():
parse_input_string("a1n1")


def test_get_move_from_string_fail():
board = Board.from_fen()
with pytest.raises(ValueError, match="Could not move empty piece."):
get_move_from_string("a1b1", board)
with pytest.raises(ValueError, match="Could not move the other player's piece."):
get_move_from_string("a0b1", board)


def test_move_eq():
board = Board.from_fen()
move1 = Move(
Expand Down

0 comments on commit 89b6c6e

Please sign in to comment.