Continuing with Bingo Submit your code to the project3 link on Blackboard. Now t
ID: 3604338 • Letter: C
Question
Continuing with Bingo Submit your code to the project3 link on Blackboard. Now that we have our Bingo Cards we should play Bingo!!! Create a program that will: do everything from program 2 (create bingo cards). 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 ...) This is my code from project number 2. how do i modify it to complete the tasks mentioned above? If possible please Bold the changes made to the code.
#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("F ");
}
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("Enter number of players:"); //asking for player count
scanf("%d", &num_players);//scanning user input
printf("Enter number of cards per players:");//Asking for number of cards per person.
scanf("%d", &num_cards);//scanning user input
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 < 1; ++m){
if(bingo_cards[i][j][m][k] == num){
already_exists = 1;
}
}
if(already_exists == 0){
break;
}
}
bingo_cards[i][j][l][k] = num;
}
}
}
}
//histogram calculations
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 && 1==2)){
count[bingo_cards[i][j][k][l] - 1]++;
}
}
}
}
}
while(1){ //while(1) means while true. so the loop continues to loop until the user presses 3 to exit the loop.
printf("Press 1 to select user bingo card to display ");
printf("Press 2 to show histogram ");
printf("Press 3 to exit ");
printf("Please enter your choice: ");//asking for user input
scanf("%d", &i);//reading user input
switch(i){
case 1:
printf("Enter player number(%d-%d): ", 1, num_players);
scanf("%d", &j);
printf("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("*"); /* when the histogram option is selected, numbers rangine from 1-75 will be shown. The asterisk will represent
how many times each number has appearead on the cards.*/
}
printf(" ");
}
break;
case 3:
return 0;
default: //in case the user inputs an invalid number they are told to make another selection.
printf("Invalid Choice. Make another selection.");
break;
}
}
return 0;
}
Explanation / Answer
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define CYCLES 1000
typedef struct elem
{
int number;
int found;
}elem;
static int check_number(elem card[][5], int n);
static int check_bingo(elem card[][5], int row, int col);
void single_card_bingo_simulate(void)
{
elem card[5][5];
int cycle;
long long sum;
int avg;
srand((unsigned int)time(NULL));
/* run bingo simulation for CYCLES times */
for(cycle = 0, sum = 0; cycle < CYCLES; cycle++)
{
int num_call;
int i, j, range, offset;
/* fill card with random numbers */
for(i = 0, range = 15, offset = 1; i < 5; i++, offset += 15)
{
for(j = 0; j < 5; j++)
{
card[i][j].number = (rand() % range) + offset;
card[i][j].found = 0;
}
}
/* keep calling until bingo */
for(num_call = 0; ;)
{
int rand_num;
/* selet a random number between 1 to 75 including 1 and 75 */
rand_num = (rand() % 75) + 1;
num_call++;
/* check if the random number is present in card */
if(check_number(card, rand_num))
break;
}
sum += num_call;
}
/* print average number of calls */
avg = sum/CYCLES;
printf("average number of calls = %d ", avg);
}
/* set found if the number is found. Then check condition for bingo and return 1 if true; otherwise return 0; */
static int check_number(elem card[][5], int n)
{
int i, j;
for(i = 0; i < 5; i++)
{
for(j = 0; j < 5; j++)
{
if(card[i][j].found == 0 && card[i][j].number == n)
{
card[i][j].found = 1;
goto FOUND;
}
}
}
FOUND:
if(i == 5 && j == 5) /* not found */
return 0;
return check_bingo(card, i, j);
}
/* return 1 if bingo; otherwise return 0 */
static int check_bingo(elem card[][5], int row, int col)
{
int i, j;
/* check row */
for(i = 0; (i < 5) && (card[row][i].found == 1); i++);
if(i == 5)
return 1;
/* check column */
for(i = 0; (i < 5) && (card[i][col].found == 1); i++);
if(i == 5)
return 1;
/* check first diagonal */
if(row == col)
{
for(i = 0; (i < 5) && (card[i][i].found == 1); i++);
if(i == 5)
return 1;
}
/* check second diagonal */
if((row + col) == 4)
{
for(i = 0, j = 4; (i < 5) && (card[i][j].found == 1); i++, j--);
if(i == 5)
return 1;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.