Hi i have a C language programming project for my computer science class that I
ID: 3601414 • Letter: H
Question
Hi i have a C language programming project for my computer science class that I am having difficulty with. This is a continuation of my previous code that I made so I will provide the code I have and what needs to be done is add another option into the code. I will attach a sample execution where when the user picks number 4 so they can play a game of bingo. The IDE I currently use is Dev C++. If there are any questions or anything confusing please let me know as I need this code done asap! thank you
For this project this is what my instructor told us.
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 per @10 (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
This is my current code right now.
-----------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
int getRandomNumber(int max, int min)
{
return min + (rand() % (max - min + 1));
}
int maxCol(int Col)
{
return ((Col + 1) * 15);
}
int minCol(int Col)
{
return ((Col * 15) + 1);
}
void bingoCard(int cards[5][10][5][5], int Players, int Cards)
{
int i; // i and j are used for the for loops
int j;
printf(" B I N G O ");
for(i = 0; i < 5; i++)
{
for(j = 0; j < 5; j++)
{
if(j == 2 && i == 2)
{
printf(" free ");
}
else
{
printf(" %2d ", cards[Players][Cards][i][j]);
}
}
printf(" ");
}
printf(" ");
}
int main()
{
srand(time(NULL));
int numofPlayers;
int numofCards;
int number;
int i;
int j;
int k;
int l;
int m;
int numofRows = 5;
int numCols = 5;
int bingoCards[5][10][5][5];
int count[75];
int Var = 0;
int Generator;
int pick = 1;
printf("Enter number of players: ");
scanf("%d", &numofPlayers);
if(numofPlayers > 5)
{
printf("Max players allowed are 5, please enter a number less than 5 ");
printf("Enter number of players: ");
scanf("%d", &numofPlayers);
}
printf("Enter the number of BINGO cards per player: ");
scanf("%d", &numofCards);
if(numofCards > 10)
{
printf("Max number of cards are 10, please enter a number less than 10 ");
printf("Enter the number of BINGO cards per player: ");
scanf("%d", &numofCards);
}
printf("You are playing a game with %d players and each player will have %d cards ", numofPlayers, numofCards);
printf("We have generated %d bingo cards. ", (numofPlayers * numofCards)); // This will do the math for how many people are playing and how many cards will be used.
for(i = 0; i < numofPlayers; i++)
for(j = 0; j < numofCards; j++)
{
for(k = 0; k < numCols; k++)
{
for(l = 0; l < numofRows; l++)
{
while(pick)
{
number = getRandomNumber(minCol(k), maxCol(k));
Var = 0;
for(m = 0; m < l; ++m)
{
if(bingoCards[i][j][m][k] == number)
{
Var = 1;
}
}
if(Var == 0)
{
break;
}
}
bingoCards[i][j][l][k] = number;
}
}
}
for(i = 0; i < 75; ++i)
{
count[i] = 0;
}
for(i = 0; i < numofPlayers; i++)
{
for(j = 0; j < numofCards; j++)
{
for(k = 0; k < numofRows; k++)
{
for(l = 0; l < numCols; l++)
{
if(!(k == 2 && l == 2))
{
count[bingoCards[i][j][k][l] - 1]++;
}
}
}
}
}
while(pick)
{
printf("Please choose an option from the following menu: ");
printf("1) Display a bingo card ");
printf("2) Run a histogram across all bingo cards generated "); //Display menu options for the user
printf("3) Exit ");
printf("You have chosen ");
scanf("%d", &i);
switch(i)
{
case 1:
printf("Enter the player and players card you would like to display, ");
printf("First player is 1, last player is %d ", numofPlayers);
printf("First card is 1, last card is %d ", numofCards);
scanf("%d %d", &j, &k);
bingoCard(bingoCards, (j - 1), (k - 1));
break;
case 2:
for(j = 0; j < 75; ++j)
{
Generator = (rand() % 25) + 1;
if(j < 10)
{
printf("%d", j + 1);
}
else
{
printf("%d", j + 1);
}
if(j < 10 && Generator < 10)
{
printf("-%d ", Generator);
}
else if(j < 10 && Generator >= 10)
{
printf("-%d ", Generator);
}
else
{
printf("-%d ", Generator);
}
if(j % 10 == 9)
{
printf(" ");
}
}
printf(" ");
break;
case 3:
printf("GoodBye");
return 0;
default:
printf("Invalid choice, try again. ");
break;
}
}
return 0;
}
Explanation / Answer
#include<iostream>
#include<iomanip>
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
using namespace std;
// Returns random number generated between max and min passed as function parameter
int getRandomNumber(int max, int min)
{
return min + (rand() % (max - min + 1));
}// End of function
// Function to return maximum columns
int maxCol(int Col)
{
return ((Col + 1) * 15);
}// End of function
// Function to return minimum column
int minCol(int Col)
{
return ((Col * 15) + 1);
}// End of function
// Function to generate bingo card for the specified player passed as a parameter to the function
void bingoCard(int cards[5][10][5][5], int Players, int Cards)
{
// i and j are used for the for loops
int i;
int j;
cout<<" B I N G O ";
// Loops 5 times
for(i = 0; i < 5; i++)
{
// Loops 5 times
for(j = 0; j < 5; j++)
{
// Checks if row index and column index is 2
if(j == 2 && i == 2)
{
cout<<" free ";
}// End of if
// Otherwise displays cards for the player
else
{
cout<<setw(4)<<cards[Players][Cards][i][j];
}// End of else
}// End of inner for
cout<<endl;
}// End of outer for
cout<<endl;
}// End of function
// Main function definition
int main()
{
// For random number time
srand(time(NULL));
// Local variables
// For number of players
int numofPlayers;
// For number of cards
int numofCards;
// To store random number generated
int number;
// Loops variables
int i;
int j;
int k;
int l;
int m;
int numofRows = 5;
int numCols = 5;
int bingoCards[5][10][5][5];
int count[75];
int Var = 0;
int Generator;
int pick = 1;
// Accepts number of players
cout<<"Enter number of players: ";
cin>>numofPlayers;
// Checks if number of players more than five
if(numofPlayers > 5)
{
cout<<"Max players allowed are 5, please enter a number less than 5 ";
cout<<"Enter number of players: ";
cin>>numofPlayers;
}// End of if
// Accepts number of bingo cards per player
cout<<"Enter the number of BINGO cards per player: ";
cin>>numofCards;
// Checks if the number of cards are more than 10
if(numofCards > 10)
{
cout<<"Max number of cards are 10, please enter a number less than 10 ";
cout<<"Enter the number of BINGO cards per player: ";
cin>>numofCards;
}// End of if
// Displays number of players and number of cards each player
cout<<"You are playing a game with "<<numofPlayers<<" players and each player will have "<<numofCards<<" cards ";
// This will do the math for how many people are playing and how many cards will be used.
cout<<"We have generated "<<(numofPlayers * numofCards)<<" bingo cards. ";
// Loops till number of players
for(i = 0; i < numofPlayers; i++)
// Loops till number of cards each player having
for(j = 0; j < numofCards; j++)
{
// Loops till number of columns
for(k = 0; k < numCols; k++)
{
// Loops till number of rows
for(l = 0; l < numofRows; l++)
{
// Loops while pick value is one. Initially pick value is one
while(pick)
{
// Generates a random number
// maximum range for random number is minCol(k)
// minimum range for random number is maxCol(k)
number = getRandomNumber(minCol(k), maxCol(k));
Var = 0;
// Loops till l
for(m = 0; m < l; ++m)
{
// Checks if the bingoCards matrix i, j, m, k index position is the number
if(bingoCards[i][j][m][k] == number)
{
// Set the Var value to one
Var = 1;
}// End of if
}// End of for loop for variable m
// Checks if the Var value is zero
// Means random number generated is not matched with the matrix contents
if(Var == 0)
{
break;
}// End of if
}// End of while
// Store randomly generated number in the matrix
bingoCards[i][j][l][k] = number;
}// End of for loop for variable l
}// End of for loop for variable k
}// End of for loop for variable j
// Loops 75 times
for(i = 0; i < 75; ++i)
{
// Initializes the count array each position to zero
count[i] = 0;
}// End of for loop
// Loops till number of players
for(i = 0; i < numofPlayers; i++)
{
// Loops till number of cards each player having
for(j = 0; j < numofCards; j++)
{
// Loops till number of rows
for(k = 0; k < numofRows; k++)
{
// Loops till number of columns
for(l = 0; l < numCols; l++)
{
// Checks if k and l value is not 2
if(!(k == 2 && l == 2))
{
// Increase the counter array's bingCards matrix i, j, k, l index position value minus one position value by one
count[bingoCards[i][j][k][l] - 1]++;
}// End of if
}// End of for loop for variable l
}// End of for loop for variable k
}// End of for loop for variable j
}// End of for loop for variable i
// Loops while pick is one
while(pick)
{
// Displays the menu
cout<<"Please choose an option from the following menu: ";
cout<<"1) Display a bingo card ";
cout<<"2) Run a histogram across all bingo cards generated "; //Display menu options for the user
cout<<"3) Exit ";
// Accepts user choice
cout<<"You have chosen ";
cin>>i;
// Checks the option entered by the user in i using switch - case
switch(i)
{
case 1:
cout<<"Enter the player and players card you would like to display, ";
cout<<"First player is 1, last player is "<<numofPlayers<<endl;
cout<<"First card is 1, last card is "<<numofCards<<endl;
cin>>j>>k;
bingoCard(bingoCards, (j - 1), (k - 1));
break;
case 2:
for(j = 0; j < 75; ++j)
{
Generator = (rand() % 25) + 1;
if(j < 10)
{
cout<<j + 1;
}// End of if
else
{
cout<<j + 1;
}// End of else
if(j < 10 && Generator < 10)
{
cout<<std::left<<setw(4)<<Generator;
}// End of if
else if(j < 10 && Generator >= 10)
{
cout<<std::left<<setw(4)<<Generator;
}// End of else if
else
{
cout<<std::left<<setw(4)<<Generator;
}// End of else
if(j % 10 == 9)
{
cout<<endl;
}// End of if
}// End of for
cout<<endl;
break;
case 3:
cout<<"GoodBye";
return 0;
default:
cout<<"Invalid choice, try again. ";
break;
}// End of switch
}// End of while
return 0;
}// End of main method
Sample Run:
Enter number of players: 2
Enter the number of BINGO cards per player: 3
You are playing a game with 2 players and each player will have 3 cards
We have generated 6 bingo cards.
Please choose an option from the following menu:
1) Display a bingo card
2) Run a histogram across all bingo cards generated
3) Exit
You have chosen 1
Enter the player and players card you would like to display,
First player is 1, last player is 2
First card is 1, last card is 3
2
1
B I N G O
18 34 45 69 86
24 35 57 62 85
21 38 free 66 82
27 40 53 65 77
26 39 51 70 81
Please choose an option from the following menu:
1) Display a bingo card
2) Run a histogram across all bingo cards generated
3) Exit
You have chosen 2
14 218 318 421 514 615 722 89 920 1015
1125 1218 1321 147 1522 165 1712 1813 1915 2017
2124 228 235 249 255 262 2724 283 2919 3016
3123 3214 335 3411 3523 3616 3715 3812 3925 4022
417 4218 4315 4420 4519 4610 4717 4815 4914 5013
512 5220 5315 5420 5521 5617 579 5819 5911 604
6114 6224 6322 647 6521 666 677 6822 6925 705
7116 725 731 747 7520
Please choose an option from the following menu:
1) Display a bingo card
2) Run a histogram across all bingo cards generated
3) Exit
You have chosen 3
GoodBye
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.