-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move piece class to its own file and add test
- Loading branch information
Pengfei Chen
committed
Oct 2, 2023
1 parent
1d51af0
commit 5298a11
Showing
5 changed files
with
134 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] |