make a game of blackjack in matlab

Here's a basic implementation of a game of Blackjack in MATLAB using object-oriented programming:

main.m
classdef Blackjack
    % Class for playing a game of Blackjack
    
    properties
        deck % deck of cards
        dealer % dealer's hand
        player % player's hand
        deckSize % current deck size
        bet % current bet
        wallet % current wallet amount
        gameOver = false % indicates if game is over
    end
    
    methods
        function obj = Blackjack(startingWallet)
            % Constructor function to initialize a new game
            obj.wallet = startingWallet;
            obj.dealer = Hand();
            obj.player = Hand();
            obj.deck = Deck();
            obj.deck.shuffle();
            obj.deckSize = 52;
            obj.bet = 0;
        end
        
        function play(obj)
            % Main game loop
            while ~obj.gameOver
                obj.takeBet();
                obj.dealCards();
                obj.playerTurn();
                obj.dealerTurn();
                obj.endGame();
            end
        end
        
        function takeBet(obj)
            % Prompt player for bet amount
            bet = input('Enter bet amount:');
            while bet > obj.wallet
                bet = input('Insufficient funds. Enter valid bet amount:');
            end
            obj.bet = bet;
            obj.wallet = obj.wallet - bet;
        end
        
        function dealCards(obj)
            % Deal initial cards to player and dealer
            obj.player.addCard(obj.deck.drawCard());
            obj.player.addCard(obj.deck.drawCard());
            obj.dealer.addCard(obj.deck.drawCard());
            obj.dealer.addCard(obj.deck.drawCard());
            obj.deckSize = obj.deckSize - 4;
            obj.player.displayHand();
            obj.dealer.displayDealerHand();
        end
        
        function playerTurn(obj)
            % Play out player's turn
            while obj.player.score < 21
                hit = input('Hit or stay? (h/s)','s');
                if hit == 'h'
                    obj.player.addCard(obj.deck.drawCard());
                    obj.deckSize = obj.deckSize - 1;
                    obj.player.displayHand();
                else
                    break
                end
            end
        end
        
        function dealerTurn(obj)
            % Play out dealer's turn
            obj.dealer.revealHand();
            while obj.dealer.score < 17
                obj.dealer.addCard(obj.deck.drawCard());
                obj.deckSize = obj.deckSize - 1;
                obj.dealer.displayHand();
            end
        end
        
        function endGame(obj)
            % Determine winner and update wallet
            obj.dealer.revealHand();
            obj.player.displayHand();
            dealerScore = obj.dealer.score;
            playerScore = obj.player.score;
            if playerScore > 21
                fprintf('Player busts! Dealer wins.\n');
            elseif dealerScore > 21
                fprintf('Dealer busts! Player wins.\n');
                obj.wallet = obj.wallet + obj.bet * 2;
            elseif dealerScore == playerScore
                fprintf('Push! Bet is returned.\n');
                obj.wallet = obj.wallet + obj.bet;
            elseif dealerScore > playerScore
                fprintf('Dealer wins.\n');
            else
                fprintf('Player wins.\n');
                obj.wallet = obj.wallet + obj.bet * 2;
            end
            
            fprintf('Current wallet: $%d\n', obj.wallet);

            % Check if player can play another hand
            if obj.wallet == 0
                fprintf('Game over. Wallet is empty.\n');
                obj.gameOver = true;
            else
                playAgain = input('Play another hand? (y/n)','s');
                if playAgain == 'n'
                    obj.gameOver = true;
                else
                    obj.deck = Deck();
                    obj.deck.shuffle();
                    obj.deckSize = 52;
                    obj.player.clearHand();
                    obj.dealer.clearHand();
                end
            end
        end
    end
end

classdef Deck
    % Class for deck of cards
    
    properties
        deck
        deckSize
    end
    
    methods
        function obj = Deck()
            % Constructor function to initialize a new deck
            suits = ["hearts","diamonds","spades","clubs"];
            values = ["ace","2","3","4","5","6","7","8","9","10","jack","queen","king"];
            deck = [];
            for i = 1:length(suits)
                for j = 1:length(values)
                    card = struct('value',values{j},'suit',suits{i});
                    deck = [deck, card];
                end
            end
            obj.deck = deck;
            obj.deckSize = 52;
        end
        
        function shuffle(obj)
            % Shuffle the deck
            shuffledDeck = obj.deck(randperm(length(obj.deck)));
            obj.deck = shuffledDeck;
        end
        
        function card = drawCard(obj)
            % Draw a card from the deck
            if obj.deckSize == 0
                obj.deck = Deck();
                obj.shuffle();
                obj.deckSize = 52;
            end
            card = obj.deck(1);
            obj.deck = obj.deck(2:end);
            obj.deckSize = obj.deckSize - 1;
        end
    end
end

classdef Hand
    % Class for a hand of cards
    
    properties
        hand
        numAces
        score
        numCards
    end
    
    methods
        function obj = Hand()
            % Constructor function to initialize a new hand
            obj.hand = [];
            obj.numAces = 0;
            obj.score = 0;
            obj.numCards = 0;
        end
        
        function addCard(obj,card)
            % Add a card to the hand
            obj.hand = [obj.hand, card];
            obj.numCards = obj.numCards + 1;
            if strcmp(card.value,'ace')
                obj.numAces = obj.numAces + 1;
                obj.score = obj.score + 11;
            elseif strcmp(card.value,'king') || strcmp(card.value,'queen') || strcmp(card.value,'jack') || strcmp(card.value,'10')
                obj.score = obj.score + 10;
            else
                obj.score = obj.score + str2double(card.value);
            end
            while obj.score > 21 && obj.numAces > 0
                obj.score = obj.score - 10;
                obj.numAces = obj.numAces - 1;
            end
        end
        
        function displayHand(obj)
            % Display the hand
            fprintf('Current hand:\n');
            for i = 1:obj.numCards
                fprintf('%s of %s\n', obj.hand(i).value, obj.hand(i).suit);
            end
            fprintf('Hand score: %d\n\n', obj.score);
        end
        
        function displayDealerHand(obj)
            % Display the dealer's hand (first card is hidden)
            fprintf('Dealer\'s hand:\n');
            fprintf('Hidden card\n');
            for i = 2:obj.numCards
                fprintf('%s of %s\n', obj.hand(i).value, obj.hand(i).suit);
            end
            fprintf('\n');
        end
        
        function revealHand(obj)
            % Reveal the entire hand
            fprintf('Hand:\n');
            for i = 1:obj.numCards
                fprintf('%s of %s\n', obj.hand(i).value, obj.hand(i).suit);
            end
            fprintf('Hand score: %d\n\n', obj.score);
        end
        
        function clearHand(obj)
            % Clear the hand
            obj.hand = [];
            obj.numAces = 0;
            obj.score = 0;
            obj.numCards = 0;
        end
    end
end
7642 chars
243 lines

To play the game, simply create a Blackjack object with a starting wallet amount and call its play() method:

main.m
game = Blackjack(1000);
game.play();
37 chars
3 lines

You can customize the starting wallet amount and any other aspects of the game by modifying the code as desired. Enjoy!

gistlibby LogSnag