player vs ai tic-tac-toe game integrated with a gui using minimax algorithm in matlab

Here's a sample code to implement a Player vs AI tic-tac-toe game with GUI using Minimax algorithm in MATLAB:

main.m
% tic-tac-toe game with minimax algorithm
% Player vs. AI

% Initializing the game board
board = [' ',' ',' '; ' ',' ',' '; ' ',' ',' '];
player = 'X';
winner = '';
depth = 0;

% Create a GUI window for the game board
f = figure('Name','Tic Tac Toe','Units','Normalized','OuterPosition',[0 0.1 0.5 0.8],...
           'MenuBar','none','ToolBar','none','NumberTitle','off');

% Draw game board in GUI
for i=1:3
    for j=1:3
        pos = [0.2+(j-1)*0.2, 0.7-i*0.2, 0.2, 0.2];
        uicontrol('Style','pushbutton','Units','Normalized','Position',pos,...
                  'String',board(i,j),'Fontsize',30,'Callback',{@button_callback,i,j});
    end
end

% Function to respond to button clicks on game board
function button_callback(source,eventdata,i,j)
    % Check if the button has been clicked already
    if source.String~=' '
        return;
    end
    % Update board with player's position
    global board player winner depth;
    board(i,j) = player;
    source.String = player;
    % Switch player for next turn
    if player=='X'
        player = 'O';
    else
        player = 'X';
    end
    depth = depth + 1;
    % Check for winner or tie game
    winner = check_winner(board);
    if winner~=' '
        uialert(source.Parent,winner,'Game Over');
        return;
    elseif depth==9
        uialert(source.Parent,'Tie Game','Game Over');
        return;
    end
    % AI makes move
    board = minimax(board, depth, -1);
    for k=1:3
        for l=1:3
            if board(k,l)~=source.String
                continue;
            end
            uicontrols = source.Parent.Children;
            for m=1:numel(uicontrols)
                if isequal(round(1000*uicontrols(m).Position),round(1000*source.Position+[0,0.020,0,0]))
                    uicontrols(m).String = board(k,l);
                end
            end
        end
    end
    depth = depth + 1;
    % Check for winner or tie game
    winner = check_winner(board);
    if winner~=' '
        uialert(source.Parent,winner,'Game Over');
        return;
    elseif depth==9
        uialert(source.Parent,'Tie Game','Game Over');
        return;
    end
    % Switch player for next turn
    if player=='X'
        player = 'O';
    else
        player = 'X';
    end
end

% Function to check for winner
function winner = check_winner(board)
    winner = ' ';
    % Check rows
    for i=1:3
        if board(i,1)==board(i,2) && board(i,2)==board(i,3) && board(i,1)~=' '
            winner = board(i,1);
            return;
        end
    end
    % Check columns
    for j=1:3
        if board(1,j)==board(2,j) && board(2,j)==board(3,j) && board(1,j)~=' '
            winner = board(1,j);
            return;
        end
    end
    % Check diagonals
    if board(1,1)==board(2,2) && board(2,2)==board(3,3) && board(1,1)~=' '
        winner = board(1,1);
        return;
    elseif board(1,3)==board(2,2) && board(2,2)==board(3,1) && board(1,3)~=' '
        winner = board(1,3);
        return;
    end
end

% Function to calculate best move using Minimax algorithm
function best_move = minimax(board, depth, maximizer)
    scores = zeros(3,3);
    moves = [];
    % Calculate score for each possible move
    for i=1:3
        for j=1:3
            if board(i,j)==' '
                board(i,j) = maximizer == 1 ? 'O' : 'X';
                score = compute_score(board, depth);
                board(i,j) = ' ';
                scores(i,j) = score;
                moves = [moves; [i, j, score]];
            end
        end
    end
    % Find best move
    if maximizer == 1
        max_score = max(scores,[],'all');
        [row,col] = find(scores==max_score,1);
    else
        min_score = min(scores,[],'all');
        [row,col] = find(scores==min_score,1);
    end
    best_move = board;
    best_move(row,col) = maximizer == 1 ? 'O' : 'X';
end

% Function to compute score for Minimax algorithm
function score = compute_score(board, depth)
    winner = check_winner(board);
    if winner == 'O'
        score = 10 - depth;
    elseif winner == 'X'
        score = depth - 10;
    else
        score = 0;
    end
end
4105 chars
148 lines

The code creates a Tic Tac Toe game board in a GUI window, and allows the player to make moves by clicking on the buttons. The game is played between the player and AI, where the AI uses the Minimax algorithm to play optimally. The algorithm recursively computes the scores for all possible moves, and chooses the best move based on the maximizer or the minimizer. The game ends when one of the players wins or the game ends in a tie.

gistlibby LogSnag