@@ -89,7 +89,8 @@ def __init__(self):
8989 self .game_state = GameState .CONTINUES
9090 self .current_player = self .board .current_player
9191 self .debug_level = 3
92- # This variable is used to save the classical properties of the whole board before each move is
92+ # This variable is used to save the classical properties
93+ # of the whole board before each move is
9394 # made, so that if we later undo we could recover the earlier classical state.
9495 self .classical_properties_history : List [List [List [int ]]] = []
9596
@@ -144,7 +145,9 @@ def parse_input_string(str_to_parse: str) -> Tuple[List[str], List[str]]:
144145
145146 @classmethod
146147 def _is_in_palace (self , color : Color , x : int , y : int ) -> bool :
147- """Check if the given location is within palace. This check will be applied to all KING ans ADVISOR moves."""
148+ """Check if the given location is within palace.
149+
150+ This check will be applied to all KING ans ADVISOR moves."""
148151 return (
149152 x <= ord ("f" )
150153 and x >= ord ("d" )
@@ -212,7 +215,8 @@ def check_classical_rule(
212215 raise ValueError ("CANNON cannot move like this." )
213216 if len (classical_path_pieces ) > 0 :
214217 if len (classical_path_pieces ) > 1 :
215- # Invalid cannon move, since there could only be at most one classical piece between
218+ # Invalid cannon move,
219+ # since there could only be at most one classical piece between
216220 # the source (i.e. the cannon) and the target.
217221 raise ValueError ("CANNON cannot fire like this." )
218222 elif source_piece .color == target_piece .color :
@@ -246,18 +250,24 @@ def classify_move(
246250 classical_path_pieces_1 : List [str ],
247251 quantum_path_pieces_1 : List [str ],
248252 ) -> Tuple [MoveType , MoveVariant ]:
249- """Determines and returns the MoveType and MoveVariant. This function assumes that check_classical_rule()
253+ """Determines and returns the MoveType and MoveVariant.
254+
255+ This function assumes that check_classical_rule()
250256 has been called before this.
251257
252258 Args:
253259 sources: the list of names of the source pieces
254260 targets: the list of names of the target pieces
255- classical_path_pieces_0: the list of names of classical pieces from source_0 to target_0 (excluded)
256- quantum_path_pieces_0: the list of names of quantum pieces from source_0 to target_0 (excluded)
257- classical_path_pieces_1: the list of names of classical pieces from source_0 to target_1 (for split)
261+ classical_path_pieces_0: the list of names of classical pieces
262+ from source_0 to target_0 (excluded)
263+ quantum_path_pieces_0: the list of names of quantum pieces
264+ from source_0 to target_0 (excluded)
265+ classical_path_pieces_1: the list of names of classical pieces
266+ from source_0 to target_1 (for split)
258267 or from source_1 to target_0 (for merge) (excluded)
259- quantum_path_pieces_1: the list of names of quantum pieces from source_0 to target_1 (for split) or
260- from source_1 to target_0 (for merge) (excluded)
268+ quantum_path_pieces_1: the list of names of quantum pieces
269+ from source_0 to target_1 (for split) or
270+ from source_1 to target_0 (for merge) (excluded)
261271 """
262272 move_type = MoveType .UNSPECIFIED
263273 move_variant = MoveVariant .UNSPECIFIED
@@ -272,15 +282,15 @@ def classify_move(
272282 and source .type_ == Type .CANNON
273283 and target .color .value == 1 - source .color .value
274284 ):
275- # CANNON is special in that there has to be a platform between itself and the target
276- # to capture.
285+ # CANNON is special in that there has to be a platform
286+ # between itself and the target to capture.
277287 raise ValueError (
278288 "CANNON could not fire/capture without a cannon platform."
279289 )
280290 if not source .is_entangled and not target .is_entangled :
281- # This handles all classical cases, where no quantum piece is envolved.
282- # We don't need to further classify MoveVariant types since all classical cases
283- # will be handled in a similar way.
291+ # This handles all classical cases, where no quantum piece
292+ # is envolved. We don't need to further classify MoveVariant
293+ # types since all classical cases will be handled in a similar way.
284294 return MoveType .CLASSICAL , MoveVariant .CLASSICAL
285295 else :
286296 # If any of the source or target is entangled, this move is a JUMP.
@@ -319,7 +329,8 @@ def classify_move(
319329 raise ValueError (
320330 "Both sources need to be in quantum state in order to merge."
321331 )
322- # TODO(): Currently we don't support merge + excluded/capture, or cannon_merge_fire + capture. Maybe add support later.
332+ # TODO(): Currently we don't support merge + excluded/capture,
333+ # or cannon_merge_fire + capture. Maybe add support later.
323334 if len (classical_path_pieces_0 ) > 0 or len (classical_path_pieces_1 ) > 0 :
324335 raise ValueError ("Currently CANNON cannot merge while firing." )
325336 if target .type_ != Type .EMPTY :
@@ -336,7 +347,8 @@ def classify_move(
336347 elif len (targets ) == 2 :
337348 # Determine types for split cases.
338349 target_1 = self .board .board [targets [1 ]]
339- # TODO(): Currently we don't support split + excluded/capture, or cannon_split_fire + capture. Maybe add support later.
350+ # TODO(): Currently we don't support split + excluded/capture,
351+ # or cannon_split_fire + capture. Maybe add support later.
340352 if len (classical_path_pieces_0 ) > 0 or len (classical_path_pieces_1 ) > 0 :
341353 raise ValueError ("Currently CANNON cannot split while firing." )
342354 if target .type_ != Type .EMPTY or target_1 .type_ != Type .EMPTY :
@@ -355,7 +367,9 @@ def classify_move(
355367 return move_type , move_variant
356368
357369 def apply_move (self , str_to_parse : str ) -> None :
358- """Check if the input string is valid. If it is, determine the move type and variant and return the move."""
370+ """Check if the input string is valid.
371+
372+ If it is, determine the move type and variant and return the move."""
359373 sources , targets = self .parse_input_string (str_to_parse )
360374
361375 # Additional checks based on the current board.
@@ -372,7 +386,8 @@ def apply_move(self, str_to_parse: str) -> None:
372386 raise ValueError ("Two sources need to be the same type." )
373387 if len (targets ) == 2 :
374388 target_1 = self .board .board [targets [1 ]]
375- # TODO(): handle the case where a piece is split into the current piece and another piece, in which case two targets are different.
389+ # TODO(): handle the case where a piece is split into the
390+ # current piece and another piece, in which case two targets are different.
376391 if target_0 .type_ != target_1 .type_ :
377392 raise ValueError ("Two targets need to be the same type." )
378393 if target_0 .color != target_1 .color :
@@ -440,8 +455,11 @@ def apply_move(self, str_to_parse: str) -> None:
440455 CannonFire (classical_pieces_0 , quantum_pieces_0 )(source_0 , target_0 )
441456
442457 def next_move (self ) -> Tuple [bool , str ]:
443- """Check if the player wants to exit or needs help message. Otherwise parse and apply the move.
444- Returns True + output string if the move was made, otherwise returns False + output string.
458+ """Check if the player wants to exit or needs help message.
459+
460+ Otherwise parse and apply the move.
461+ Returns True + output string if the move was made,
462+ otherwise returns False + output string.
445463 """
446464 input_str = input (
447465 f"\n It is { self .players_name [self .current_player ]} 's turn to move: "
@@ -492,8 +510,9 @@ def update_board_by_sampling(self) -> List[float]:
492510 for col in "abcdefghi" :
493511 piece = self .board .board [f"{ col } { row } " ]
494512 prob = probs [row * num_cols + ord (col ) - ord ("a" )]
495- # TODO(): This threshold does not actually work right now since we have 100 sampling.
496- # Change it to be more meaningful values maybe when we do error mitigation.
513+ # TODO(): This threshold does not actually work right now
514+ # since we have 100 sampling. Change it to a more meaningful value
515+ # when we do error mitigation.
497516 if prob < 1e-3 :
498517 piece .reset ()
499518 probs [row * num_cols + ord (col ) - ord ("a" )] = 0
@@ -507,14 +526,16 @@ def game_over(self) -> None:
507526 if self .game_state != GameState .CONTINUES :
508527 return
509528 if self .board .flying_general_check ():
510- # If two KINGs are directly facing each other (i.e. in the same column) without any pieces in between, then the game ends. The other player wins.
529+ # If two KINGs are directly facing each other (i.e. in the same column)
530+ # without any pieces in between, then the game ends. The other player wins.
511531 self .game_state = GameState (1 - self .current_player )
512532 return
513533 # TODO(): add the following checks
514534 # - If player 0 made N repeatd back-and_forth moves in a row.
515535
516536 def save_snapshot (self ) -> None :
517- """Saves the current length of the effect history, qubit_remapping_dict, and the current classical states of all pieces."""
537+ """Saves the current length of the effect history,
538+ qubit_remapping_dict, and the current classical states of all pieces."""
518539 # Save the current length of the effect history and qubit_remapping_dict.
519540 self .board .board .save_snapshot ()
520541
@@ -529,8 +550,10 @@ def save_snapshot(self) -> None:
529550 self .classical_properties_history .append (snapshot )
530551
531552 def undo (self ) -> bool :
532- """Undo the last move, which includes reset quantum effects and classical properties, and remapping
533- qubits.
553+ """Undo the last move.
554+
555+ This includes reset quantum effects and classical properties,
556+ and remapping qubits.
534557
535558 Returns True if the undo is success, and False otherwise.
536559 """
@@ -543,8 +566,10 @@ def undo(self) -> bool:
543566 # length == 1 corresponds to the initial state, and no more undo could be made.
544567 return False
545568
546- # Recover the mapping of qubits to the last snapshot, remove any related post selection memory,
547- # and recover the effects up to the last snapshot (which was saved after the last move finished).
569+ # Recover the mapping of qubits to the last snapshot,
570+ # remove any related post selection memory,
571+ # and recover the effects up to the last snapshot
572+ # (which was saved after the last move finished).
548573 try :
549574 world .restore_last_snapshot ()
550575 except ValueError as err :
0 commit comments