-
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.
Merge pull request #158 from madcpf/add_enum
change enum.Piece to add color and language
- Loading branch information
Showing
5 changed files
with
159 additions
and
40 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,33 @@ | ||
# 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: | ||
return Type.symbol(self.type_, self.color, lang) | ||
|
||
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,49 @@ | ||
# 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] | ||
] |