def test_actions(self): actions_1 = ttt.actions(self.board_1) self.assertEqual(actions_1, {(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)}) actions_2 = ttt.actions(self.board_2) self.assertEqual(actions_2, {(0, 0), (0, 1), (1, 0), (1, 2), (2, 1), (2, 2)}) actions_3 = ttt.actions(self.board_3) self.assertEqual(actions_3, {(1, 2), (2, 0), (2, 1), (2, 2)})
def test_available_actions_after_move(coord, marker): init = ttt.initial_state() x, y = coord init[x][y] = marker actions = ttt.actions(init) assert len(actions) == 8 assert (x, y) not in actions
def test_get_actions(self): board = [[EMPTY, EMPTY, "O"], [EMPTY, "X", "X"], ["X", "O", "O"]] action = set() action.add((0, 0)) action.add((0, 1)) action.add((1, 0)) self.assertEqual(actions(board), action)
def test_actions_one_move_left(): board = [["X", EMPTY, "X"], ["O", "X", "O"], ["X", "O", "X"]] expected = set() expected.add((0, 1)) assert ttt.actions(board) == expected
def minimax(board): """ Returns the optimal action for the current player on the board. """ if terminal(board) is True: return None # Code for player O if player(board) is O: v = float("inf") optimal = [] for action in actions(board): actionvalue = MaxValue(result(board, action)) if actionvalue < v: optimal.clear() optimal.append(action) v = actionvalue elif actionvalue == v: optimal.append(action) i = random.randrange(len(optimal)) return optimal[i] # Code for player X else: """ As any first move is expected to tie playing optimally, just randomize the first move to be quicker To make it more interesting, we take a random move between all the optimal sollutions so that the computer wont always play the same game. """ if board == [[EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY]]: return (random.randrange(3), random.randrange(3)) else: v = float("-inf") optimal = [] for action in actions(board): actionvalue = MinValue(result(board, action)) if actionvalue > v: optimal.clear() optimal.append(action) v = actionvalue elif actionvalue == v: optimal.append(action) i = random.randrange(len(optimal)) return optimal[i]
def test_actions_no_moves_left(self): player_board = \ [[self.X, self.O, self.X], [self.O, self.O, self.X], [self.X, self.X, self.O]] avail_moves = None self.assertIsNone(avail_moves, ttt.actions(player_board))
def test_actions_game_in_progress(self): player_board = \ [[self.X, self.O, self.X], [self.O, self.O, self.EMPTY], [self.X, self.EMPTY, self.EMPTY]] avail_moves = set(((1, 2), (2, 1), (2, 2))) self.assertEqual(avail_moves, ttt.actions(player_board))
def test_actions(self): board = [[tictactoe.X, tictactoe.O, tictactoe.EMPTY], [tictactoe.EMPTY, tictactoe.X, tictactoe.EMPTY], [tictactoe.O, tictactoe.EMPTY, tictactoe.EMPTY]] moves = tictactoe.actions(board) print(moves)
def test_minimax(self): board = [[tictactoe.X, tictactoe.EMPTY, tictactoe.EMPTY], [tictactoe.EMPTY, tictactoe.EMPTY, tictactoe.EMPTY], [tictactoe.EMPTY, tictactoe.EMPTY, tictactoe.EMPTY]] bestMoves = tictactoe.actions(board) print(bestMoves)
def test_actions_empty_board(self): player_board = \ [[self.EMPTY, self.EMPTY, self.EMPTY], [self.EMPTY, self.EMPTY, self.EMPTY], [self.EMPTY, self.EMPTY, self.EMPTY]] avail_moves = set(((0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2))) self.assertEqual(avail_moves, ttt.actions(player_board))
def test_actions_empty_board(): board = [[EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY]] expected = set() for y in range(0, 3): for x in range(0, 3): expected.add((y, x)) assert ttt.actions(board) == expected
def rollout(self, node): """ Randomly plays from the state defined by node until the end of the game, returning the final outcome. """ # Available moves from this point board = node.board moves = ttt.actions(board) # Used to store the winner w = None # Play until there's a winner while w is None: # Pick a random move n = randint(low=0, high=len(moves)) action = moves[n] # Update the board (result automatically checks # whose go it is) board = ttt.result(board, action) # List all possible moves moves = ttt.actions(board) # See if anyone has won w = ttt.winner(board) # Break if we've run out of moves if len(moves) == 0: break if w is 'O': reward = 2 elif w is None: reward = 1 elif w is 'X': reward = 0 return reward
def test_actions_alternate(self): test_set = set(self.full_set) board = initial_state() value = X for i, row in enumerate(board): for j, cell in enumerate(row): board[i][j] = value value = X if value == O else O test_set.remove((i, j)) self.assertEqual(actions(board), test_set)
def test_actions(self): board = [ [tictactoe.X, tictactoe.O, tictactoe.EMPTY], [tictactoe.O, tictactoe.O, tictactoe.X], [tictactoe.X, tictactoe.EMPTY, tictactoe.EMPTY], ] # Check three actions result = tictactoe.actions(board) self.assertEqual(result, [(0, 2), (2, 1), (2, 2)]) # Check two actions board[2][2] = tictactoe.X result = tictactoe.actions(board) self.assertEqual(result, [(0, 2), (2, 1)]) # Check one actions board[0][2] = tictactoe.O result = tictactoe.actions(board) self.assertEqual(result, [(2, 1)])
def test_result(): board = [["X", "O", "X"], ["O", "O", "X"], ["X", EMPTY, "O"]] possibleMoves = ttt.actions(board) expected = [["X", "O", "X"], ["O", "O", "X"], ["X", "X", "O"]] initBoard = copy.deepcopy(board) assert ttt.result(board, possibleMoves.pop()) == expected assert board == initBoard
def minimax(board): """ Returns the optimal action for the current player on the board. To be quicker if we detect a winning move we take it, not taking into account other moves """ if terminal(board) is True: return None # Code for player O if player(board) is O: v = float("inf") for action in actions(board): actionvalue = MaxValue(result(board, action)) if actionvalue == -1: return action if actionvalue < v: optimal = action v = actionvalue return optimal # Code for player X else: """ As any first move is expected to tie playing optimally, just randomize the first move to be quicker """ if board == [[EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY]]: return (random.randrange(3), random.randrange(3)) else: v = float("-inf") for action in actions(board): actionvalue = MinValue(result(board, action)) if actionvalue == 1: return action if actionvalue > v: optimal = action v = actionvalue return optimal
def max_value(state, depth=0): if ttt.terminal(state): return (None, ttt.utility(state)) v = (None, -2) for action in ttt.actions(state): v = max( v, (action, min_value(ttt.result(state, action), depth + 1)[1] - (depth / 10)), key=lambda x: x[1]) return v
def minimax(board): """ Returns the optimal action for the current player on the board. """ if terminal(board) is True: return None else: options = [] for action in actions(board): options.append(action) i = random.randrange(len(options)) #print(f"{options} length= {len(options)}, chose {i}") return options[i]
def test_all_available_actions(): init = ttt.initial_state() actions = ttt.actions(init) assert len(actions) == 9
import tictactoe as t board = t.initial_state() board[0][2] = t.X board[1][1] = t.X board[2][0] = t.X print(t.utility(board)) print(t.terminal(board)) print(board) for row in board: print(row.count(t.O)) print (t.player(board)) print(t.actions(board)) for a in t.actions(board): print(t.result(board,a))
from tictactoe import initial_state, player, actions, result, winner, terminal, utility, MaxValue, MinValue X = "X" O = "O" EMPTY = None board = [[X, EMPTY, EMPTY], [EMPTY, O, EMPTY], [EMPTY, EMPTY, EMPTY]] for action in actions(board): print(f"{action} value of {MaxValue(result(board,action))}")
def test_actions_terminal_board(self): board = [[X, X, O], [O, O, O], [X, O, X]] expected = set() self.assertEqual(actions(board), expected)
def test_actions_end_game(self): board = [[X, X, O], [O, O, EMPTY], [X, O, X]] expected = {(1, 2)} self.assertEqual(actions(board), expected)
def test_actions_mid_game(self): board = [[X, EMPTY, EMPTY], [EMPTY, O, EMPTY], [X, EMPTY, EMPTY]] expected = {(0, 1), (0, 2), (1, 0), (1, 2), (2, 1), (2, 2)} self.assertEqual(actions(board), expected)
def test_actions_initial_state(self): expected = {(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)} self.assertEqual(actions(initial_state()), expected)
from tictactoe import EMPTY, actions, X, O emptyBoard = [[EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY]] action1 = {(0, 0), (1, 1), (1, 0), (0, 2), (2, 0), (1, 2), (2, 2), (2, 1), (0, 1)} if actions(emptyBoard) == action1: print("emptyBoard actions work") else: print("emptyBoard check failed") Board1 = [[EMPTY, X, EMPTY], [EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY]] action2 = {(0, 0), (1, 1), (1, 0), (0, 2), (2, 0), (1, 2), (2, 2), (2, 1)} if actions(Board1) == action2: print("1move actions work") else: print("1move check failed") Board2 = [[EMPTY, X, EMPTY], [EMPTY, EMPTY, O], [EMPTY, EMPTY, EMPTY]] action3 = {(0, 0), (1, 1), (1, 0), (0, 2), (2, 0), (2, 2), (2, 1)} if actions(Board2) == action3: print("2move actions work") else: print("2move check failed") fullBoard = [[X, X, X], [X, X, O], [X, X, X]] action4 = set() if actions(fullBoard) == action4: print("fullBoard actions work") else: print("fullBoard check failed")
import tictactoe as ttt print(str(ttt.actions(ttt.initial_state()))) print(str(ttt.minimax(ttt.initial_state())))
from tictactoe import initial_state from tictactoe import player from tictactoe import actions from tictactoe import result from tictactoe import winner from tictactoe import terminal from tictactoe import utility from tictactoe import minimax board = initial_state() actions = actions(board) winner = winner(board) terminal = terminal(board) utility = utility(board) minimax = minimax(board) print(minimax)
def test_actions_method_empty_board(self): actions_to_do = actions(initial_state()) for row in range(3): for col in range(3): self.assertTrue((row, col) in actions_to_do)
from tictactoe import initial_state import tictactoe as ttt EMPTY = None board = initial_state() board = [['X', 'X', 'O'], [EMPTY, 'O', EMPTY], [EMPTY, EMPTY, EMPTY]] player = ttt.player(board) moves = ttt.actions(board) print(f'board: {board}') while True: if ttt.terminal(board): break optimal = ttt.minimax(board) print(optimal) board = ttt.result(board, optimal) print(f'board: {board}') # print(ttt.utility(board))