Filling a Vector with Find Function

6 次查看(过去 30 天)
How can I fill in my x vector with the find function?
I have a code that is used for a Bingo game. I have created a vector of 25 random numbers wth no duplicates, then used that vector to create a bingo card (in the form of a 5x5 matrix) and have set the center to 0, so that the player has a freebe. I have created 12 different vectors that will give you all possible ways to win a bingo (vertical, horizontal, and diagonal win). That is a matrix that is 12x5 and I have labeled Win. The Calll vector is a vector of 50 random integers (no duplicates) that is calling the winning numbers. Therefore, I created the x vector, in the nested for loop so that simulataneously, it will find() where numbers in Call equal to an entire row in the Win matrix, because this is how we know there is a winner. The if statement is used to know whether there is a winner or not, because if there are 5 matches in a row, then there are 5 numbers in the Call, that give the Card a Bingo. It will also show the user their bingo card and that they won. My trouble is debugging the
x = [x, find(Call == Win(i,j))];
line where the x vector is supposed to be filled with the row of Win that matches five numbers in the Call vector to cause a bingo. I will include the part of my code that does all I described (and intended to accomplish) above:
WinStatus = false;
%Using my Call to check my Winning Card
for i = 1:length(Win)
x = [];
for j = 1:length(Win(i,:))
x = [x, find(Call == Win(i,j))];
end
%If the length of x is equal to 5 then there is a winner
if(length(x) == 5)
disp('Winner! :D');
disp('This is your winning card (The zero is your free be): ');
disp(Card);
WinStatus = true;
break
end
end
I have another part of my code that deals when there is a loss. It's just that one line that is causing me trouble. Thank you for your time!
  2 个评论
Stephen23
Stephen23 2020-10-26
Most likely the MATLAB approach would be to use ismember or something similar, rathter than loops and find.
Please show (or upload by clicking the paperclip button) some test data and also the expected output for that data.
Sam Zavala
Sam Zavala 2020-10-28
Some Test Data:
Winner! :D
This is your winning card (The zero is your free be):
24 18 69 91 94
36 92 44 90 57
67 17 0 2 38
54 74 41 12 68
78 42 15 89 71
(The x vector that was displayed in the Workspace area)
[31,7,41,22,40]
The expected output would be that x be filled in with one of the 12 combinations needed to make a bingo. Such as
[24,92,0,12,71] or [78,42,15,89,71]

请先登录,再进行评论。

采纳的回答

Constantino Carlos Reyes-Aldasoro
This line does not look like Matlab syntax, it looks more like a python loop
x [x, find(Call == Win(i,j))];
I would suggest that you start with x with the dimensions you want, either
x=zeros(5,5);
or
x = rand(5,5);
and then scale and round appropriately to have the values of your bingo. Then iterate as you go through the game and change the values of x, or better, have another variable in which you record the hits (equivalent of placing an object on the number), say something like this
x= round(25*rand(5,5));
y=zeros(5,5);
Then the iteration when a number has been called, say Z would be
if any(x==Z)
y(x==z) =1;
end
Then you have the hits recorded in y and finally just check the number of hits per column, row or diagonal.
Hope this helps.
  1 个评论
Sam Zavala
Sam Zavala 2020-10-28
I was wondering if there was something missing because it felt like there was too much a gap. I appreciate this a ton, thank you!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Card games 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by