6 views (last 30 days)

Show older comments

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!

Constantino Carlos Reyes-Aldasoro
on 26 Oct 2020

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.

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

Start Hunting!