Skip to content

Commit

Permalink
move piece class to its own file and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Pengfei Chen committed Oct 2, 2023
1 parent 1d51af0 commit 5298a11
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 99 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ You can then clone the repository into your development environment by
using (substitute USER with your github username)

git clone https://github.com/USER/unitary.git
cd unitary
git remote add upstream https://github.com/quantumlib/unitary.git

This will clone your fork so that you can work on it, while marking the
Expand Down
96 changes: 31 additions & 65 deletions unitary/examples/quantum_chinese_chess/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,71 +47,37 @@ class MoveVariant(enum.Enum):
CAPTURE = 3


class Piece:
class Type(enum.Enum):
EMPTY = "."
SOLDIER = "s"
CANNON = "c"
ROOK = "r"
HORSE = "h"
ELEPHANT = "e"
ADVISOR = "a"
GENERAL = "g"

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

def __init__(self, type_: Type, color_: Color):
self.type_ = type_
self.color_ = color_

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


class Type(enum.Enum):
""" The four values are symbols corresponding to
English red
English black
Chinese red
Chinese black
"""
EMPTY = [".", ".", ".", "."]
SOLDIER = ["s", "S", "兵", "卒"]
CANNON = ["c", "C", "炮", "砲"]
ROOK = ["r", "R", "车", "車"]
HORSE = ["h", "H", "马", "馬"]
ELEPHANT = ["e", "E", "象", "相"]
ADVISOR = ["a", "A", "士", "仕"]
GENERAL = ["g", "G", "将", "帥"]

@staticmethod
def type_of(c: str) -> Optional["Type"]:
return {
"s": Piece.Type.SOLDIER,
"c": Piece.Type.CANNON,
"r": Piece.Type.ROOK,
"h": Piece.Type.HORSE,
"e": Piece.Type.ELEPHANT,
"a": Piece.Type.ADVISOR,
"g": Piece.Type.GENERAL,
".": Piece.Type.EMPTY,
"s": Type.SOLDIER,
"c": Type.CANNON,
"r": Type.ROOK,
"h": Type.HORSE,
"e": Type.ELEPHANT,
"a": Type.ADVISOR,
"g": Type.GENERAL,
".": Type.EMPTY,
}.get(c.lower(), None)

def red_symbol(self, lang: Language = Language.EN) -> str:
if lang == Language.EN: # Return English symbols
return self.type_.value.upper()
else: # Return Chinese symbols
return {
Piece.Type.SOLDIER: "兵",
Piece.Type.CANNON: "炮",
Piece.Type.ROOK: "车",
Piece.Type.HORSE: "马",
Piece.Type.ELEPHANT: "象",
Piece.Type.ADVISOR: "士",
Piece.Type.GENERAL: "将",
Piece.Type.EMPTY: ".",
}.get(self.type_)

def black_symbol(self, lang: Language = Language.EN) -> str:
if lang == Language.EN: # Return English symbols
return self.type_.value
else: # Return Chinese symbols
return {
Piece.Type.SOLDIER: "卒",
Piece.Type.CANNON: "砲",
Piece.Type.ROOK: "車",
Piece.Type.HORSE: "馬",
Piece.Type.ELEPHANT: "相",
Piece.Type.ADVISOR: "仕",
Piece.Type.GENERAL: "帥",
Piece.Type.EMPTY: ".",
}.get(self.type_)

def symbol(self, lang: Language = Language.EN) -> str:
return {
Piece.Color.RED: self.red_symbol(lang),
Piece.Color.BLACK: self.black_symbol(lang),
Piece.Color.NA: ".",
}.get(self.color_)
42 changes: 8 additions & 34 deletions unitary/examples/quantum_chinese_chess/enums_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,13 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unitary.examples.quantum_chinese_chess.enums import Piece, Language
from unitary.examples.quantum_chinese_chess.enums import Type, Color, Language


def test_piece_type_of():
assert Piece.type_of("s") == Piece.Type.SOLDIER
assert Piece.type_of("S") == Piece.Type.SOLDIER
assert Piece.type_of("g") == Piece.Type.GENERAL
assert Piece.type_of("G") == Piece.Type.GENERAL
assert Piece.type_of(".") == Piece.Type.EMPTY
assert Piece.type_of("b") == None


