function to call the gui for a tic-tac-toe game in matlab

Here's an example function code in MATLAB to call the GUI of a tic-tac-toe game:

main.m
function playTicTacToe()
    % Create figure and GUI components
    fig = figure('Name', 'TIC TAC TOE', 'NumberTitle', 'off', ...
                 'Position', [400, 400, 300, 300], 'MenuBar', 'none');
    board = uicontrol(fig, 'Style', 'grid', 'Position', [50, 50, 200, 200], ...
                      'FontWeight', 'bold', 'FontSize', 30);
    resetButton = uicontrol(fig, 'Style', 'pushbutton', 'String', 'Reset', ...
                            'Position', [100, 10, 100, 30], 'Callback', @resetBoard);
    
    % Define game logic
    player = 1; % Player 1 starts the game
    gameBoard = zeros(3); % Initialize game board to all zeros
    
    % Helper functions
    function buttonCallback(source, event)
        % Get button position on the game board
        position = source.Position;
        row = ceil((200 - position(2))/67);
        col = ceil((position(1) - 50)/67);
        
        % If the button has not been clicked before, mark it with player's sign
        if gameBoard(row, col) == 0
            if player == 1
                source.String = 'X';
                gameBoard(row, col) = 1;
                player = 2;
            else
                source.String = 'O';
                gameBoard(row, col) = 2;
                player = 1;
            end
            
            % Check if there is a winner or a tie
            winner = checkWinner(gameBoard);
            if winner > 0
                msgbox(sprintf('Player %d is the winner!', winner), 'Game result');
            elseif all(gameBoard(:) ~= 0)
                msgbox('It''s a tie!', 'Game result');
            end
        end
    end

    function resetBoard(source, event)
        % Reset the game board and button labels
        gameBoard = zeros(3);
        for row = 1:3
            for col = 1:3
                button = findobj(board, 'Position', [50+(col-1)*67, 233-(row-1)*67, 67, 67]);
                button.String = '';
            end
        end
    end

    function winner = checkWinner(board)
        % Check rows
        for row = 1:3
            if all(board(row,:) == 1)
                winner = 1;
                return;
            elseif all(board(row,:) == 2)
                winner = 2;
                return;
            end
        end

        % Check columns
        for col = 1:3
            if all(board(:,col) == 1)
                winner = 1;
                return;
            elseif all(board(:,col) == 2)
                winner = 2;
                return;
            end
        end

        % Check diagonals
        if all(diag(board) == 1) || all(diag(flipud(board)) == 1)
            winner = 1;
            return;
        elseif all(diag(board) == 2) || all(diag(flipud(board)) == 2)
            winner = 2;
            return;
        end
        
        % No winner found
        winner = 0;
    end
end
2851 chars
90 lines

To call this function, simply type playTicTacToe() in the MATLAB Command Window.

gistlibby LogSnag