def coop_direction(board): if (rules.is_full(board)): return None m = 0 a = 0 for i in range(3): if rules.move_dir(i, board) != board: s = coop_score_dir(rules.move_dir(i, board), 4) if s > m: m = s a = i if m == 0: a = 3 return a
def test_move_dir(direction, board, expected): xboard = [board[i].copy() for i in range(rules.SIZE)] res = rules.move_dir(direction, xboard) assert (res is not xboard) assert_equal(res, expected) start_test("purity", INDENT) assert_equal(board, xboard)
def coop_score_dir(board, depth): if depth > 0: m = 0 a = 0 for i in range(3): if rules.move_dir(i, board) != board: s = coop_score_tile(rules.move_dir(i, board), depth - 1) if s > m: m = s a = i if m == 0: a = 3 ret = rules.move_dir(a, board) return (basic_coop_score(ret)) else: return basic_coop_score(board)
def basic_coop_direction(board): possibles = [ d for d in rules.DIRECTIONS if rules.move_dir_possible(d, board) ] if possibles != [3] and 3 in possibles: possibles.remove(3) #Ne jamais jouer haut quand on peut l'éviter score = {basic_coop_score(rules.move_dir(d, board)): d for d in possibles} return score[max(score)]
def game_direction_first(dir_player, tile_player, board): i = 0 while not rules.game_over(board): direction = dir_player(board) assert (direction is not None) #On peut jouer assert (0 <= direction < 4) #On ne peut jouer que 4 directions assert (rules.move_dir_possible(direction, board)) board = rules.move_dir(direction, board) tuile = tile_player(board) assert (tuile is not None) rules.move_tile(tuile, board) i += 1 return (1 << rules.max_tile(board), i) #1 << a is 2 ** a
def game_tile_first(dir_player, tile_player, board): """Plays a game on board when tile_player is the first player""" board2 = [board[i].copy() for i in range(rules.SIZE)] if not dir_player(board): assert rules.game_over(board) is True i = 0 while tile_player(board2) is not None: rules.move_tile(tile_player(board2), board2) dir = dir_player(board2) i += 1 if dir is not None: board2 = rules.move_dir(dir, board2) else: assert rules.game_over(board2) is True return 2**rules.max_tile(board2), i
def game_direction_first(dir_player, tile_player, board): """Plays a game on board when dir_player is the first player""" if not dir_player(board): assert rules.is_full(board) is True i = 0 while dir_player(board) is not None: board = rules.move_dir(dir_player(board), board) tile = tile_player(board) if tile is not None: rules.move_tile(tile_player(board), board) i += 1 else: assert rules.move_tile is None return 2**rules.max_tile(board), i
def move_dir(direction): global player, board old = board board = rules.move_dir(direction, old) if board is old: if is_interactive: help_interactive("direction {0} does not change the board".format( rules.DIR_NAME[direction])) else: print("ERROR: direction {0} is invalid !".format( rules.DIR_NAME[direction])) assert False else: update() player = PLAY_TILE run_next_player()
def test_move_dir_possible(direction, board, expected): if not STAGE_SLIDE_IS_OVER: assert_equal(rules.move_dir_possible(direction, board), expected) if expected: start_test("game not over", INDENT) assert not rules.game_over(board) return start_test("mimicking with rules.move_dir", INDENT) xboard = [board[i].copy() for i in range(rules.SIZE)] res = rules.move_dir(direction, xboard) if expected: if res == xboard: print("Error of rules.slide on input", xboard) print("Output is the same while it should be different") assert res != xboard elif res is not xboard: print("Error of rules.slide on input", xboard) print("Output is ", xboard) assert xboard is board start_test("purity", INDENT) assert_equal(board, xboard)