def play_game(player_one_algorithm, player_two_algorithm): player_one = Player() player_one.move_function = player_one_algorithm player_one.number = 1 player_two = Player() player_two.move_function = player_two_algorithm player_two.number = 2 player_iter = iter([ player_one, player_two, player_one, player_two, player_one, player_two, player_one, player_two, player_one, ]) board = [[0,0,0],[0,0,0],[0,0,0]] for player in player_iter: row, column = player.make_move(board) board[row][column] = player.number winner = detect_win(board) if winner: print 'player ' + str(winner) + ' wins.' break else: print 'cat game. nobody wins.'
def test_no_win(): assert 0 == detect_win([ [1,0,2], [0,2,2], [0,0,1], ]) assert 0 == detect_win([ [1,1,0], [0,2,1], [2,1,2], ]) assert 0 == detect_win([ [2,2,1], [0,2,0], [1,0,0], ]) assert 0 == detect_win([ [0,1,2], [1,2,0], [1,0,0], ])
def test_horizontal_win(): assert 1 == detect_win([ [0,0,0], [1,1,1], [0,2,2], ]) assert 2 == detect_win([ [0,0,0], [0,1,1], [2,2,2], ]) assert 1 == detect_win([ [0,0,0], [1,1,1], [2,2,0], ]) assert 2 == detect_win([ [0,0,0], [1,1,0], [2,2,2], ])
def test_diagonal_win(): assert 1 == detect_win([ [1,0,2], [0,1,2], [0,0,1], ]) assert 2 == detect_win([ [2,1,0], [0,2,1], [0,0,2], ]) assert 1 == detect_win([ [2,2,1], [0,1,0], [1,0,0], ]) assert 2 == detect_win([ [0,1,2], [1,2,0], [2,0,0], ])
def test_vertical_win(): assert 1 == detect_win([ [0,1,0], [0,1,2], [0,1,2], ]) assert 2 == detect_win([ [0,0,2], [0,1,2], [0,1,2], ]) assert 1 == detect_win([ [0,1,2], [0,1,2], [0,1,0], ]) assert 2 == detect_win([ [0,1,2], [0,1,2], [0,2,2], ])
def build_scenario_map(board, turn_number, player_number): hashed_board = board_hash(board) if hashed_board in scenario_map: return scenario_map[hashed_board][3] else: winning_player = detect_win(board) if winning_player: # somebody won scenario_map[hashed_board] = ([], [], [], winning_player) return winning_player else: if 9 == turn_number: #obvious tie game scenario_map[hashed_board] = ([], [], [], 0) return 0 else: #something will happen in the future decisions = loopyblar(board, turn_number, player_number) scenario_map[hashed_board] = decisions return decisions[3]