This outputs three bingo cards with random numbers, I need help on how to add th
ID: 3625570 • Letter: T
Question
This outputs three bingo cards with random numbers, I need help on how to add the free space and check to make sure each number is unique.#include <iostream>
using namespace std;
int Rand_Int(int a, int b)
{
return rand() %(b-a+1)+a;
}
int main ()//create the card
{
int grid[5][5];
int b[5], i[5], n[5], g[5], o[5];
int y, count(0);
unsigned int seed;
do{
cout << endl << " B " << "I " << "N " << "G " << "O " << endl;
for(y=0; y<=4; y++)
{
b[y]= Rand_Int (1,15);
i[y]= Rand_Int (16,30);
n[y]= Rand_Int (31,45);
g[y]= Rand_Int (46,60);
o[y]= Rand_Int (61,75);
cout << " " << b[y] << " " << i[y] << " "<< n[y] << " " << g[y] << " " << o[y] << endl;
}
count++;
}while (count<3);
}
Explanation / Answer
please rate - thanks
you can use this to get you started
seefunction fillcard
#include <time.h>
#include <iostream>
#include <iomanip>
using namespace std;
#define SIZE 5
#define SIZE_NUM_ARR 76
#define FLUSH while (getchar() != ' ')
void fillCard ( int card[][SIZE] );
int linearSearch ( int card[][SIZE], int col, int target );
void printCard ( int card[][SIZE] );
int playGame ( int card1[][SIZE], int card2[][SIZE], int matches1[][SIZE], int matches2[][SIZE] );
int getNumber ( int chosenNumArray[] );
void displayNumber ( int currNum );
int checkForMatch ( int card[][SIZE], int matches[][SIZE],int );
int checkWinner ( int matches[][SIZE] );
void showWinner ( char nameStr[], int card[][SIZE], int matches[][SIZE] );
int continueFcn ( void );
void printmatches(int[][SIZE],int);
int main ()
{
srand(time(NULL));
int card1[SIZE][SIZE];
int card2[SIZE][SIZE];
int matches1[SIZE][SIZE]={0};
int matches2[SIZE][SIZE]={0};
int winner,i,j;
do
{for(i=0;i<SIZE;i++)
for(j=0;j<SIZE;j++)
{matches1[i][j]=0;
matches2[i][j]=0;
}
fillCard( card1 );
fillCard( card2 );
cout<<"Your Bingo Card: ";
printCard( card1 );
cout<<" ";
cout<<"Computer's Bingo Card: ";
printCard( card2 );
cout<<" ";
winner = playGame( card1, card2, matches1, matches2 );
if( winner >0&&winner<100 )
showWinner( "You", card1, matches1 );
else if( winner >100 )
showWinner( "Computer", card2, matches2 );
else if( winner == 0 )
cout<<" No one won! ";
}while( continueFcn() );
system("pause");
return 0;
}
void fillCard ( int card[][SIZE] )
{
int i, j;
int c0, c1, c2, c3, c4;
int col;
for( i = 0; i < SIZE; i++ )
{
for( j = 0; j < SIZE; j++ )
{
c0 = rand() % (15) + 1;
while( linearSearch(card, 0, c0) != -1 )
c0 = rand() % (15) + 1;
card[i][0] = c0;
c1 = rand() % (30-16+1) + 16;
while( linearSearch(card, 1, c1) != -1 )
c1 = rand() % (30-16+1) + 16;
card[i][1] = c1;
c2 = rand() % (45-31+1) + 31;
while( linearSearch(card, 2, c2) != -1 )
c2 = rand() % (45-31+1) + 31;
card[i][2] = c2;
c3 = rand() % (60-46+1) + 46;
while( linearSearch(card, 3, c3) != -1 )
c3 = rand() % (60-46+1) + 46;
card[i][3] = c3;
c4 = rand() % (75-61+1) + 61;
while( linearSearch(card, 4, c4) != -1 )
c4 = rand() % (75-61+1) + 61;
card[i][4] = c4;
card[2][2] = 0;
}
}
}
int linearSearch ( int card[][SIZE], int col, int target )
{
int i, j;
for( i = 0; i < SIZE; i++ )
if( target == card[i][col] )
return i;
return -1;
}
void printCard ( int card[][SIZE] )
{
int i, j;
cout<<" B I N G O ";
for( i = 0; i < SIZE; i ++ )
{
for( j = 0; j < SIZE; j++ )
{
if( card[i][j] == 0 )
cout<<" FREE";
else
cout<<setw(5)<<card[i][j];
}
cout<<" ";
}
}
void printmatches(int a[][SIZE],int n)
{int i,j;
cout<<"matches "<<n<<endl;
for(i=0;i<SIZE;i++)
{for(j=0;j<SIZE;j++)
cout<<setw(5)<<a[i][j];
cout<<" ";
}
}
int playGame ( int card1[][SIZE], int card2[][SIZE], int matches1[][SIZE], int matches2[][SIZE] )
{ int done;
int chosenNumArray[SIZE_NUM_ARR] = {0};
matches1[2][2] = 1;
matches2[2][2] = 1;
//printmatches(matches1,1);
//printmatches(matches2,2);
int num;
int currNum;
do
{
num = getNumber( chosenNumArray);
cout<<"Current Bingo Number: ";
displayNumber ( num );
if( checkForMatch( card1, matches1,num ) == 1 )
{cout<<" You got a match!";
// printmatches(matches1,1);
done=checkWinner( matches1 );
if( done!=0 )
return done;
}
if( checkForMatch( card2, matches2,num ) == 1 )
{cout<<" Computer got a match!";
//printmatches(matches2,2);
done=checkWinner( matches2 );
if( done!=0 )
return done;
}
}while ( continueFcn() );
return 0;
}
int getNumber ( int chosenNumArray[] )
{int currNum;
currNum = (rand() % (75-1+1) + 1);
while( chosenNumArray[currNum] != 0 )
currNum = (rand() % (75-1+1) + 1);
chosenNumArray[currNum] = 1;
return currNum;
}
void displayNumber ( int currNum )
{
int i;
if( currNum >= 1 && currNum <= 15 )
cout<<"B-"<<currNum;
else if ( currNum >= 16 && currNum <= 30 )
cout<<"I-"<<currNum;
else if ( currNum >= 31 && currNum <= 45 )
cout<<"N-"<<currNum;
else if ( currNum >= 46 && currNum <= 60 )
cout<<"G-"<<currNum;
else if ( currNum >= 61 && currNum <= 75 )
cout<<"O-"<< currNum;
else if ( currNum == 0 )
cout<<"FREE ";
}
int checkForMatch ( int card[][SIZE], int matches[][SIZE],int currNum )
{
int foundIndex;
int i,j ;
int col;
col=currNum/15;
foundIndex = linearSearch( card, col, currNum );
if( foundIndex >= 0 )
{//cout<<"%d %d %d ",currNum,foundIndex,col);
matches[foundIndex][col] = 1;
return 1;
}
else if( foundIndex < 0 )
return 0;
}
int checkWinner ( int matches[][SIZE] )
{
int i, j, yes,found=0;
//printmatches(matches,-1);
for( i = 0; i < SIZE; i++ )
{
yes = 1;
for( j = 0; j < SIZE; j++ )
{
if( matches[i][j] != 1 )
{
yes = 0;
j = SIZE + 5;
}
}
if( yes == 1 )
for( j = 0; j < SIZE; j++ )
{found=i;
matches[i][j] = 2;
}
}
//printmatches(matches,0);
for( i = 0; i < SIZE; i++ )
{
yes = 1;
for( j = 0; j < SIZE; j++ )
{
if( matches[j][i] != 1 )
{
yes = 0;
j = SIZE + 5;
}
}
if( yes == 1 )
for( j = 0; j < SIZE; j++ )
{found=SIZE+i;
matches[j][i] = 2;
}
}
// printmatches(matches,3);
yes = 1;
for( i = 0; i < SIZE; i++ )
if( matches[i][i] != 1 )
{
yes = 0;
i = SIZE + 5;
}
if( yes == 1 )
for( i = 0; i < SIZE; i++ )
{found=SIZE*2+i;
matches[i][i] = 2;
}
//printmatches(matches,4);
yes = 1;
for( i = 0; i < SIZE; i++ )
if( matches[i][SIZE-i-i] != 1 )
{
yes = 0;
i = SIZE + 5;
}
if( yes == 1 )
for( i = 0; i < SIZE; i++ )
{found=SIZE*3+i;
matches[i][SIZE-i-1] = 2;
}
//printmatches(matches,5);
//if(found!=0)cout<<"found= %d ",found);
return found;
}
void showWinner ( char nameStr[], int card[][SIZE], int matches[][SIZE] )
{
int i, j, k;
cout<<" ";
cout<<nameStr<<" won with numbers: ";
for( i = 0; i < SIZE; i++ )
{
for( j = 0; j < SIZE; j++ )
{
if( matches[i][j] == 2 )
displayNumber(card[i][j]);
}
//displayNumber( k );
}
}
int continueFcn ( void )
{
char answer;
cout<<" Continue? (N for No): ";
scanf(" %c", &answer);
FLUSH;
cout<<" ";
if( toupper(answer) == 'N' )
return 0;
else
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.