def test_piece_symbol():
p0 = Piece(Piece.Type.CANNON, Piece.Color.RED)
assert p0.red_symbol() == "C"
assert p0.black_symbol() == "c"
assert p0.symbol() == "C"
assert p0.red_symbol(Language.ZH) == "炮"
assert p0.black_symbol(Language.ZH) == "砲"
assert p0.symbol(Language.ZH) == "炮"

p1 = Piece(Piece.Type.HORSE, Piece.Color.BLACK)
assert p1.red_symbol() == "H"
assert p1.black_symbol() == "h"
assert p1.symbol() == "h"
assert p1.red_symbol(Language.ZH) == "马"
assert p1.black_symbol(Language.ZH) == "馬"
assert p1.symbol(Language.ZH) == "馬"

p2 = Piece(Piece.Type.EMPTY, Piece.Color.NA)
assert p2.red_symbol() == "."
assert p2.black_symbol() == "."
assert p2.symbol() == "."
assert p2.red_symbol(Language.ZH) == "."
assert p2.black_symbol(Language.ZH) == "."
assert p2.symbol(Language.ZH) == "."
def test_type_type_of():
assert Type.type_of("s") == Type.SOLDIER
assert Type.type_of("S") == Type.SOLDIER
assert Type.type_of("g") == Type.GENERAL
assert Type.type_of("G") == Type.GENERAL
assert Type.type_of(".") == Type.EMPTY
assert Type.type_of("b") == None
47 changes: 47 additions & 0 deletions unitary/examples/quantum_chinese_chess/piece.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2023 The Unitary Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unitary.alpha import (
QuantumObject
)
from unitary.examples.quantum_chinese_chess.enums import (
SquareState,
Language,
Color,
Type
)


class Piece(QuantumObject):
def __init__(self, name: str, state: SquareState, type_: Type, color: Color):
QuantumObject.__init__(self, name, state)
self.type_ = type_
self.color = color

def symbol(self, lang: Language = Language.EN) -> str:
if self.type_ == Type.EMPTY:
return "."
if lang == Language.EN: # Return English symbols
if self.color == Color.RED:
return self.type_.value[0]
elif self.color == Color.BLACK:
return self.type_.value[1]
elif lang == Language.ZH: # Return Chinese symbols
if self.color == Color.RED:
return self.type_.value[2]
elif self.color == Color.BLACK:
return self.type_.value[3]
return "Unexpected combinations"

def __str__(self):
return self.symbol()
47 changes: 47 additions & 0 deletions unitary/examples/quantum_chinese_chess/piece_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2023 The Unitary Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from unitary.examples.quantum_chinese_chess.enums import (
SquareState,
Language,
Color,
Type
)
from unitary.examples.quantum_chinese_chess.piece import Piece
from unitary.alpha import QuantumWorld


def test_symbol():
p0 = Piece("a0", SquareState.OCCUPIED, Type.CANNON, Color.RED)
assert p0.symbol() == "c"
assert p0.__str__() == "c"
assert p0.symbol(Language.ZH) == "炮"

p1 = Piece("b1", SquareState.OCCUPIED, Type.HORSE, Color.BLACK)
assert p1.symbol() == "H"
assert p1.__str__() == "H"
assert p1.symbol(Language.ZH) == "馬"

p2 = Piece("c2", SquareState.EMPTY, Type.EMPTY, Color.NA)
assert p2.symbol() == "."
assert p2.__str__() == "."
assert p2.symbol(Language.ZH) == "."


def test_enum():
p0 = Piece("a0", SquareState.OCCUPIED, Type.CANNON, Color.RED)
p1 = Piece("b1", SquareState.OCCUPIED, Type.HORSE, Color.BLACK)
p2 = Piece("c2", SquareState.EMPTY, Type.EMPTY, Color.NA)
board = QuantumWorld([p0, p1, p2])
assert board.peek() == [[SquareState.OCCUPIED,SquareState.OCCUPIED,SquareState.EMPTY]]

0 comments on commit 5298a11

Please sign in to comment.