How to make matrix dimensions agree

I am trying to make a blackjack game and have gotten to where it is almost user-friendly.
%BlackJack single player game
clear; clc;
deck = [11 11 11 11 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10 10]; %assume all aces are worth 11
dealercount = 0;
playercount = 0;
playercard1 = deck(randperm(length(deck),1));
playercard2 = deck(randperm(length(deck),1));
playercard3 = deck(randperm(length(deck),1));
playercard4 = deck(randperm(length(deck),1));
dealercard1 = deck(randperm(length(deck),1));
dealercard2 = deck(randperm(length(deck),1));
dealercard3 = deck(randperm(length(deck),1));
dealercard4 = deck(randperm(length(deck),1));
if dealercount < 21
dealercount = dealercard1;
end
fprintf('What would you like to do? Hit or Stand?')
playerchoice = input('','s')
if playerchoice == 'Hit'
playercount = playercard1
else playerchoice == 'Stand'
playercount = playercount
end
When the player inputs Hit it works, but when they input Stand it says:
Matrix dimensions must agree.
Error in BlackJack (line 22)
if playerchoice == 'Hit'
How do I fix this?

 Accepted Answer

fprintf('What would you like to do? Hit or Stand?')
playerchoice = input('','s')
if playerchoice == 'Hit'
playercount = playercard1
elseif playerchoice == 'Stand'%you need elseif, otherwise you are executing the logical statement
playercount = playercount
end

3 Comments

Even with elseif if comes up with the same error. I'm not sure why
You need to use isequal
fprintf('What would you like to do? Hit or Stand?')
playerchoice = input('','s')
if isequal(playerchoice,'Hit')
playercount = playercard1
elseif isequal(playerchoice,'Stand')
playercount = playercount
end
Look at what is produced when you code:
'Pass'=='Stop'
It is a logical operation looking at each element of the character array.The character arrays have to be the same length or else you will get that error.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!