I'm receiving this error code Line: 112 Column: 5 Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To co
    6 次查看(过去 30 天)
  
       显示 更早的评论
    
classdef NShirley_004_BlackJackApp < matlab.apps.AppBase
    % Properties that correspond to app components
    properties (Access = public)
        UIFigure          matlab.ui.Figure
        StandButton       matlab.ui.control.Button
        HitButton         matlab.ui.control.Button
        StartButton       matlab.ui.control.Button
        PlayerScoreLabel  matlab.ui.control.Label
        DealerScoreLabel  matlab.ui.control.Label
        PlayerPanel       matlab.ui.container.Panel
        PlayerCardsLabel  matlab.ui.control.Label
        DealerPanel       matlab.ui.container.Panel
        DealerCardsLabel  matlab.ui.control.Label
    end
    properties (Access = private)
        Property % Variables used for game logic
        deck      % a deck of cards
        dealer    % dealer's hand
        player    % player's hand
        playerScore = 0;
        dealerScore = 0;
        gameOver  = false;
    end
    % App initialization and setup
 methods (Access = private)
        % Create the deck of cards
        function createDeck(app)
            % A deck is created as an array of card structures
            suits = {'H', 'D', 'C', 'S'};
            values = {'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'};
            cardIdx = 1;
            for s = 1:length(suits)
                for v = 1:length(values)
                    app.deck(cardIdx).suit = suits{s};
                    app.deck(cardIdx).value = values{v};
                    cardIdx = cardIdx + 1;
                end
            end
            app.deck = app.deck(randperm(length(app.deck))); % shuffle the deck
        end
        % Start a new game
        function start(app)
            app.deck = [];
            app.dealer = [];
            app.player = [];
            app.playerScore = 0;
            app.dealerScore = 0;
            app.gameOver = false;
            app.createDeck();
            % Dealer draws two cards, one face up and one face down
            app.dealer(1) = app.drawCard();
            app.dealer(2) = app.drawCard();
            app.dealerScore = app.getHandValue(app.dealer);
            app.DealerCards.Text = [app.dealer(1).suit app.dealer(1).value '  ??'];
            app.DealerScore.Text = ['Dealer score: ' num2str(app.getHandValue(app.dealer))];
            % Player draws two cards
            app.player(1) = app.drawCard();
            app.player(2) = app.drawCard();
            app.playerScore = app.getHandValue(app.player);
            app.PlayerCards.Text = [app.player(1).suit app.player(1).value '  ' app.player(2).suit app.player(2).value];
            app.PlayerScore.Text = ['Player score: ' num2str(app.playerScore)];
        end
                % Draw a card from the deck
        function card = drawCard(app)
            if ~isempty(app.deck)
                card = app.deck(1);
                app.deck = app.deck(2:end);
            else
                card = [];
            end
        end
    % Get the value of a hand (accounting for aces)
function value = getHandValue(~, hand)
    value = 0;
    numAces = 0;
    for i = 1:length(hand)
        if ismember(hand(i).value, {'J', 'Q', 'K'})
            value = value + 10;
        elseif strcmp(hand(i).value, 'A')
            value = value + 1;
            numAces = numAces + 1;
        else
            value = value + str2double(hand(i).value);
        end
    end
    % If there are aces in the hand, and the hand value is less than or
    % equal to 11, treat one ace as a value of 11 instead of 1
    while numAces > 0 && value <= 11
        value = value + 10;
        numAces = numAces - 1;
    end
end
    end
