Coding Assignment 3
1. The format and content of the output is not a suggestion – it is the specification given to you to follow so please follow
it exactly. Points will be lost for not following the specification. Formatting the output to look exactly the same is part
of the exercises in this assignment.
2. Please note that part of the rubric is how you coded the program in addition to outputting valid responses. Be sure to
review the rubric before submitting your code.
3. Make sure your name and student id are in a comment in the first line of your program.
4. Name your program Code3_studentid.c and name the zip file Code3_studentid.zip for submission to
Canvas.
This assignment is simulating a bingo game. If you are unfamiliar with how bingo is played, please see this YouTube link
describing the game and how it is played. You will be expected to code your program’s behavior according to this version of
BINGO.
[Link]
This Wikipedia article describes the rules of the game and the layout of the cards. We will be using a 5x5 bingo card.
[Link]
Pseudocode
Create a 2D array that will be your bingo card. The bingo card will ONLY be 5x5.
Make sure your random numbers are truly random between program
runs.
Create a function to fill the bingo card with random numbers in the
proper ranges. Pass your 2D bingo array to this function to be
populated. Be sure to mark the free spot.
The ‘B’ column contains numbers between 1 and 15
The ‘I’ column contains numbers between 16 and 30 These are EXAMPLE BINGO cards -do
not hardcode any of the numbers in
The ‘N’ column contains numbers between 31 and 45 your BINGO card – every number
should random and in the correct
The ‘G’ column contains numbers between 46 and 60
range.
The ‘O’ column contains numbers between 61 and 75
Just a reminder - BINGO numbers MUST be unique. You cannot have the same number in multiple cells of your
BINGO card. The rules of BINGO state that every number is unique. Obtaining random numbers using rand()
does NOT guarantee that each number is unique – YOU must check that each number is unique. If a number has
already been used, then get another number.
Create a function to print the bingo card to the screen. You must match the formatting shown in the sample output.
The bingo array must be passed to this function.
Create a function to pick a number that has not already been chosen. You will need to use a 1D array to keep track
of which numbers between 1 and 75 have been used so that you don’t pick numbers that have already been called.
If you randomly pick a number that was already called, then pick another. Continue this process until you find a
number that has not been previously called. Print the number to the screen along with its corresponding letter.
This function should return that value.
Create a function to determine if a called number exists in the player’s bingo card. Pass the bingo array and the
number to the function. Loop over the array (nested for loops) and, if the number is found in the bingo array, then
change the value to 0 to “mark” it. If the number is found, then this function should return true; otherwise, return
false.
Create a function to check for a completed row. A completed row is a row in the bingo array that has 0 for every
value (we “marked” our bingo numbers by changing the existing value to zero to indicate that the called number
matches one in our bingo card). This function should check every row in the bingo array and return whether or not
it found a completed row.
Create a function to check for a completed column. A completed column is a column in the bingo array that has 0
for every value (we “marked” our bingo numbers by changing the existing value to zero to indicate that the called
number matches one in our bingo card). This function should check every column in the bingo array and return
whether or not it found a completed column.
Create a function to check for a completed diagonal. A completed diagonal is a diagonal in the bingo array that has
0 for every value (we “marked” our bingo numbers by changing the existing value to zero to indicate that the called
number matches one in our bingo card). This function should check both diagonals in the bingo array and return
whether or not it found a completed diagonal.
Add prototypes for ALL functions at the top of the program.
Yellow highlighting has been added to make the combinations more clear – your code will NOT produce this highlighting.
Pseudocode for main()
main()
Call function to fill bingo card
Call function to print bingo card to screen.
Open a file named “[Link]” with w+ mode
If the file does not open, then use exit() to stop the program.
Call function to print bingo card to a file named “[Link]”.
While the player has not gotten a row or column or diagonal and
while there are still numbers to choose from (there are 75 total)
Call a function to pick a number that has not been chosen already – this function returns the called
number.
Show the player the number (including the B, I, N, G or O) and ask if the player has that number on
their bingo card.
If the player answers anything other than something that begins with ‘Y’, then reprint the bingo card
and increment the count of numbers drawn so far.
If the player answers something that begins with ‘Y’, then
Call a function to determine if the number drawn IS a number from the bingo card.
If the function returns false, then print the message about cheating.
If the function returns true, then call a function to check for a completed row and a function
to check for a completed column and a function to check for a completed diagonal. If a
completed row and/or column and/or diagonal is found, then the player has won and a
message should print and the game should end.
Write the final BINGO card to the file [Link].
Write the called numbers to the file [Link] in rows of 10 numbers each.
HINTS
Please review the rubric about using a return inside a loop to break out of the loop and about using an arbitrary
value/sentinel value to control a loop. We went over both of these in class. Neither are allowed and if you use either, you
will receive a 0 on the assignment.
We discussed in class how to get numbers with a range - something you need to do to fill in your BINGO card.
rand() % (end-start+1) + start
Remember that you need to get unique numbers within each range. That formula could give you, for example, a value of 8
twice. To prevent using duplicate values in a single column, you need to keep a 1D array to hold the values you have used - I'll
call it the DUP array.
For example, if your random number is 12, then loop through the DUP array and see if 12 has already been used. If it has not,
then use it and add it to the DUP array. If it has been used (you find it in DUP), then get a new random number and check if it
is a duplicate (need a loop here...).
Make sure you include time.h to use srand(time(0))
Question : How do we get an X in the free space when the BINGO 2D array is int?
Answer : In the video, notice that every time a number is called, and it exists in the BINGO card, the number is replaced with an
X. Since the 2D array is int, 0 is used to indicate free space OR a called number; therefore, when printing the BINGO card, if an
array element is 0, then print 'X'. When filling the BINGO 2d array, you can fill it - every space - using your code and then, at
the end of the FillBingoCard function, set element [2][2] to 0.
Question : How do we keep track of what numbers have already been called during the game play if we pick new numbers in
function PickNumber()? That function prints the called number to the screen and returns that number.
Answer : Since you need to keep an array of called/picked numbers to keep track of what has been called so you don't call the
same number twice, that function cannot be created/live in function PickNumber() since it would go away as soon as the
function returns. So that array must be created in main. If it is created in main(), then it must be passed to function
PickNumber() to be used. You will need to keep track of how many numbers have been added to that array - declare a
counter in main, pass it to PickNumber() to be used, and then increment it in main after returning from PickNumber().
Question : what happens if the player says a number is NOT in their card (and it really is)?
Answer : Just like in real BINGO - if you snooze, you lose! If the player says a number is NOT in their card (and it really is), then
you do NOT mark it for them. They miss out and that number will not be called again.
You only mark a value in the card if the player says yes AND it exists in the card.
Coding Suggestions to get you started
Some thoughts to consider
Always check the rubric for what coding elements are required by the assignment.
For filling the card with unique numbers, you have to go through the BINGO card column by column (rather than row by row
which is what we normally do for 2D arrays). Since the BINGO card has 5 columns, this would be a good place to use a for loop.
Now, for each column (inside the for loop), you need to find 5 unique values that fit within the allowed range for that column.
So, WHILE you haven't found all 5, get a random number in range, check if you have already gotten it in that column - if you
have, throw it out and get another - if you haven't, put it in your BINGO card.
So for the
0th column, your number needs to be 1-15
1st column, your number needs to be 16-30
2nd column, your number needs to be 31-45
3rd column, your number needs to be 46-60
4th column, your number needs to be 61-75
We talked about generating a random number in a range by shifting the result from rand()
To get a value from 1-15 rand() % 15 + 1
To get a value from 16-30 rand() % 15 + 16
To get a value from 31-45 rand() % 15 + 31
To get a value from 46-60 rand() % 15 + 46
To get a value from 61-75 rand() % 15 + 61
We can rewrite this as
To get a value from 1-15 rand() % 15 + 1
To get a value from 16-30 rand() % 15 + 15+1
To get a value from 31-45 rand() % 15 + 30+1
To get a value from 46-60 rand() % 15 + 45+1
To get a value from 61-75 rand() % 15 + 60+1
This can be rewritten as
To get a value from 1-15 rand() % 15 + (15*0)+1
To get a value from 16-30 rand() % 15 + (15*1)+1
To get a value from 31-45 rand() % 15 + (15*2)+1
To get a value from 46-60 rand() % 15 + (15*3)+1
To get a value from 61-75 rand() % 15 + (15*4)+1
So you could use one statement
rand()%15 + (15*x)+1
to get random numbers in range for each column (the x in the formula is the column number/index).
Screenshot when player answers Y to a number that is not on the bingo card.
B I N G O
-----------------------------------------
| 12 | 21 | 43 | 54 | 73 |
-----------------------------------------
| 10 | 17 | 33 | 53 | 69 |
-----------------------------------------
| 7 | 24 | X | 59 | 61 |
-----------------------------------------
| 5 | 26 | 38 | 55 | 67 |
-----------------------------------------
| 6 | 23 | 42 | 51 | 62 |
-----------------------------------------
The next number is O65
Do you have it? (Y/N)Y
That value is not on your BINGO card - are you trying to cheat??
Screenshot when player answers n to a number that is not on the bingo card.
B I N G O
-----------------------------------------
| 12 | 21 | 43 | 54 | 73 |
-----------------------------------------
| 10 | 17 | 33 | 53 | 69 |
-----------------------------------------
| 7 | 24 | X | 59 | 61 |
-----------------------------------------
| 5 | 26 | 38 | 55 | 67 |
-----------------------------------------
| 6 | 23 | 42 | 51 | 62 |
-----------------------------------------
The next number is O72
Do you have it? (Y/N)N
B I N G O
-----------------------------------------
| 12 | 21 | 43 | 54 | 73 |
-----------------------------------------
| 10 | 17 | 33 | 53 | 69 |
-----------------------------------------
| 7 | 24 | X | 59 | 61 |
-----------------------------------------
| 5 | 26 | 38 | 55 | 67 |
-----------------------------------------
| 6 | 23 | 42 | 51 | 62 |
-----------------------------------------
Screenshot when player answers y to a number that is on the bingo card. Player’s answer must start with capital Y
to be considered positive.
B I N G O
-----------------------------------------
| 12 | 21 | 43 | 54 | 73 |
-----------------------------------------
| 10 | 17 | 33 | 53 | 69 |
-----------------------------------------
| 7 | 24 | X | 59 | 61 |
-----------------------------------------
| 5 | 26 | 38 | 55 | 67 |
-----------------------------------------
| 6 | 23 | 42 | 51 | 62 |
-----------------------------------------
The next number is B6
Do you have it? (Y/N)y
B I N G O
-----------------------------------------
| 12 | 21 | 43 | 54 | 73 |
-----------------------------------------
| 10 | 17 | 33 | 53 | 69 |
-----------------------------------------
| 7 | 24 | X | 59 | 61 |
-----------------------------------------
| 5 | 26 | 38 | 55 | 67 |
-----------------------------------------
| 6 | 23 | 42 | 51 | 62 |
-----------------------------------------
Screenshot when player wins by filling out both a column and row.
B I N G O
-----------------------------------------
| X | X | X | 60 | X |
-----------------------------------------
| X | X | 33 | X | X |
-----------------------------------------
| X | 19 | X | X | X |
-----------------------------------------
| X | X | 35 | X | X |
-----------------------------------------
| 2 | X | X | X | 72 |
-----------------------------------------
The next number is I19
Do you have it? (Y/N)Y
B I N G O
-----------------------------------------
| X | X | X | 60 | X |
-----------------------------------------
| X | X | 33 | X | X |
-----------------------------------------
| X | X | X | X | X |
-----------------------------------------
| X | X | 35 | X | X |
-----------------------------------------
| 2 | X | X | X | 72 |
-----------------------------------------
You filled out a row and a column - BINGO!!!
Screenshot when player wins by filling out a row.
B I N G O
-----------------------------------------
| 9 | 17 | 32 | X | 68 |
-----------------------------------------
| 12 | 23 | X | X | X |
-----------------------------------------
| X | 26 | X | X | X |
-----------------------------------------
| X | X | 37 | 46 | 75 |
-----------------------------------------
| X | 24 | X | X | X |
-----------------------------------------
The next number is I24
Do you have it? (Y/N)
B I N G O
-----------------------------------------
| 9 | 17 | 32 | X | 68 |
-----------------------------------------
| 12 | 23 | X | X | X |
-----------------------------------------
| X | 26 | X | X | X |
-----------------------------------------
| X | X | 37 | 46 | 75 |
-----------------------------------------
| X | X | X | X | X |
-----------------------------------------
You filled out a row - BINGO!!!
Screenshot when player wins by filling out a column.
B I N G O
-----------------------------------------
| X | X | 35 | 58 | X |
-----------------------------------------
| X | 30 | 31 | X | 67 |
-----------------------------------------
| 10 | 18 | X | 54 | X |
-----------------------------------------
| X | 29 | 37 | X | X |
-----------------------------------------
| 8 | X | 44 | 60 | X |
-----------------------------------------
The next number is O67
Do you have it? (Y/N)
B I N G O
-----------------------------------------
| X | X | 35 | 58 | X |
-----------------------------------------
| X | 30 | 31 | X | X |
-----------------------------------------
| 10 | 18 | X | 54 | X |
-----------------------------------------
| X | 29 | 37 | X | X |
-----------------------------------------
| 8 | X | 44 | 60 | X |
-----------------------------------------
You filled out a column - BINGO!!!
Example of [Link]
B I N G O
-----------------------------------------
| 5 | 22 | 33 | 60 | 67 |
-----------------------------------------
| 8 | 16 | 32 | 57 | 72 | BINGO card before game
----------------------------------------- starts
| 7 | 23 | X | 59 | 71 |
-----------------------------------------
| 12 | 21 | 44 | 50 | 68 |
-----------------------------------------
| 6 | 17 | 35 | 52 | 69 |
-----------------------------------------
B I N G O
-----------------------------------------
| X | 22 | X | X | 67 |
-----------------------------------------
| X | X | 32 | X | 72 |
----------------------------------------- BINGO card after game ends
| X | 23 | X | 59 | X |
-----------------------------------------
| 12 | X | X | 50 | X |
-----------------------------------------
| X | X | X | X | X |
-----------------------------------------
52 42 8 5 45 68 57 10 15 14
21 25 55 19 71 24 46 69 13 44
54 47 75 11 39 4 33 6 3 18
7 16 38 29 17 48 70 37 61 60
63 36 35