How to make matrix dimensions agree
1 view (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
David Hill
on 19 Apr 2020
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
David Hill
on 19 Apr 2020
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
David Hill
on 19 Apr 2020
Edited: David Hill
on 19 Apr 2020
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.
More Answers (0)
See Also
Categories
Find more on Pulsed Waveforms in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!