Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Pengfei Chen committed Oct 30, 2023
1 parent 7c82d5a commit 4af5d53
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 65 deletions.
2 changes: 1 addition & 1 deletion unitary/examples/quantum_chinese_chess/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
MoveVariant,
)
from unitary.examples.quantum_chinese_chess.piece import Piece
from unitary.examples.quantum_chinese_chess.move import Jump
from unitary.examples.quantum_chinese_chess.move import *


# The default initial state of the game.
Expand Down
46 changes: 13 additions & 33 deletions unitary/examples/quantum_chinese_chess/move.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,21 @@
import cirq
from unitary import alpha
from unitary.alpha.quantum_effect import QuantumEffect
from unitary.examples.quantum_chinese_chess.board import Board
from unitary.examples.quantum_chinese_chess.piece import Piece
from unitary.examples.quantum_chinese_chess.enums import MoveType, MoveVariant, Type


# TODO(): now the class is no longer the base class of all chess moves. Maybe convert this class
# to a helper class to save each move (with its pop results) in a string form into move history.
class Move(QuantumEffect):
class Move:
"""The base class of all chess moves."""

def __init__(
self,
source: str,
target: str,
board: Board,
source2: Optional[str] = None,
target2: Optional[str] = None,
source: Piece,
target: Piece,
source2: Optional[Piece] = None,
target2: Optional[Piece] = None,
move_type: Optional[MoveType] = None,
move_variant: Optional[MoveVariant] = None,
):
Expand All @@ -41,28 +39,12 @@ def __init__(
self.target2 = target2
self.move_type = move_type
self.move_variant = move_variant
self.board = board

def __eq__(self, other):
if isinstance(other, Move):
return (
self.source == other.source
and self.source2 == other.source2
and self.target == other.target
and self.target2 == other.target2
and self.move_type == other.move_type
and self.move_variant == other.move_variant
)
return self.to_str(3) == other.to_str(3)
return False

def _verify_objects(self, *objects):
# TODO(): add checks that apply to all move types
return

def effect(self, *objects):
# TODO(): add effects according to move_type and move_variant
return

def is_split_move(self) -> bool:
return self.target2 is not None

Expand All @@ -80,27 +62,25 @@ def to_str(self, verbose_level: int = 1) -> str:
return ""

if self.is_split_move():
move_str = [self.source + "^" + self.target + str(self.target2)]
move_str = [self.source.name + "^" + self.target.name + self.target2.name]
elif self.is_merge_move():
move_str = [self.source + str(self.source2) + "^" + self.target]
move_str = [self.source.name + self.source2.name + "^" + self.target.name]
else:
move_str = [self.source + self.target]
move_str = [self.source.name + self.target.name]

if verbose_level > 1:
move_str.append(self.move_type.name)
move_str.append(self.move_variant.name)

if verbose_level > 2:
source = self.board.board[self.source]
target = self.board.board[self.target]
move_str.append(
source.color.name
self.source.color.name
+ "_"
+ source.type_.name
+ self.source.type_.name
+ "->"
+ target.color.name
+ self.target.color.name
+ "_"
+ target.type_.name
+ self.target.type_.name
)
return ":".join(move_str)

Expand Down
67 changes: 36 additions & 31 deletions unitary/examples/quantum_chinese_chess/move_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,31 @@

def test_move_eq():
board = Board.from_fen()
world = board.board
move1 = Move(
"a1",
"b2",
board,
"c1",
world["a1"],
world["b2"],
world["c1"],
move_type=MoveType.MERGE_JUMP,
move_variant=MoveVariant.CAPTURE,
)
move2 = Move(
"a1",
"b2",
board,
"c1",
world["a1"],
world["b2"],
world["c1"],
move_type=MoveType.MERGE_JUMP,
move_variant=MoveVariant.CAPTURE,
)
move3 = Move(
"a1", "b2", board, move_type=MoveType.JUMP, move_variant=MoveVariant.CAPTURE
world["a1"],
world["b2"],
move_type=MoveType.JUMP,
move_variant=MoveVariant.CAPTURE,
)
move4 = Move(
"a1",
"b2",
board,
"c1",
world["a1"],
world["b2"],
world["c1"],
move_type=MoveType.MERGE_SLIDE,
move_variant=MoveVariant.CAPTURE,
)
Expand All @@ -75,30 +76,32 @@ def test_move_eq():
def test_move_type():
# TODO(): change to real senarios
board = Board.from_fen()
world = board.board
move1 = Move(
"a1",
"b2",
board,
"c1",
world["a1"],
world["b2"],
world["c1"],
move_type=MoveType.MERGE_JUMP,
move_variant=MoveVariant.CAPTURE,
)
assert move1.is_split_move() == False
assert move1.is_merge_move()

move2 = Move(
"a1",
"b2",
board,
target2="c1",
world["a1"],
world["b2"],
target2=world["c1"],
move_type=MoveType.SPLIT_JUMP,
move_variant=MoveVariant.BASIC,
)
assert move2.is_split_move()
assert move2.is_merge_move() == False

move3 = Move(
"a1", "b2", board, move_type=MoveType.SLIDE, move_variant=MoveVariant.CAPTURE
world["a1"],
world["b2"],
move_type=MoveType.SLIDE,
move_variant=MoveVariant.CAPTURE,
)
assert move3.is_split_move() == False
assert move3.is_merge_move() == False
Expand All @@ -107,11 +110,11 @@ def test_move_type():
def test_to_str():
# TODO(): change to real scenarios
board = Board.from_fen()
world = board.board
move1 = Move(
"a0",
"a6",
board,
"c1",
world["a0"],
world["a6"],
world["c1"],
move_type=MoveType.MERGE_JUMP,
move_variant=MoveVariant.CAPTURE,
)
Expand All @@ -121,10 +124,9 @@ def test_to_str():
assert move1.to_str(3) == "a0c1^a6:MERGE_JUMP:CAPTURE:RED_ROOK->BLACK_PAWN"

move2 = Move(
"a0",
"b3",
board,
target2="c1",
world["a0"],
world["b3"],
target2=world["c1"],
move_type=MoveType.SPLIT_JUMP,
move_variant=MoveVariant.BASIC,
)
Expand All @@ -134,7 +136,10 @@ def test_to_str():
assert move2.to_str(3) == "a0^b3c1:SPLIT_JUMP:BASIC:RED_ROOK->NA_EMPTY"

move3 = Move(
"a0", "a6", board, move_type=MoveType.SLIDE, move_variant=MoveVariant.CAPTURE
world["a0"],
world["a6"],
move_type=MoveType.SLIDE,
move_variant=MoveVariant.CAPTURE,
)
assert move3.to_str(0) == ""
assert move3.to_str(1) == "a0a6"
Expand Down

0 comments on commit 4af5d53

Please sign in to comment.