def test_minimax(self): # Based on page 281 of lecture0.pdf board = [[EMPTY, X, O], [O, X, EMPTY], [X, EMPTY, O]] self.assertEqual(minimax(board), (2, 1))
else: title = f"Game Over: {winner} wins." elif user == player: title = f"Play as {user}" else: title = f"Computer thinking..." title = largeFont.render(title, True, white) titleRect = title.get_rect() titleRect.center = ((width / 2), 30) screen.blit(title, titleRect) # Check for AI move if user != player and not game_over: if ai_turn: time.sleep(0.5) move = ttt.minimax(board) board = ttt.result(board, move) ai_turn = False else: ai_turn = True # Check for a user move click, _, _ = pygame.mouse.get_pressed() if click == 1 and user == player and not game_over: mouse = pygame.mouse.get_pos() for i in range(3): for j in range(3): if (board[i][j] == ttt.EMPTY and tiles[i][j].collidepoint(mouse)): board = ttt.result(board, (i, j))
def test_minimax_prevent_o_from_winning(self): board = [[X, X, O], [O, O, EMPTY], [EMPTY, EMPTY, EMPTY]] self.assertEqual(minimax(board), (1, 2))
def test_minimax_tie(self): board = [[X, X, O], [O, O, X], [X, O, EMPTY]] self.assertEqual(minimax(board), (2, 2))
def test_minimax_x_wins(self): board = [[X, X, EMPTY], [O, O, EMPTY], [EMPTY, EMPTY, EMPTY]] self.assertEqual(minimax(board), (0, 2))
def test_minimax_terminal(self): board = [[X, X, O], [O, O, X], [X, O, X]] self.assertEqual(minimax(board), None)
else: title = f"Game Over: {winner} wins." elif user == player: title = f"Play as {user}" else: title = f"Computer thinking..." title = largeFont.render(title, True, white) titleRect = title.get_rect() titleRect.center = ((width / 2), 30) screen.blit(title, titleRect) # Check for AI move if user != player and not game_over: if ai_turn: time.sleep(0.5) mov_dirt = ttt.minimax(board, 0) move = (mov_dirt[0], mov_dirt[1]) board = ttt.result(board, move) ai_turn = False else: ai_turn = True # Check for a user move click, _, _ = pygame.mouse.get_pressed() if click == 1 and user == player and not game_over: mouse = pygame.mouse.get_pos() for i in range(3): for j in range(3): if (board[i][j] == ttt.EMPTY and tiles[i][j].collidepoint(mouse)): board = ttt.result(board, (i, j))