In C language Only Please! Thanks a lot! Continuing with Bingo Now that we have
ID: 3604053 • Letter: I
Question
In C language Only Please! Thanks a lot!
Continuing with Bingo
Now that we have our Bingo Cards we should play Bingo!!!
Create a program that will:
do everything from program 2 (create bingo cards), if you didn't get this working just right go see TAs/professor As Soon As Possible
Add an option 4 to your menu for Play Bingo
read in a bingo call (e,g, B6, I17, G57, G65)
checks to see if the bingo call read in is valid (i.e., G65 is not valid)
marks all the boards that have the bingo call
checks to see if there is a winner, for our purposes winning means
5 tokens in a row or column
5 tokens in a diagonal
1 token in each of the 4 corners of the game board
Loop accepting bingo calls until there is a winner, and display an appropriate message specifying which user won, print their bingo card, and how they won (5 in a row with calls B6, I20, ...; 5 in a column with calls ...; 5 on a diagonal with calls ... ; 4 corners with calls ...)
Grading:
35 points style (We will be looking for functions now. So having a function to check for a winner, or initialize the board or, ....)
20 points checking for winner code, we expect to see multiple functions here, 1 function that checks for each way of winning (row, col, diag, 4 corners)
10 points checks for valid bingo calls
10 points appropriate message for winner displayed
10 points game play works until winner is found
5 points recording which calls were received,
10 points marking the boards with the calls that have been received
Program 2:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int get_random_number(int min, int max)
{
return min + (rand() % (max - min + 1));
}
int min_for_col(int column_index)
{
return (column_index * 15) + 1;
}
int max_for_col(int column_index)
{
return (column_index + 1) * 15;
}
void print_bingo_card(int cards[5][10][5][5], int player, int card)
{
int i, j;
printf(" B I N G O ");
for(i = 0; i < 5; ++i)
{
for(j = 0; j < 5; ++j)
{
if(i == 2 && j == 2)
{
printf(" ");
}
else
{
printf("%2d ", cards[player][card][i][j]);
}
}
printf(" ");
}
printf(" ");
}
int main()
{
srand(time(NULL));
int num_players, num_cards, i, j, k, l, m, num;
int bingo_cards[5][10][5][5];
int count[75];
int num_rows = 5, num_cols = 5;
int already_exists = 0;
printf("Please enter number of players: ");
scanf("%d", &num_players);
printf("Please enter number of cards per player: ");
scanf("%d", &num_cards);
for(i = 0; i < num_players; ++i)
{
for(j = 0; j < num_cards; ++j)
{
for(k = 0; k < num_cols; ++k)
{
for(l = 0; l < num_rows; ++l)
{
while(1)
{
num = get_random_number(min_for_col(k), max_for_col(k));
already_exists = 0;
for(m = 0; m < l; ++m)
{
if(bingo_cards[i][j][m][k] == num)
{
already_exists = 1;
}
}
if(already_exists == 0)
{
break;
}
}
bingo_cards[i][j][l][k] = num;
}
}
}
}
for(i = 0; i < 75; ++i)
{
count[i] = 0;
}
for(i = 0; i < num_players; ++i)
{
for(j = 0; j < num_cards; ++j)
{
for(k = 0; k < num_rows; ++k)
{
for(l = 0; l < num_cols; ++l)
{
if(!(k == 2 && l == 2))
{
count[bingo_cards[i][j][k][l] - 1]++;
}
}
}
}
}
while(1)
{
printf("1. To select user/bingo card to display ");
printf("2. To show histogram ");
printf("3. Exit ");
printf("Please enter your choice: ");
scanf("%d", &i);
switch(i)
{
case 1:
printf("Please enter player number(%d - %d): ", 1, num_players);
scanf("%d", &j);
printf("Please enter card number(%d - %d): ", 1, num_cards);
scanf("%d", &k);
print_bingo_card(bingo_cards, j - 1, k - 1);
break;
case 2:
for(j = 0; j < 75; ++j)
{
printf("%2d: ", j + 1);
for(k = 0; k < count[j]; ++k)
{
printf("*");
}
printf(" ");
}
break;
case 3: // program exits
printf("Invalid choice. Please try again!! ");
break;
}
}
return 0;
}
Explanation / Answer
#include<stdio.h>
#include <stdlib.h>
#include<time.h>
int get_random_number(int min, int max) {
return min + (rand() % (max - min + 1));// random number class generator
}
int min_for_col(int columnIndex) { // class for number of column
return (columnIndex * 15) + 1;
}
int max_for_col(int column_index) { // int for
return (column_index + 1) * 15;
}
void print_bingo_card(int cards[5][10][5][5], int player, int card) { //class to print up to 10 bingo cards for 5 players
//(5 rows and 5 columns for a total of 25 numbers on each card. )
int i, j;
printf(" B I N G O "); // prints B I N G O on each bingo card generated
for(i = 0; i < 5; ++i) {
for(j = 0; j < 5; ++j) {
if(i == 2 && j == 2) {
printf("fr "); // I'm printint 'fr' instead of 'free' because free has too many characters and it messes my columns.
}
else {
printf("%2d ", cards[player] [card] [i] [j] ); //trying to print spaces between the columns does not work.
}
}
printf(" ");
}
printf(" ");
}
int main() {
srand(time(NULL));
int numOfplayers, num_cards, i, j, k, l, m, num;
int bingo_cards[5][10][5][5];
int count[75];
int numOfrows = 5, num_cols = 5;
int already_exists = 0;
printf(" Enter the number of players: ");
scanf("%d", &numOfplayers);
printf("Enter the number of BINGO cards per player: ");
scanf("%d", &num_cards);
printf("You are playing a game with %d players and each player will have %d cards. ", numOfplayers, num_cards); //
for (i = 0; i < numOfplayers; ++i) {
for (j = 0; j < num_cards; ++j) {
for (k = 0; k < num_cols; ++k) {
for (l = 0; l < numOfrows; ++l) {////////
while (1) {
num = get_random_number(min_for_col(k), max_for_col(k));
already_exists = 0;
for (m = 0; m < l; ++m) {
if (bingo_cards[i][j][m][k] == num) {
already_exists = 1;
}
}
if (already_exists == 0) {
break;
}
}
bingo_cards[i ][j ][l ][k ] = num;
}
}
}
}
// running histogram
for (i = 0; i < 75; ++i) {
count[i] = 0;
}
for (i = 0; i < numOfplayers; ++i) {
for (j = 0; j < num_cards; ++j) {
for (k = 0; k < numOfrows; ++k) {
for (l = 0; l < num_cols; ++l) {
if (!(k == 2 && l == 2)) {
count[bingo_cards[i][j][k][l] - 1]++;/// -1
}
}
}
}
}
while (1) {
printf("1) Display a bingo card ");
printf("2) Run a histogram across all bingo cards generated ");
printf("3) Exit ");
printf("Please enter your choice: ");
scanf("%d", &i);
printf("you have chosen: %d ", i);// may be removed. not necessary
switch (i) {
case 1:
printf("Please enter player number(%d - %d): ", 1, numOfplayers);
scanf("%d", &j);
printf("Please enter card number(%d - %d): ", 1, num_cards);
scanf("%d", &k);
print_bingo_card(bingo_cards, j - 1, k - 1);
break;
case 2:
for (j = 0; j < 75; ++j) {
printf("%2d: ", j + 1);
for (k = 0; k < count[j]; ++k) {
printf("%d",k); // NEED TO FIX
}
printf(" ");
}
break;
case 3:
printf("Goodbye!");
return 0;
default:
printf("You have chosen to exit, Goodbye!");
break;
}
}
return 0;
}