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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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

1
2
3
4
5
6
7
8
9
10
11
12
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
#include <cstdlib>
#include <ctime>

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<Square> 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.