### BINGO

Here It is for Bingo game. Here i am going to get 9 number ie 1-9. if he/she enter 1 then it check whether it is located in a or not.If it is present then replace it with 0.Upto this it is ok but if the enter number is not present i want to display that enter another number which is not present in array n want to get that number and to continue the process. How to do that.
 ``123456789101112131415161718`` ``````int a[3][3]={1,2,3,4,5,6,7,8,9},k; cout<<"Enter a number"; for(int l=0;l<9;l++) { cin>>k; for(int i=0;i<3;i++){ for(int j=0;j<3;j++) { if(a[i][j]==k) { cout<<"\n Enter next number"; a[i][j]=0; } } } } ``````
Last edited on
closed account (3qX21hU5)
First off I would highly recommend you ditch the multi demensional array and go for a standard vector.

This would work much easier and save you some problems later on.
`std::vector<int> board = {1, 2, 3, 4, 5, 6, 7, 8, 9};`

That would also open up some possibilities for your current problem. Like using any_of like so

 ``123456789101112`` ``````int guess; cin >> guess; if (!std::any_of(board.begin(), board.end(), [guess] (int i) { return i == guess; })) { std::cout << "That number is not on the board! Please enter another number" << std::endl; std::cin >> guess; } else { // Do whatever happens when the user guesses a number on the board. }``````

This code doesn't take into account multiple guesses but it shows a possible option.

Though that is a bit complicated I admit so your best bet is to just loop through your array or vector and check if the number the user entered matches any of the numbers in the array or vector. You can do this with a function if you know about them yet which would be easiest or you can just do it with loops and a conditional variable.

for example

 ``1234567891011121314`` ``````int myArray[3] = {1, 2, 3}; int guess; cin >> guess; bool found = false; for (int i = 0; i != 3; ++i) { if (myArray[i] == guess) { found = true; break; } }``````

That is probably the most simple solution but there are many more out there also.
Last edited on
So I played around a bit and got a good Bingo simulation (I think). I ran it in the debugger with a breakpoint at line 78:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229`` ``````#include #include #include #include #include #include class RandSeed // Seeds the random number { public: RandSeed() { srand(time(0)); } } g_seed; enum Letter { B = 0, I = 1, N = 2, G = 3, O = 4 }; class Square { public: int number; bool revealed; Letter letter; Square() : revealed(false) {} Square(int i) : number(i), revealed(false) { setLetter(); } void operator=(int num); bool operator==(Square other); private: void setLetter(); }; class Board { Square sq[5][5]; void Reveal(Square call); bool CheckRows(); bool CheckCols(); bool CheckDiags(); bool CheckRow(int r); bool CheckCol(int c); bool CheckDiag1(); bool CheckDiag2(); public: Board(); bool Call(Square call); void Paint(); bool CheckBingo(); }; class Caller { std::vector rollything; public: Caller(); Square GetCall(); }; int main() { Caller c; Board b; b.Paint(); while ( !b.CheckBingo() ) { b.Call( c.GetCall() ); b.Paint(); } std::cout << "You win!" << std::endl; } ///////////////////////////////////////////////////////////////// void Square::operator=(int num) { this->number = num; this->setLetter(); if (num == 0) this->revealed = true; } bool Square::operator==(Square other) { return other.number == this->number; } void Square::setLetter() { if (number == 0 ) letter = N; else if (number <= 15) letter = B; else if (number <= 30) letter = I; else if (number <= 45) letter = N; else if (number <= 60) letter = G; else if (number <= 75) letter = O; } Board::Board() { Square squares[75]; for (int i = 1; i <= 75; ++i) squares[i-1] = i; std::random_shuffle(squares , squares + 15); std::random_shuffle(squares + 15, squares + 30); std::random_shuffle(squares + 30, squares + 45); std::random_shuffle(squares + 45, squares + 60); std::random_shuffle(squares + 60, squares + 75); for (int i = 0; i < 5; ++i) for (int j = 0; j < 5; ++j) this->sq[i][j] = squares[i*15 + j]; sq[N][2] = 0; } bool Board::Call(Square call) { Reveal(call); return CheckBingo(); } void Board::Reveal(Square call) { for (int i = 0; i < 5; ++i) if (this->sq[call.letter][i] == call) sq[call.letter][i].revealed = true; } bool Board::CheckBingo() { return CheckRows() || CheckCols() || CheckDiags(); } bool Board::CheckRows() { for (int i = 0; i < 5; ++i) if (CheckRow(i)) return true; return false; } bool Board::CheckCols() { for (int i = 0; i < 5; ++i) if (CheckCol(i)) return true; return false; } bool Board::CheckRow(int r) { for (int c = 0; c < 5; ++c) if ( ! this->sq[c][r].revealed ) return false; return true; } bool Board::CheckCol(int c) { for (int r = 0; r < 5; ++r) if ( ! this->sq[c][r].revealed ) return false; return true; } bool Board::CheckDiags() { return CheckDiag1() || CheckDiag2(); } bool Board::CheckDiag1() { for (int r = 0; r < 5; ++r) if ( ! this->sq[r][r].revealed ) return false; return true; } bool Board::CheckDiag2() { for (int r = 0; r < 5; ++r) if ( ! this->sq[r][4-r].revealed ) return false; return true; } void Board::Paint() // Re-implement this if you want to put it out of the console { std::cout << "B |I |N |G |O \n"; for (int i = 0; i < 5; ++i) { for (int j = 0; j < 5; ++j) { if (this->sq[j][i].revealed) std::cout << "X |"; else std::cout << std::setw(2) << this->sq[j][i].number << "|"; } std::cout << std::endl; } std::cout << std::endl; } Caller::Caller() { for (int i = 1; i <= 75; ++i) rollything.push_back( Square(i) ); std::random_shuffle( rollything.begin(), rollything.end() ); } Square Caller::GetCall() { Square retval = rollything[0]; rollything.erase( rollything.begin() ); return retval; }``````
while running above program there is no header file for the above program ... That is it showing that there is no algorithm header file is available.. where to run these program ???
Are you sure that you are compiling this in C++ and not C? What compiler are you using? This file is in the standard library and has been since at least C++03.

http://www.cplusplus.com/reference/algorithm/
i have download code::blocks and in that it is running... Thank u ...
Topic archived. No new replies allowed.