def play(action_modes): """1ゲームの実行 """ # 3目並べの状態を保持するクラス"State"を初期化する。 state = game.State() # グラフの初期化 # G = nx.DiGraph() # コマの位置をbinaryでノードに加える # G.add_node(state.get_pieces_for_binary(), label="pieces_for_binary") # ゲーム終了までループ。(Stateクラスのis_doneで確認) while not state.is_done(): # 行動前の状態のbinary binary_state = state.get_pieces_for_binary() # 行動の取得 action_mode = action_modes[0] if state.is_first_player( ) else action_modes[1] action = ai.action(state, action_mode) # 行動を状態に反映させた次の状態に更新する。 state = state.next(action) print(state) print() # # ノードの追加 # G.add_node(state.get_pieces_for_binary()) # # 枝の追加 # G.add_edge(binary_state, state.get_pieces_for_binary()) # # ネットワークの出力 # nx.readwrite.gml.write_gml(G, "game.mgl") # 先手プレイヤーのポイントを返す return first_player_point(state)
def rotate270_state(state): my_small_pieces = rotate270(state.my_small_pieces) enemy_small_pieces = rotate270(state.enemy_small_pieces) my_large_pieces = rotate270(state.my_large_pieces) enemy_large_pieces = rotate270(state.enemy_large_pieces) convert_state = game.State(my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces) return convert_state
def horizontal_state(state): my_small_pieces = horizontal(state.my_small_pieces) enemy_small_pieces = horizontal(state.enemy_small_pieces) my_large_pieces = horizontal(state.my_large_pieces) enemy_large_pieces = horizontal(state.enemy_large_pieces) convert_state = game.State(my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces) return convert_state
def upper_right_state(state): my_small_pieces = upper_right(state.my_small_pieces) enemy_small_pieces = upper_right(state.enemy_small_pieces) my_large_pieces = upper_right(state.my_large_pieces) enemy_large_pieces = upper_right(state.enemy_large_pieces) convert_state = game.State(my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces) return convert_state
def test_upper_right_state(self): patterns = [ # 空 # small # --- # --- # --- # large # --- # --- # --- # # small # --- # --- # --- # large # --- # --- # --- (([0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]), ([0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0])), # コマあり1 # small # ox- # --- # --- # large # --- # x-- # -o- # # small # --- # --x # --o # large # --- # o-- # -x- (([1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0]), ([0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0])), ] for input_param, expect_param in patterns: my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces = input_param state = game.State(my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces) expect = expect_param # 変換後の盤面をもつstateのxx_xx_pieces state = convert.upper_right_state(state) actual = (state.my_small_pieces, state.enemy_small_pieces, state.my_large_pieces, state.enemy_large_pieces) self.assertEqual(expect, actual)
def main(): print() print("盤面の列挙を行います。") print("出力するファイル名を打ち込んで下さい(.gml含む):", end="") filename = input() if not filename: print("filenameが正常に入力されませんでした") # 3目並べの状態を保持するクラス"State"を初期化する。 state = game.State() # グラフの初期化 G = nx.DiGraph() # コマの位置をbinaryでノードに加える G.add_node(state.get_pieces_for_binary()) # ゲーム終了までループ。(Stateクラスのis_doneで確認) while not state.is_done(): # 行動前の状態のbinary # binary_state = state.get_pieces_for_binary() # # # 行動の取得 # action_modes = ("MiniMax", "Random") # action_mode = action_modes[0] if state.is_first_player() else action_modes[1] # action = ai.action(state, action_mode) # # # 行動を状態に反映させた次の状態に更新する。 # state = state.next(action) print(state) print() # ノードの追加 G.add_node(state.get_pieces_for_binary()) # 枝の追加 G.add_edge(binary_state, state.get_pieces_for_binary()) # ネットワークの出力 nx.readwrite.gml.write_gml(G, "output/" + filename)
def test_piece_count(self): patterns = [ # 0つ ([0, 0, 0, 0, 0, 0, 0, 0, 0], 0), # piece # --- # --- # --- # 1つ ([1, 0, 0, 0, 0, 0, 0, 0, 0], 1), # piece # o-- # --- # --- # 2つ ([0, 0, 1, 0, 1, 0, 0, 0, 0], 2), # piece # --o # -o- # --- # 9つ ([1, 1, 1, 1, 1, 1, 1, 1, 1], 9), # piece # ooo # ooo # ooo ] for input_param, expect_param in patterns: pieces = input_param state = game.State() expect = expect_param # num of pieces actual = state.piece_count(pieces) self.assertEqual(expect, actual)
def test_mini_max(self): patterns = [ # 勝ち盤面 # small # --- # --- # --- # large # o-- # oo- # o-- # ↓ # # visible # o-- # oo- # o-- (([0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]), 1), # 負け盤面 # small # --- # --- # --- # large # x-- # xx- # x-- # ↓ # # visible # x-- # xx- # x-- (([0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 1, 0, 0]), -1), # 引き分け盤面 # small # oxo # xxo # oox # large # oxo # xxo # oox # ↓ # # visible # oxo # xxo # oox (([1, 0, 1, 0, 0, 1, 1, 1, 0], [0, 1, 0, 1, 1, 0, 0, 0, 1], [1, 0, 1, 0, 0, 1, 1, 1, 0], [0, 1, 0, 1, 1, 0, 0, 0, 1]), 0), # 自分のコマを置いて勝ち # small # x-- # xx- # --- # large # o-- # oo- # --- # ↓ # # visible # o-- # oo- # --- (([0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]), 1), # 相手がどこに置いても、次に自分がコマを置いて勝ち # small # x-- # x-- # --- # large # o-- # oo- # --- # ↓ # # visible # o-- # oo- # --- (([0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]), 1), # 自分がどこに置いても、次に相手がコマを置いて負け # small # o-- # oo- # --- # large # x-- # xx- # --- # ↓ # # visible # x-- # xx- # --- (([1, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0]), -1), ] for input_param, expect_param in patterns: my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces = input_param state = game.State(my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces) expect = expect_param # 負けなら-1, 引き分けなら0 NegaMaxなので、-1をかける actual = value.mini_max(state) self.assertEqual(expect, actual)
def test___str__(self): patterns = [ # 空 # small # --- # --- # --- # large # --- # --- # --- # ↓ # # visible # --- # --- # --- ((None, None, None, None), " small\n" " ---\n" " ---\n" " ---\n " "\r large\n" " ---\n" " ---\n" " ---\n " "\r ↓\n" "\n" "visible\n" " ---\n" " ---\n" " ---\n "), # smallのみ # small # ox- # --- # --- # large # --- # --- # --- # ↓ # # visible # ox- # --- # --- (([1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], None, None), " small\n" " ox-\n" " ---\n" " ---\n " "\r large\n" " ---\n" " ---\n" " ---\n " "\r ↓\n" "\n" "visible\n" " ox-\n" " ---\n" " ---\n "), # largeのみ # small # --- # --- # --- # large # ox- # --- # --- # ↓ # # visible # ox- # --- # --- ((None, None, [1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0]), " small\n" " ---\n" " ---\n" " ---\n " "\r large\n" " ox-\n" " ---\n" " ---\n " "\r ↓\n" "\n" "visible\n" " ox-\n" " ---\n" " ---\n "), # small, large重なりなし # small # ox- # --- # --- # large # --- # ox- # --- # ↓ # # visible # ox- # ox- # --- (([1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0]), " small\n" " ox-\n" " ---\n" " ---\n " "\r large\n" " ---\n" " ox-\n" " ---\n " "\r ↓\n" "\n" "visible\n" " ox-\n" " ox-\n" " ---\n "), # small, large重なりあり # small # ox- # --- # ox- # large # --- # ox- # ox- # ↓ # # visible # ox- # ox- # ox- (([1, 0, 0, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0, 0, 1, 0]), " small\n" " ox-\n" " ---\n" " ox-\n " "\r large\n" " ---\n" " ox-\n" " ox-\n " "\r ↓\n" "\n" "visible\n" " ox-\n" " ox-\n" " ox-\n "), # small, large重なりあり色重なり # small # oxo # --x # ox- # large # --o # oxx # ox- # ↓ # # visible # oxo # oxx # ox- (([1, 0, 1, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 1, 0, 1, 0]), " small\n" " oxo\n" " --x\n" " ox-\n " "\r large\n" " --o\n" " oxx\n" " ox-\n " "\r ↓\n" "\n" "visible\n" " oxo\n" " oxx\n" " ox-\n "), ] for input_param, expect_param in patterns: my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces = input_param state = game.State(my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces) expect = expect_param # expect_param == string actual = str(state) self.assertEqual(expect, actual)
def test_get_pieces_for_binary(self): patterns = [ # 空 # small # --- # --- # --- # large # --- # --- # --- # ↓ # # visible # --- # --- # --- ((None, None, None, None), 0), # my_small_piecesで構成される盤面 # small # o-- # oo- # ooo # large # --- # --- # --- # ↓ # # visible # o-- # oo- # ooo (([1, 0, 0, 1, 1, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0], None, None), 0b111_011_001_000_000_000_000_000_000_000_000_000), # enemy_small_piecesで構成される盤面 # small # x-- # xx- # xxx # large # --- # --- # --- # ↓ # # visible # x-- # xx- # xxx (([0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 1, 1, 1], None, None), 0b000_000_000_111_011_001_000_000_000_000_000_000), # my_large_piecesで構成される盤面 # small # --- # --- # --- # large # o-- # oo- # ooo # ↓ # # visible # o-- # oo- # ooo (([0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0]), 0b000_000_000_000_000_000_111_011_001_000_000_000), # enemy_large_piecesで構成される盤面 # small # --- # --- # --- # large # x-- # xx- # xxx # ↓ # # visible # x-- # xx- # xxx (([0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 1, 1, 1]), 0b000_000_000_000_000_000_000_000_000_111_011_001), # my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_piecesで構成される盤面 # small # ox- # --- # ox- # large # --- # ox- # ox- # ↓ # # visible # ox- # ox- # ox- (([1, 0, 0, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0, 0, 1, 0]), 0b001_000_001_010_000_010_001_001_000_010_010_000), ] for input_param, expect_param in patterns: my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces = input_param state = game.State(my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces) expect = expect_param # bit化した盤面 actual = state.get_pieces_for_binary() self.assertEqual(expect, actual)
def test_get_pieces_for_tuple(self): patterns = [ # 空 # small # --- # --- # --- # large # --- # --- # --- # ↓ # # visible # --- # --- # --- ((None, None, None, None), ((0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0))), # 空 # small # --- # --- # --- # large # --- # --- # --- # ↓ # # visible # --- # --- # --- ((None, None, None, None), ((0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0))), # small, large それぞれのコマあり # small # o-x # o-x # --- # large # --- # x-o # x-o # ↓ # # visible # --- # x-o # x-o (([1, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 1], [0, 0, 0, 1, 0, 0, 1, 0, 0]), ((1, 0, 0, 1, 0, 0, 0, 0, 0), (0, 0, 1, 0, 0, 1, 0, 0, 0), (0, 0, 0, 0, 0, 1, 0, 0, 1), (0, 0, 0, 1, 0, 0, 1, 0, 0))), ] for input_param, expect_param in patterns: my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces = input_param state = game.State(my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces) expect = expect_param # expect_param == string actual = state.get_pieces_for_tuple() self.assertEqual(expect, actual)
def test_legal_actions(self): patterns = [ # 空いているマスにはおける # small # --- # --- # --- # large # --- # --- # --- # ↓ # # visible # --- # --- # --- ((None, None, None, None), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]), # 同じ大きさのコマがあるところにはおけない(small) # small # ox- # --- # --- # large # --- # --- # --- # ↓ # # visible # ox- # --- # --- (([1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], None, None), [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]), # 同じ大きさのコマがあるところにはおけない(large) # small # --- # --- # --- # large # ox- # --- # --- # ↓ # # visible # ox- # --- # --- ((None, None, [1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0]), [2, 3, 4, 5, 6, 7, 8, 11, 12, 13, 14, 15, 16, 17]), # largeがあるマスにsmallはおけない, smallがあるマスにlargeはおける(small, large重なりなし) # ox- # --- # --- # large # --- # ox- # --- # ↓ # # visible # ox- # ox- # --- (([1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0]), [2, 5, 6, 7, 8, 9, 10, 11, 14, 15, 16, 17]), # largeがあるマスにsmallはおけない, smallがあるマスにlargeはおける(small, large重なりあり) # small # ox- # --- # ox- # large # --- # ox- # ox- # ↓ # # visible # ox- # ox- # ox- (([1, 0, 0, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0, 0, 1, 0]), [2, 5, 8, 9, 10, 11, 14, 17]) ] for input_param, expect_param in patterns: my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces = input_param state = game.State(my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces) expect = expect_param # expect_param == [0, 1, 2, 3, ... , 16, 17] actual = state.legal_actions() self.assertEqual(expect, actual)
def test_is_draw(self): patterns = [ # 空 # small # --- # --- # --- # large # --- # --- # --- # ↓ # # visible # --- # --- # --- ((None, None, None, None), False), # 8つ # small # oxo # x-x # oxo # large # --- # --- # --- # ↓ # # visible # oxo # x-x # oxo (([1, 0, 1, 0, 0, 0, 1, 0, 1], [0, 1, 0, 1, 0, 1, 0, 1, 0], None, None), False), # 9つ # small # xox # oox # xxo # large # --- # --- # --- # ↓ # # visible # xox # oox # xxo (([0, 1, 0, 1, 1, 0, 0, 0, 1], [1, 0, 1, 0, 0, 1, 1, 1, 0], None, None), True), # 10つ # small # xox # oox # xxo # large # --- # -o- # --- # ↓ # # visible # xox # oox # xxo (([0, 1, 0, 1, 1, 0, 0, 0, 1], [1, 0, 1, 0, 0, 1, 1, 1, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], None), True), ] for input_param, expect_param in patterns: my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces = input_param state = game.State(my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces) expect = expect_param # True or False actual = state.is_done() self.assertEqual(expect, actual)
def test_is_lose(self): patterns = [ # 負けていない(空) # small # --- # --- # --- # large # --- # --- # --- # ↓ # # visible # --- # --- # --- ((None, None, None, None), False), # 負けていない(oなし、x揃わず) # small # -x- # x-x # -x- # large # --- # --- # --- # ↓ # # visible # -x- # x-x # -x- (([0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 1, 0, 1, 0, 1, 0], None, None), False), # 負けていない(o,x揃わず) # small # xox # xox # oxo # large # --- # --- # --- # ↓ # # visible # xox # xox # oxo (([0, 1, 0, 0, 1, 0, 1, 0, 1], [1, 0, 1, 1, 0, 1, 0, 1, 0], None, None), False), # 勝っている(oが揃う,x揃わず=負けていない) # small # xox # ooo # xox # large # --- # --- # --- # ↓ # # visible # small # xox # ooo # xox (([0, 1, 0, 1, 1, 1, 0, 1, 0], [1, 0, 1, 0, 0, 0, 1, 0, 1], None, None), False), # 縦並び(0,3,6) # small # x-- # x-- # x-- # large # --- # --- # --- # ↓ # # visible # x-- # x-- # x-- ((None, [1, 0, 0, 1, 0, 0, 1, 0, 0], None, None), True), # 縦並び(1,4,7) # small # -x- # -x- # -x- # large # --- # --- # --- # ↓ # # visible # -x- # -x- # -x- ((None, [0, 1, 0, 0, 1, 0, 0, 1, 0], None, None), True), # 縦並び(2,5,8) # small # --x # --x # --x # large # --- # --- # --- # ↓ # # visible # --x # --x # --x ((None, [0, 0, 1, 0, 0, 1, 0, 0, 1], None, None), True), # 横並び(0,1,2) # small # xxx # --- # --- # large # --- # --- # --- # ↓ # # visible # xxx # --- # --- ((None, [1, 1, 1, 0, 0, 0, 0, 0, 0], None, None), True), # 横並び(3,4,5) # small # --- # xxx # --- # large # --- # --- # --- # ↓ # # visible # --- # xxx # --- ((None, [0, 0, 0, 1, 1, 1, 0, 0, 0], None, None), True), # 横並び(6,7,8) # small # --- # --- # xxx # large # --- # --- # --- # ↓ # # visible # --- # --- # xxx ((None, [0, 0, 0, 0, 0, 0, 1, 1, 1], None, None), True), # 斜め並び1(0,4,8) # small # x-- # -x- # --x # large # --- # --- # --- # ↓ # # visible # x-- # -x- # --x ((None, [1, 0, 0, 0, 1, 0, 0, 0, 1], None, None), True), # 斜め並び2(2,4,6) # small # --x # -x- # x-- # large # --- # --- # --- # ↓ # # visible # --x # -x- # x-- ((None, [0, 0, 1, 0, 1, 0, 1, 0, 0], None, None), True), ] for input_param, expect_param in patterns: my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces = input_param state = game.State(my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces) expect = expect_param # True or False actual = state.is_lose() self.assertEqual(expect, actual)
def test___init__(self): patterns = [ # 空 # small # --- # --- # --- # large # --- # --- # --- # ↓ # # visible # --- # --- # --- ((None, None, None, None), ([0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0])), # smallのみ # small # ox- # --- # --- # large # --- # --- # --- # ↓ # # visible # ox- # --- # --- (([1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]), ([1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0])), # largeのみ # small # --- # --- # --- # large # ox- # --- # --- # ↓ # # visible # ox- # --- # --- (([0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0]), ([0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0])), # small, large重なりなし # small # ox- # --- # --- # large # --- # ox- # --- # ↓ # # visible # ox- # ox- # --- (([1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0]), ([1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [1, 0, 0, 1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0, 0, 0, 0])), # small, large重なりあり # small # ox- # --- # ox- # large # --- # ox- # ox- # ↓ # # visible # ox- # ox- # ox- (([1, 0, 0, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0, 0, 1, 0]), ([1, 0, 0, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0, 0, 1, 0], [1, 0, 0, 1, 0, 0, 1, 0, 0], [0, 1, 0, 0, 1, 0, 0, 1, 0])), # # small, large重なりあり色重なり # small # oxo # --x # ox- # large # --o # oxx # ox- # ↓ # # visible # oxo # oxx # ox- (([1, 0, 1, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 1, 0, 1, 0]), ([1, 0, 1, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 1, 0, 1, 0], [1, 0, 1, 1, 0, 0, 1, 0, 0], [0, 1, 0, 0, 1, 1, 0, 1, 0])), ] for input_param, expect_param in patterns: my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces = input_param state = game.State(my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces) expect = expect_param # my_xx_pieces, enemy_xx_pieces, ... , enemy_xx_pieces actual = state.my_small_pieces, state.enemy_small_pieces, state.my_large_pieces, state.enemy_large_pieces, state.my_visible_pieces, state.enemy_visible_pieces self.assertEqual(expect, actual)
def test_normalize_state(self): patterns = [ # 空 # small # --- # --- # --- # large # --- # --- # --- # # small # --- # --- # --- # large # --- # --- # --- (([0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]), 0b_000_000_000_000_000_000_000_000_000_000_000_000), # my_small_pieces(中央) # small # --- # -o- # --- # normalize_pieces # small # --- # -o- # --- (([0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]), # 0b_ 000_010_000 _000_000_000_000_000_000_000_000_000 0b_000_010_000_000_000_000_000_000_000_000_000_000), # my_small_pieces(角) # small # o-- # --- # --- # normalize_pieces # small # --- # --- # --o (([1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]), # 0b_ 100_000_000 _000_000_000_000_000_000_000_000_000 0b_000_000_001_000_000_000_000_000_000_000_000_000), # my_small_pieces(辺) # small # -o- # --- # --- # normalize_pieces # small # --- # --- # -o- (([0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]), # 0b_ 010_000_000 _000_000_000_000_000_000_000_000_000 0b_000_000_010_000_000_000_000_000_000_000_000_000), # enemy_small_pieces(中央) # small # --- # -x- # --- # normalize_pieces # small # --- # -x- # --- (([0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]), # 0b_000_000_000_ 000_010_000 _000_000_000_000_000_000 0b_000_000_000_000_010_000_000_000_000_000_000_000), # enemy_small_pieces(角) # small # x-- # --- # --- # normalize_pieces # small # --- # --- # --x (([0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]), # 0b_000_000_000_ 100_000_000 _000_000_000_000_000_000 0b_000_000_000_000_000_001_000_000_000_000_000_000), # enemy_small_pieces(辺) # small # -x- # --- # --- # normalize_pieces # small # --- # --- # -x- (([0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]), # 0b_000_000_000_ 010_000_000 _000_000_000_000_000_000 0b_000_000_000_000_000_010_000_000_000_000_000_000), # my_large_pieces(中央) # large # --- # -o- # --- # normalize_pieces # large # --- # -o- # --- (([0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]), # 0b_000_000_000_000_000_000_ 000_010_000 _000_000_000 0b_000_000_000_000_000_000_000_010_000_000_000_000), # my_large_pieces(角) # large # o-- # --- # --- # normalize_pieces # large # --- # --- # --o (([0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0],[1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]), # 0b_000_000_000_000_000_000_ 100_000_000 _000_000_000 0b_000_000_000_000_000_000_000_000_001_000_000_000), # my_large_pieces(辺) # large # -o- # --- # --- # normalize_pieces # large # --- # --- # -o- (([0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]), # 0b_000_000_000_000_000_000_ 010_000_000 _000_000_000 0b_000_000_000_000_000_000_000_000_010_000_000_000), # enemy_large_pieces(中央) # large # --- # -x- # --- # normalize_pieces # large # --- # -x- # --- (([0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0],), # 0b_000_000_000_000_000_000_000_000_000_ 000_010_000 0b_000_000_000_000_000_000_000_000_000_000_010_000), # enemy_large_pieces(角) # large # x-- # --- # --- # normalize_pieces # large # --- # --- # --x (([0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0]), # 0b_000_000_000_000_000_000_000_000_000_ 100_000_000 0b_000_000_000_000_000_000_000_000_000_000_000_001), # enemy_large_pieces(辺) # large # -x- # --- # --- # normalize_pieces # large # --- # --- # -x- (([0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0]), # 0b_000_000_000_000_000_000_000_000_000_010_000_000 0b_000_000_000_000_000_000_000_000_000_000_000_010), # my_small_piecesとenemy_small_pieces(それぞれrotate180, rotate270で最も小さくなるが、組み合わせだとrotate180が最小) # small # o-- # x-- # --- # normalize_pieces # small # --- # --- # -xo (([1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]), # 0b_ 100_000_000 _ 000_100_000 _000_000_000_010_000_000 0b_000_000_001_000_000_010_000_000_000_000_000_000), # my_large_piecesとenemy_large_pieces(それぞれhorizontal, rotate90で最も小さくなるが、組み合わせだとhorizontalが最小) # large # -oo # --x # --- # normalize_pieces # large # --- # --x # -oo (([0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0]), # 0b_000_000_000_000_000_000 _011_000_000 _ 000_001_000 0b_000_000_000_000_000_000_000_000_011_000_001_000), # my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces(組み合わせだとupper_leftが最小) # small # --x # --o # --o # large # -oo # --x # --- # normalize_pieces # small # --- # --- # xoo # large # --- # o-- # ox- (([0, 0, 0, 0, 0, 1, 0, 0, 1], [0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0]), # 0b_ 000_001_001 _ 001_000_000 _ 011_000_000 _ 000_001_000 0b_000_000_011_000_000_100_000_100_100_000_000_010), ] for input_param, expect_param in patterns: my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces = input_param state = game.State(my_small_pieces, enemy_small_pieces, my_large_pieces, enemy_large_pieces) expect = expect_param # 正規化したstateのpiecesのbinary表現 state = convert.normalize_state(state) actual = state.get_pieces_for_binary() self.assertEqual(bin(expect), bin(actual))