Assigning an iterative variable a script
显示 更早的评论
Does anyone know how I can assign an iterative variable in a for loop a script value. I am trying to make a poker game in MatLab and when I try to assign suits in a for loop it doesn't work and it instead assigns them NAN. I know I could just make my suit vector a collection of 1,2,3,and 4 and then later assign those values to the suit name, but is there any way i could do it directly in the for loop? The following code is where the issue is (in the for loop, but I included the other code, so it would be simpler to understand). (I know spaids is spelled spades I need to go through and fix it)
deck=[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 11 11 11 11 12 12 12 12 13 13 13 13 14 14 14 14];
players=input('How many players would you like to play with (max number of players is 5) ');
while players>5 | players<1 | round(players)~=players
players=input('ERROR INPUT AN INTEGER BETWEEN 1 and 5. How many players would you like to play with (max number of players is 5) ');
end
% initializing variables that will be used later
n=0;
folded=zeros(length(players));
order=randperm(52);
suit1=zeros(length(players));
suit2=zeros(length(players));
card1=zeros(length(players));
card2=zeros(length(players));
% creating the vector for the suits of the cards
suits=["spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds","spaids","hearts","clubs","diamonds"];
% assigning individual players cards
for i=1:players
n=n+1;
card1(i)=deck(order(i));
card2(i)=deck(order(i+players));
suit1(i)=suits(order(n));
suit2(i)=suits(order(n+players));
fprintf('Player %1.0f your cards are %1.0f %s and %1.0f %s\n',i,card1(i),suit1(i),card2(i),suit2(i))
end
4 个评论
Voss
2022-5-5
Note that if players is a scalar (which I think it's intended to be), then length(players) is 1, so instead of pre-allocating like this:
card1=zeros(length(players)); % makes card1 a scalar
pre-allocate like this:
card1=zeros(1,players); % makes card1 a vector of size 1-by-players
Also, n is always the same as i, so just use i.
Also, i and the elements of card1 and card2 are all integers, so you can use %d format instead of %1.0f, but it would be interesting to figure out how to display the names of face cards, i.e., Jack, Queen, King, Ace instead of 11, 12, 13, 14 (in which case the cards printed there are no longer necessarily integers or even numeric).
Tim Keegan
2022-5-5
"for some reason it wasn't messing up"
That's because, if you assign to an element of an array that's off the end of the array, MATLAB will extend the array automatically (or if the array doesn't exist yet, MATLAB will allocate it to the size required):
% card1 doesn't exist yet, so MATLAB creates it and
% gives it 5 elements, with the first 4 being 0
card1(5) = 7
or similarly for a string array:
suit1(3) = "spade"
so, pre-allocation is not strictly necessary, but it's a good idea. When you were allocating your variables to be of size 1 and then assigning elements 2, 3, etc., it's no problem, just not optimal and not what was apparently intended.
Similarly, the other things I pointed out are not problems per se, just pointers to simplify or clarify the code.
deck = repelem(2:14,1,4)
suits = repmat(["spaids","hearts","clubs","diamonds"],1,13)
Tim Keegan
2022-5-6
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!