Help in C Language please, I have a program for the generation of bingo cards. N
ID: 3605209 • Letter: H
Question
Help in C Language please,
I have a program for the generation of bingo cards. Now that I have my program my question is how do I create a program to play bingo?
I will provide my program for bingo cards (My program):
CREATE A PROGRAM THAT WILL:
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, 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 ...)
Below is the working code I have so far, just need help with what is asked above. (My program):
#include<stdio.h>
#include <stdlib.h>
#include<time.h>
int get_random_number(int min, int max) {
return min + (rand() % (max - min + 1)); //Gets input of random number from user
}
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) { /*Begins to form construct of bingo 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() { //main defined
srand(time(NULL));
int num_players, num_cards, i, j, k, l, m, num;
int bingo_cards[5][10][5][5]; //Array for 4 dimensional selection of bingo cards
int count[75]; //Assigning count integer index to 1-75 as instructed
int num_rows = 5, num_cols = 5; //Declaring number of rows and columns for Bingo card game
int already_exists = 0;
printf("Please enter number of players: "); //Prompts user input for number of players for bingo card game
scanf("%d", &num_players);
printf("Please enter number of cards per player: ");//Prompts user input for number of cards per player
scanf("%d", &num_cards);
for(i = 0; i < num_players; ++i) { // num players incremented by 1 if user chooses to add players
for(j = 0; j < num_cards; ++j) {//num cards incremented by 1
for(k = 0; k < num_cols; ++k) {// num cols incremented by 1
for(l = 0; l < num_rows; ++l) { //num rows incremented by 1
while(1) {
num = get_random_number(min_for_col(k), max_for_col(k)); /* Gets random number for column and checks to see if already exist*/
already_exists = 0;
for(m = 0; m < l; ++m) {
if(bingo_cards[i][j][m][k] == num) {
already_exists = 1;
}
}
if(already_exists == 0) { //Test cases checking if user input exists in row/col
break;
}
}
bingo_cards[i][j][l][k] = num;
}
}
}
}
// calculation for histogram using for loop.
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 "); //Prompts user input to display bingo card
printf("2. To show histogram "); //Prompts user inpu to display histogram
printf("3. Exit "); //If user no longer wishes to play user exits
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); //Read in user input 1-75
printf("Please enter card number(%d - %d): ", 1, num_cards);
scanf("%d", &k); //Read in user input for card number 1-75
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:
return 0;
default:
printf("Invalid choice. Please try again!!"); //test cases if user enters letter or number not listed bingo game exits
break;
}
}
return 0;
}
(Heres a sample Execution:)
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));
}
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() { //main defined
srand(time(NULL));
int num_players, num_cards, i, j, k, l, m, num;
int bingo_cards[5][10][5][5]; //Array for 4 dimensional selection of bingo cards
int count[75]; //Assigning count integer index to 1-75 as instructed
int num_rows = 5, num_cols = 5int 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) { // num players incremented by 1 if user chooses to add players
for(j = 0; j < num_cards; ++j) {//num cards incremented by 1
for(k = 0; k < num_cols; ++k) {// num cols incremented by 1
for(l = 0; l < num_rows; ++l) { //num rows incremented by 1
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) { //Test cases checking if user input exists in row/col
break;
}
}
bingo_cards[i][j][l][k] = num;
}
}
}
}
// calculation for histogram using for loop.
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 "); //If user no longer wishes to play user exits
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:
return 0;
default:
printf("Invalid choice. Please try again!!"); bingo game exits
break;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.