end % end of classdef block
    % Callbacks that handle component events
    methods (Access = private)
        % Button pushed function: StartButton
        function StartButtonPushed(app, event)
    app.start();
    app.DealerCards.Text = [app.dealer(1).suit app.dealer(1).value '  ??'];
    app.DealerScore.Text = ['Dealer score: ' num2str(app.getHandValue(app.dealer))];
    app.PlayerCards.Text = [app.player(1).suit app.player(1).value '  ' app.player(2).suit app.player(2).value];
    app.PlayerScore.Text = ['Player score: ' num2str(app.playerScore)];
        end
        % Button pushed function: HitButton
        function HitButtonPushed(app, event)
         if ~app.gameOver
            app.player(end+1) = app.drawCard();
            app.playerScore = app.getHandValue(app.player);
            app.PlayerCards.Text = [app.PlayerCards.Text '  ' app.player(end).suit app.player(end).value];
            app.PlayerScore.Text = ['Player score: ' num2str(app.playerScore)];
            if app.playerScore > 21
                app.dealerScore = app.getHandValue(app.dealer);
                app.DealerCards.Text = [app.DealerCards.Text '  ' app.dealer(end).suit app.dealer(end).value];
                app.DealerScore.Text = ['Dealer score: ' num2str(app.dealerScore)];
                app.gameOver = true;
                app.result('You busted!');
            end
         end
        end
        % Button pushed function: StandButton
        function StandButtonPushed(app, event)
            if ~app.gameOver
            app.dealerScore = app.getHandValue(app.dealer);
            while app.dealerScore < 17
                app.dealer(end+1) = app.drawCard();
                app.dealerScore = app.getHandValue(app.dealer);
                app.DealerCards.Text = [app.DealerCards.Text '  ' app.dealer(end).suit app.dealer(end).value];
                app.DealerScore.Text = ['Dealer score: ' num2str(app.dealerScore)];
            end
            end
            if app.dealerScore > 21 || app.dealerScore < app.playerScore
                app.result('You win!');
            elseif app.dealerScore == app.playerScore
                app.result('Push!');
            else
                app.result('You lose!');
            end
            app.gameOver = true;
            end
        end
    end
    % Component initialization
    methods (Access = private)
        % Create UIFigure and components
        function createComponents(app)
            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 640 480];
            app.UIFigure.Name = 'MATLAB App';
            % Create DealerPanel
            app.DealerPanel = uipanel(app.UIFigure);
            app.DealerPanel.Title = 'Dealer';
            app.DealerPanel.Position = [1 87 260 221];
            % Create DealerCardsLabel
            app.DealerCardsLabel = uilabel(app.DealerPanel);
            app.DealerCardsLabel.HorizontalAlignment = 'center';
            app.DealerCardsLabel.FontSize = 25;
            app.DealerCardsLabel.Position = [47 75 153 68];
            app.DealerCardsLabel.Text = 'Dealer Cards';
            % Create PlayerPanel
            app.PlayerPanel = uipanel(app.UIFigure);
            app.PlayerPanel.Title = 'Player';
            app.PlayerPanel.Position = [381 87 260 221];
            % Create PlayerCardsLabel
            app.PlayerCardsLabel = uilabel(app.PlayerPanel);
            app.PlayerCardsLabel.HorizontalAlignment = 'center';
            app.PlayerCardsLabel.FontSize = 25;
            app.PlayerCardsLabel.Position = [53 75 153 68];
            app.PlayerCardsLabel.Text = 'Player Cards';
            % Create DealerScoreLabel
            app.DealerScoreLabel = uilabel(app.UIFigure);
            app.DealerScoreLabel.HorizontalAlignment = 'center';
            app.DealerScoreLabel.FontSize = 25;
            app.DealerScoreLabel.Position = [23 331 171 74];
            app.DealerScoreLabel.Text = 'Dealer Score';
            % Create PlayerScoreLabel
            app.PlayerScoreLabel = uilabel(app.UIFigure);
            app.PlayerScoreLabel.HorizontalAlignment = 'center';
            app.PlayerScoreLabel.FontSize = 25;
            app.PlayerScoreLabel.Position = [425 344 171 48];
            app.PlayerScoreLabel.Text = 'Player Score';
            % Create StartButton
            app.StartButton = uibutton(app.UIFigure, 'push');
            app.StartButton.ButtonPushedFcn = createCallbackFcn(app, @StartButtonPushed, true);
            app.StartButton.Position = [271 256 100 23];
            app.StartButton.Text = 'Start';
            % Create HitButton
            app.HitButton = uibutton(app.UIFigure, 'push');
            app.HitButton.ButtonPushedFcn = createCallbackFcn(app, @HitButtonPushed, true);
            app.HitButton.Position = [271 185 100 23];
            app.HitButton.Text = 'Hit';
            % Create StandButton
            app.StandButton = uibutton(app.UIFigure, 'push');
            app.StandButton.ButtonPushedFcn = createCallbackFcn(app, @StandButtonPushed, true);
            app.StandButton.Position = [271 102 100 23];
            app.StandButton.Text = 'Stand';
            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end
    % App creation and deletion
    methods (Access = public)
        % Construct app
        function app = NShirley_004_BlackJackAp1
            % Create UIFigure and components
            createComponents(app)
            % Register the app with App Designer
            registerApp(app, app.UIFigure)
            if nargout == 0
                clear app
            end
        end
        % Code that executes before app deletion
        function delete(app)
            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end
0 个评论
回答(1 个)
  Walter Roberson
      
      
 2023-4-29
        end % end of classdef block
Methods must be defined inside the classdef block.
You have several similar issues, of method blocks not being properly scoped. I suggest you take advantage of "Smart Indent" to see how MATLAB is interpreting the nesting.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

