(NOTE: I tried to use code formatting so people could copy and paste, but it got all messed up when I tried it. Sorry 'bout that!)
After finishing diner last night, I was overcome by a small case of intense boredom. In my mind I had two options. I could check out the board games that were three feet away from me, or I could burn the house down.
So I picked out an unopened bingo set and started playing bingo alone with 5 cards. "I'm a 100% sane person" I promised to myself. Card 2 won, and after that I thought "I could code this myself and let the computer play for me."
So I did just that.
The components of a game of bingo are (In no particular order):
Bingo balls are pretty simple. They're little plastic thingies with a number and a letter stamped on them. This simplicity translates to code pretty easily.
In code, they're just structs with a char variable and an int variable. It looks a little something like this:
A bingo cage holds all of the bingo balls and produces them one at a time.
In code, I translated this to a list of bingo balls. The list is created and populated with 75 balls in the constructor, all staying in line with how real life bingo balls work. That being:
The Draw() function picks a ball from the list at random, removes it from the list, and returns it. Here's how that looks:
The bingo cards consist of a multidimensional array of cells, a name for flavor text, and an event called OnCardCompleted.
The cells have four properties.
The cards need to do two things. Firstly, they need to cross reference the drawn bingo ball with the cells and mark the appropriate one. Secondly, they need to check their cells for five marks in a row and call a win function if that's the case.
The first is very straightforward. Loop through all of the cells with a reference to the drawn ball (structs are copied when they're passed to a function, but for this case I'm just going to say reference), if the characters and numbers match, mark the cell.
The second thing was not much harder, but far more tedious. To check for a bingo, we need start with a bingo bool set to true, then loop through the array of cells a few times. The first loops through each row. If any cell in the row is not marked, set the bingo bool to false. At the end of a row, if the bingo bool is still true then invoke the OnCardCompleted event. Otherwise, repeat the process for each row.
Then repeat that but with columns instead of rows.
Here's what a bingo check loop looks like:
Checking for a diagonal bingo is a lot simpler. It's basically the same, but with a single loop. Have two bools for left to right and right to left. Check from left to right by passing the loop index into both dimensions of the array. Right to left is checked by passing the array width minus the array index. It looks like this:
All that's left to do now is to throw in a random name picker, wrap it all up in a game class with a neat little bow, and simulate some bingo!
I hate my life...
Did you like this post? Tell us
Leave a comment
Log in with your itch.io account to leave a comment.