Assigning an iterative variable a script
18 次查看(过去 30 天)
显示 更早的评论
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
编辑:Voss
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)
采纳的回答
Jon
2022-5-5
编辑:Jon
2022-5-5
The problem is that you preallocated suit1, and suit2 as a vector of doubles (numeric values), but then in the loop you assign strings to the individual elements
2 个评论
Jon
2022-5-5
编辑:Jon
2022-5-5
You should instead preallocate using strings, as for example
suit1=strings(length(players),1);
Note, I think your original code also has errors preallocating suit1, suit2,card1 and card2 where you set them equal to zeros(length(players)). This will create length(players) by length(players) matrix of zeros. I think you just wanted a vector. So for a column vector preallocate card1 and card 2 using zeros(length(players),1), and for the suits use strings(length(players),1) as shown above
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!