From 8a56c29f8dc94d71ad3586d748f2e676a4c3859d Mon Sep 17 00:00:00 2001 From: Simone Pierazzini Date: Sat, 9 Dec 2023 15:20:48 +0100 Subject: [PATCH] shortcut rotate moves at start when tile is full symmetric --- tng/game/fsm.py | 25 +++++-- tools/sample_match.py | 155 +++++++++++++++++++++++++++++++----------- 2 files changed, 135 insertions(+), 45 deletions(-) diff --git a/tng/game/fsm.py b/tng/game/fsm.py index bdb5e9b..86ff4db 100644 --- a/tng/game/fsm.py +++ b/tng/game/fsm.py @@ -65,11 +65,19 @@ def discover_start_tiles_place_tile( # to check for bounds: there are surely tiles to draw placed_tile = game.tile_holder[game.draw_index] - return ( - self._apply_place_tile(game, player, move, placed_tile, replace_allowed=False) - .new_phase(Phase.rotate_discovered_start_tile) - .draw_tile() - ) + game = self._apply_place_tile( + game, player, move, placed_tile, replace_allowed=False + ).draw_tile() + + if placed_tile in [Tile.straight_passage, Tile.t_passage]: + return game.new_phase(Phase.rotate_discovered_start_tile) + + start_pos = game.players[game.turn].pos + + if start_pos is None: + raise GameRuntimeError('current player without pos') + + return self._next_from_discover_start_tiles(game, start_pos) def rotate_discovered_start_tile_rotate_tile( self, game: Game, player: PlayerColor, move: RotateTile @@ -98,7 +106,10 @@ def rotate_discovered_start_tile_rotate_tile( # apply - cells = new_game.board.visible_cells_from(player_status.pos) + return self._next_from_discover_start_tiles(new_game, player_status.pos) + + def _next_from_discover_start_tiles(self, new_game: Game, start_pos: Position) -> Game: + cells = new_game.board.visible_cells_from(start_pos) if any(cell.tile is None for cell in cells): return new_game.new_phase(Phase.discover_start_tiles) @@ -107,7 +118,7 @@ def rotate_discovered_start_tile_rotate_tile( return new_game.new_phase(Phase.move_player).set_turn(0) else: - return new_game.new_phase(Phase.place_start).set_turn(game.turn + 1) + return new_game.new_phase(Phase.place_start).set_turn(new_game.turn + 1) def move_player_stay(self, game: Game, player: PlayerColor, move: Stay) -> Game: player_status = game.players[game.turn] diff --git a/tools/sample_match.py b/tools/sample_match.py index a46df3a..a200eba 100644 --- a/tools/sample_match.py +++ b/tools/sample_match.py @@ -4,7 +4,7 @@ from tng.game.factory import GameFactory from tng.game.fsm import TNGFSM -from tng.game.types import PlayerColor, Direction, Position, FallDirection +from tng.game.types import PlayerColor, Direction, Position, FallDirection, Tile from tng.game.moves import Move, PlaceTile, MoveType, RotateTile, Walk, Stay, Fall, Drop from tng.game.game import Game @@ -27,6 +27,85 @@ def run_sample_match(): PlayerColor.blue, PlayerColor.red, PlayerColor.green, PlayerColor.purple ) + assert game.tile_holder == [ + Tile.t_passage, + Tile.straight_passage, + Tile.t_passage, + Tile.four_way_passage, + Tile.straight_passage, + Tile.t_passage, + Tile.four_way_passage, + Tile.t_passage, + Tile.t_passage, + Tile.t_passage, + Tile.wax_eater, + Tile.gate, + Tile.t_passage, + Tile.four_way_passage, + Tile.key, + Tile.key, + Tile.wax_eater, + Tile.straight_passage, + Tile.t_passage, + Tile.wax_eater, + Tile.t_passage, + Tile.t_passage, + Tile.t_passage, + Tile.wax_eater, + Tile.gate, + Tile.straight_passage, + Tile.key, + Tile.key, + Tile.four_way_passage, + Tile.four_way_passage, + Tile.straight_passage, + Tile.t_passage, + Tile.wax_eater, + Tile.t_passage, + Tile.t_passage, + Tile.t_passage, + Tile.gate, + Tile.wax_eater, + Tile.wax_eater, + Tile.t_passage, + Tile.t_passage, + Tile.four_way_passage, + Tile.four_way_passage, + Tile.t_passage, + Tile.t_passage, + Tile.four_way_passage, + Tile.wax_eater, + Tile.four_way_passage, + Tile.four_way_passage, + Tile.t_passage, + Tile.t_passage, + Tile.straight_passage, + Tile.t_passage, + Tile.straight_passage, + Tile.gate, + Tile.wax_eater, + Tile.t_passage, + Tile.wax_eater, + Tile.t_passage, + Tile.wax_eater, + Tile.t_passage, + Tile.t_passage, + Tile.key, + Tile.t_passage, + Tile.straight_passage, + Tile.t_passage, + Tile.t_passage, + Tile.t_passage, + Tile.four_way_passage, + Tile.four_way_passage, + Tile.key, + Tile.straight_passage, + Tile.wax_eater, + Tile.t_passage, + Tile.t_passage, + Tile.straight_passage, + ] + moves = [ # start macro phase Move( # 0 @@ -82,142 +161,142 @@ def run_sample_match(): param=PlaceTile(move=MoveType.place_tile, pos=Position(x=3, y=1)), ), Move( # 13 - player=PlayerColor.green, - param=RotateTile(move=MoveType.rotate_tile, direction=Direction.n), - ), - Move( # 14 player=PlayerColor.purple, param=PlaceTile(move=MoveType.place_tile, pos=Position(x=5, y=1)), ), - Move( # 15 + Move( # 14 player=PlayerColor.purple, param=RotateTile(move=MoveType.rotate_tile, direction=Direction.n), ), - Move( # 16 + Move( # 15 player=PlayerColor.purple, param=PlaceTile(move=MoveType.place_tile, pos=Position(x=5, y=2)), ), - Move( # 17 + Move( # 16 player=PlayerColor.purple, param=RotateTile(move=MoveType.rotate_tile, direction=Direction.n), ), - Move( # 18 + Move( # 17 player=PlayerColor.purple, param=PlaceTile(move=MoveType.place_tile, pos=Position(x=4, y=1)), ), - Move( # 19 + Move( # 18 player=PlayerColor.purple, param=RotateTile(move=MoveType.rotate_tile, direction=Direction.s), ), # in game macro phase, move turn 1 - Move( # 20 + Move( # 19 player=PlayerColor.blue, param=Walk(move=MoveType.walk, direction=Direction.w), ), - Move( # 21 + Move( # 20 player=PlayerColor.blue, param=PlaceTile(move=MoveType.place_tile, pos=Position(x=1, y=4)), ), - Move( # 22 + Move( # 21 player=PlayerColor.red, param=Stay(move=MoveType.stay), ), - Move( # 23 + Move( # 22 player=PlayerColor.red, param=Fall(move=MoveType.fall, direction=FallDirection.column), ), - Move( # 24 + Move( # 23 player=PlayerColor.green, param=Walk(move=MoveType.walk, direction=Direction.e), ), - Move( # 25 + Move( # 24 player=PlayerColor.green, param=PlaceTile(move=MoveType.place_tile, pos=Position(x=3, y=0)), ), - Move( # 26 + Move( # 25 player=PlayerColor.green, param=RotateTile(move=MoveType.rotate_tile, direction=Direction.n), ), - Move( # 27 + Move( # 26 player=PlayerColor.green, param=PlaceTile(move=MoveType.place_tile, pos=Position(x=3, y=2)), ), - Move( # 28 + Move( # 27 player=PlayerColor.green, param=RotateTile(move=MoveType.rotate_tile, direction=Direction.e), ), - Move( # 29 + Move( # 28 player=PlayerColor.purple, param=Walk(move=MoveType.walk, direction=Direction.w), ), - Move( # 30 + Move( # 29 player=PlayerColor.purple, param=PlaceTile(move=MoveType.place_tile, pos=Position(x=4, y=0)), ), - Move( # 31 + Move( # 30 player=PlayerColor.blue, param=Walk(move=MoveType.walk, direction=Direction.w), ), - Move( # 32 + Move( # 31 player=PlayerColor.blue, param=PlaceTile(move=MoveType.place_tile, pos=Position(x=1, y=5)), ), - Move( # 33 + Move( # 32 player=PlayerColor.blue, param=PlaceTile(move=MoveType.place_tile, pos=Position(x=0, y=4)), ), - Move( # 34 + Move( # 33 player=PlayerColor.blue, param=RotateTile(move=MoveType.rotate_tile, direction=Direction.n), ), - Move( # 35 + Move( # 34 player=PlayerColor.blue, param=PlaceTile(move=MoveType.place_tile, pos=Position(x=1, y=3)), ), - Move( # 36 + Move( # 35 player=PlayerColor.red, param=Drop(move=MoveType.drop, place=4), ), - Move( # 37 + Move( # 36 player=PlayerColor.red, param=PlaceTile(move=MoveType.place_tile, pos=Position(x=3, y=5)), ), - Move( # 38 + Move( # 37 player=PlayerColor.red, param=PlaceTile(move=MoveType.place_tile, pos=Position(3, 3)), ), - Move( # 39 + Move( # 38 player=PlayerColor.red, param=PlaceTile(move=MoveType.place_tile, pos=Position(4, 4)), ), - Move( # 40 + Move( # 39 player=PlayerColor.red, param=RotateTile(move=MoveType.rotate_tile, direction=Direction.e), ), - Move( # 41 + Move( # 40 player=PlayerColor.green, param=Walk(move=MoveType.walk, direction=Direction.w), ), - Move( # 42 + Move( # 41 player=PlayerColor.green, param=Fall(move=MoveType.fall, direction=FallDirection.column), ), - Move( # 43 + Move( # 42 player=PlayerColor.purple, param=Walk(move=MoveType.walk, direction=Direction.w), ), - Move( # 44 + Move( # 43 player=PlayerColor.purple, param=PlaceTile(move=MoveType.place_tile, pos=Position(3, 0)), ), - Move( # 45 + Move( # 44 player=PlayerColor.purple, param=RotateTile(move=MoveType.rotate_tile, direction=Direction.n), ), - Move( # 46 + Move( # 45 player=PlayerColor.purple, param=PlaceTile(move=MoveType.place_tile, pos=Position(3, 2)), ), + Move( # 46 + player=PlayerColor.purple, + param=RotateTile(move=MoveType.rotate_tile, direction=Direction.e), + ), Move( # 47 player=PlayerColor.purple, param=PlaceTile(move=MoveType.place_tile, pos=Position(2, 1)), @@ -250,7 +329,7 @@ def run_sample_match(): player=PlayerColor.blue, param=PlaceTile(move=MoveType.place_tile, pos=Position(1, 0)), ), - Move( # 54 + Move( # 55 player=PlayerColor.red, param=Walk(move=MoveType.walk, direction=Direction.e), ),