Bingo Help

I am creating a bingo program (not the entire game). I'm having trouble getting the numbers not to repeat and put a box around the entire board. The box is at the end of the code.
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
# include <string>
# include <iostream>
# include <time.h>
# include <stdlib.h>
using namespace std;
int card[5][5][2];
///////////////////////////////////////////////////////////////////////
int createcard()
{
    int x,y,choice;
    for (y=0;y<5;y++)
    {
        for (x=0;x<5;x++)
        {
            choice=rand()%15+1+15*x;
            if (choice<10) cout<<" ";
            cout<<choice<<" ";
        }
        cout<<"\n";
    }
    return 0;
}
///////////////////////////////////////////////////////////////////////
int main()
{
    system("espeak -s 130 'Welcome to Bingo!'");
    srand(time(NULL));
    createcard();
	return 0;
}
//B=1-15
//I=16-30
//N=31-45
//G=46-60
//O=61-75
/*
╔════╦════╦════╦════╦════╗
║    ║    ║    ║    ║    ║
║    ║    ║    ║    ║    ║
╠════╬════╬════╬════╬════╣
║    ║    ║    ║    ║    ║
║    ║    ║    ║    ║    ║
╠════╬════╬════╬════╬════╣
║    ║    ║Free║    ║    ║
║    ║    ║Free║    ║    ║
╠════╬════╬════╬════╬════╣
║    ║    ║    ║    ║    ║
║    ║    ║    ║    ║    ║
╠════╬════╬════╬════╬════╣
║    ║    ║    ║    ║    ║
║    ║    ║    ║    ║    ║
╚════╩════╩════╩════╩════╝
*/
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
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

void printCard(int card[][5])
{
    cout << "╔════╦════╦════╦════╦════╗\n";
    for (int row = 0; row < 5; row++) {
        cout << "║";
        for (int col = 0; col < 5; col++)
            if (row == 2 && col == 2)
                cout << "Free║";
            else
                cout << setw(3) << card[row][col] << " ║";
        if (row < 4)
            cout << "\n╠════╬════╬════╬════╬════╣\n";
    }
    cout << "\n╚════╩════╩════╩════╩════╝\n";
}

void createCard(int card[][5]) {
    for (int col = 0; col < 5; col++) {
        bool used[15] = {false};
        for (int row = 0; row < 5; row++) {
            int choice = 0;
            do
                choice = rand() % 15;
            while (used[choice]);
            used[choice] = true;
            card[row][col] = 1 + 15 * col;
        }
    }
}

int main() {
    int card[5][5];
    srand(unsigned(time(0)));

    createCard(card);
    printCard(card);

    return 0;
}

Last edited on
tpb,
Thank you for your help but your code prints out only 5 numbers and repeats them.
If you can help me fix that (if you can't that is fine) will be appreciated.
Last edited on
@CNoob2

Here's a Bingo card program I wrote awhile back. I didn't use any ansi graphics, but you can add them if you wish.
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
#include<iostream>
#include <ctime>
#include <windows.h> 

using namespace std;

void ClearScreen();

int main()
{

	int num[5][5], number, row, col;
	int ask;
	bool ok = true;

	srand((unsigned)time(0)); // Better srand() call

	do{

		ClearScreen(); // Using system calls, isn't advised. Here's a good ClearScreen() function
		cout << endl << endl << "\t\t\t\t    BINGO GAME" << endl;
		for (row = 0; row<5; row++)
			for (col = 0; col<5; col++)
				num[row][col] = 0; // Zeroes out all the rows & columns
		cout << endl << endl << "\tB\t\tI\t\tN\t\tG\t\tO" << endl << endl << endl;

		for (col = 0; col < 5; col++)
		{
			for (row = 0; row < 5; row++)
			{
				do
				{
					ok = true;
					number = 1 + (col * 15) + rand() % 15;
					for (int ck = 0; ck<5; ck++)
					{
						if (number == num[ck][col])
							ok = false; // Same number found, so create a new number
					}
				} while (!ok);
				num[row][col] = number;
			}
		}


		for (row = 0; row < 5; row++)
		{
			for (col = 0; col < 5; col++)
			{
				if ((row == 2) && (col == 2))
					cout << "\tFREE\t";
				else
					cout << "\t" << num[row][col] << "\t";

			}
			cout << endl;
		}
		
		{
			cout << endl << endl << "\tIs this the card you want to use? [0 for no only.] : ";
			cin >> ask;
		}
	} while (ask == 0);

	return 0;
}

void ClearScreen()
{
	DWORD n;
	DWORD size;
	COORD coord = { 0 };
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
	GetConsoleScreenBufferInfo(h, &csbi);
	size = csbi.dwSize.X * csbi.dwSize.Y;
	FillConsoleOutputCharacter(h, TEXT(' '), size, coord, &n);
	GetConsoleScreenBufferInfo(h, &csbi);
	FillConsoleOutputAttribute(h, csbi.wAttributes, size, coord, &n);
	SetConsoleCursorPosition(h, coord);
}
I now need the numbers to be in numerical order, check bingo, and another card that you can play on.
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
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
///////////////////////////////////////////////////////////////////////
void printCard(int card[5][5])
{
    char normal[] = { 0x1b, '[', '0', ';', '3', '9', 'm', 0 }; //Declare Variables
	char red[] = { 0x1b, '[', '0', ';', '3', '1', 'm', 0 };
	char blue[] = { 0x1b, '[', '0', ';', '3', '4', 'm', 0 };
	char green[] = { 0x1b, '[', '0', ';', '3', '2', 'm', 0 };
	char purple[] = { 0x1b, '[', '0', ';', '3', '5', 'm', 0 };
	char cyan[] = { 0x1b, '[', '0', ';', '3', '6', 'm', 0 };
	char yellow[] = { 0x1b, '[', '0', ';', '3', '3', 'm', 0 };
	cout<<blue<<"   B "<<green<<"   I "<<red<<"   N "<<yellow<<"   G "<<cyan<<"   O "<<normal<<"\n";
    cout<<purple<<"╔════╦════╦════╦════╦════╗\n";
    for (int row=0;row<5;row++)
	{
        cout<<purple<<"║";
        for (int col=0;col<5;col++)
            if (row==2&&col==2)
                cout<<normal<<"Free"<<purple<<"║"<<normal;
            else
                cout<<normal<<setw(3)<<card[row][col]<<purple<<" ║"<<normal;
        if (row<4)
            cout<<purple<<"\n╠════╬════╬════╬════╬════╣\n";
    }
   cout<<purple<<"\n╚════╩════╩════╩════╩════╝\n"<<normal;
}
///////////////////////////////////////////////////////////////////////
void createCard(int card[5][5])
{
	int number, row, col; 						//Declare Variables
	bool ok=true;
    for (col=0;col<5;col++)
		{
			for (row=0;row<5;row++)
			{
				do
				{
					ok=true;
					number=1+(col*15)+rand()%15;
					for (int ck=0;ck<5;ck++)
					{
						if (number==card[ck][col])
							ok=false; // Same number found, so create a new number
					}
				}while (!ok);
				card[row][col]=number;
			}
		}
}
///////////////////////////////////////////////////////////////////////
void playbingo(int card[5][5])
{
	printCard(card);
}
///////////////////////////////////////////////////////////////////////
void checkBingo(int card[5][5])
{
	system("espeak Bingo!");
}
///////////////////////////////////////////////////////////////////////
int main() {
    int card[5][5];
    srand(unsigned(time(0)));
    //system("espeak -s 130 'Welcome to Bingo!'");
    createCard(card);
    do
    {
		playbingo(card);
    }while (!checkBingo);
    return 0;
}
Last edited on
CNoob2 wrote:
but your code prints out only 5 numbers and repeats them.

No they don't repeat. Did you even run it?
Anyway, I added the sorting.
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
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;

void printCard(int card[][5])
{
    cout << "╔════╦════╦════╦════╦════╗\n";
    for (int row = 0; row < 5; row++) {
        cout << "║";
        for (int col = 0; col < 5; col++)
            if (row == 2 && col == 2)
                cout << "Free║";
            else
                cout << setw(3) << card[row][col] << " ║";
        if (row < 4)
            cout << "\n╠════╬════╬════╬════╬════╣\n";
    }
    cout << "\n╚════╩════╩════╩════╩════╝\n";
}

void createCard(int card[][5])
{
    for (int col = 0; col < 5; col++)
    {
        bool used[15] = {false};
        int n[5];
        for (int row = 0; row < 5; row++)
        {
            int choice = 0;
            do
                choice = rand() % 15;
            while (used[choice]);
            used[choice] = true;
            choice += 1 + 15 * col;
            n[row] = choice;
        }
        sort(&n[0], &n[5]);
        for (int row = 0; row < 5; row++)
            card[row][col] = n[row];
    }
}

int main()
{
    int card[5][5];

    srand(unsigned(time(0)));

    createCard(card);
    printCard(card);

	return 0;
}



$ ./bingo
╔════╦════╦════╦════╦════╗
║  2 ║ 23 ║ 33 ║ 48 ║ 63 ║
╠════╬════╬════╬════╬════╣
║  4 ║ 24 ║ 35 ║ 50 ║ 66 ║
╠════╬════╬════╬════╬════╣
║  6 ║ 25 ║Free║ 51 ║ 71 ║
╠════╬════╬════╬════╬════╣
║  7 ║ 28 ║ 39 ║ 52 ║ 73 ║
╠════╬════╬════╬════╬════╣
║  9 ║ 29 ║ 44 ║ 59 ║ 74 ║
╚════╩════╩════╩════╩════╝
$ ./bingo
╔════╦════╦════╦════╦════╗
║  8 ║ 17 ║ 33 ║ 47 ║ 62 ║
╠════╬════╬════╬════╬════╣
║ 10 ║ 18 ║ 34 ║ 49 ║ 65 ║
╠════╬════╬════╬════╬════╣
║ 11 ║ 21 ║Free║ 50 ║ 68 ║
╠════╬════╬════╬════╬════╣
║ 12 ║ 22 ║ 41 ║ 56 ║ 74 ║
╠════╬════╬════╬════╬════╣
║ 15 ║ 26 ║ 44 ║ 60 ║ 75 ║
╚════╩════╩════╩════╩════╝
$ ./bingo
╔════╦════╦════╦════╦════╗
║  2 ║ 16 ║ 36 ║ 48 ║ 63 ║
╠════╬════╬════╬════╬════╣
║  3 ║ 23 ║ 37 ║ 52 ║ 65 ║
╠════╬════╬════╬════╬════╣
║  5 ║ 25 ║Free║ 53 ║ 67 ║
╠════╬════╬════╬════╬════╣
║  6 ║ 26 ║ 39 ║ 56 ║ 68 ║
╠════╬════╬════╬════╬════╣
║  9 ║ 28 ║ 44 ║ 59 ║ 71 ║
╚════╩════╩════╩════╩════╝
I need to detect bingo now and this will be done.
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
# include <iostream>
# include <string.h>
# include <time.h>
# include <cstdlib>
# include <stdio.h>
# include <termios.h>
# include <unistd.h>
# include <iomanip>
using namespace std;
///////////////////////////////////////////////////////////////////////
int getch()
{
	struct termios oldt, newt;
	int ch;
	tcgetattr(STDIN_FILENO, &oldt);
	newt=oldt;
	newt.c_lflag &= ~( ICANON | ECHO );
	tcsetattr(STDIN_FILENO, TCSANOW, &newt);
	ch=getchar();
	tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
	return ch;
}
///////////////////////////////////////////////////////////////////////
int MakeCard(int Board[5][15][2])
{
	srand(time(NULL));
	int x,y,z,temp,random,swap;
	for (y=0;y<15;y++)
		for (x=0;x<5;x++)
			Board[x][y][0]=x*15+y+1;
	for (y=0;y<5;y++)
	{
		for (x=0;x<5;x++)
		{
			random=rand()%15;
			temp=Board[x][random][0];
			Board[x][random][0]=Board[x][y][0];
			Board[x][y][0]=temp;
		}
	}
	for (z=0;z<5;z++)
		for (y=0;y<5;y++)
			for (x=0;x<5;x++)
				if (Board[x][y][0]>Board[x][y+1][0])
				{
					swap=Board[x][y][0];			// Sort dice
					Board[x][y][0]=Board[x][y+1][0];
					Board[x][y+1][0]=swap;
				}
	for (y=0;y<5;y++)
		for (x=0;x<5;x++)
			Board[x][y][1]=0;
	return 0;	
}
///////////////////////////////////////////////////////////////////////
int Color(int Board[5][15][2],int posx,int posy)
{
	char normal[]={ 0x1b, '[', '0', ';', '3', '9', 'm', 0 }; //Declare Variables
	char red[]={ 0x1b, '[', '0', ';', '3', '1', 'm', 0 };
	char blue[]={ 0x1b, '[', '0', ';', '3', '4', 'm', 0 };
	char green[]={ 0x1b, '[', '0', ';', '3', '2', 'm', 0 };
	char purple[]={ 0x1b, '[', '0', ';', '3', '5', 'm', 0 };
	char cyan[]={ 0x1b, '[', '1', ';', '3', '6', 'm', 0 };
	char yellow[]={ 0x1b, '[', '0', ';', '3', '3', 'm', 0 };
	int x,y;
	cout<<blue<<"   B "<<green<<"   I "<<red<<"   N "<<purple<<"   G "<<yellow<<"   O "<<normal<<"\n";
	cout<<cyan<<"╔════╦════╦════╦════╦════╗\n";
	for (y=0;y<5;y++)
	{
		for (x=0;x<5;x++)
			{
				cout<<cyan<<"║ ";
				if (Board[x][y][0]<10) cout<<" ";
				if (Board[x][y][0]<76) cout<<" ";
				if ((x==posx)&&(y==posy)) cout<<blue;
				else if (Board[x][y][0]==0) cout<<red;
				else
					cout<<normal;
					cout<<Board[x][y][0];
			}
		cout<<cyan<<"║\n";
	}
	 cout<<cyan<<"╚════╩════╩════╩════╩════╝\n"<<normal;
	cout<<"\n\n";
	for (y=0;y<5;y++)
	{
		for (x=0;x<5;x++)
		{
			cout<<Board[x][y][1]<<" ";
		}
		cout<<"\n";
	}
	return 0;
}
///////////////////////////////////////////////////////////////////////
int DetectBingo(int Board[5][15][2])
{
	int x,y,loseH,loseV,loseD1,loseD2;
	for (y=0;y<5;y++)
	{
		loseH=0;
		loseV=0;
		loseD1=0;
		loseD2=0;
		for (x=0;x<5;x++)
		{
			loseH=Board[x][y][0];
			loseV=Board[x][y][0];
			loseD1+=Board[x][y][0];
			loseD2+=Board[4-x][y][0];
		}
		if (!loseH) cout<<Board[x][0][0]<<Board[0][y][0]<<"BINGO!";
		if (!loseV) cout<<"Bingo!";
		if (!loseD1)cout<<"Bingo!";
		if (!loseD2)cout<<"Bingo!";
	}
	return 0;
}
///////////////////////////////////////////////////////////////////////
int main()
{
	int x=2,y=2,Board[5][15][2],input;
	MakeCard(Board);
	do
	{
		system("clear");
		Color(Board,x,y);
		system("sleep .01");
		input=toupper(getch());
		if (input=='D') x++;
		if (input=='A') x--;
		if (input=='S') y++;
		if (input=='W') y--;
		if (input==' ') 
		{
			if (Board[x][y][0]!=0)
			{
				Board[x][y][1]=Board[x][y][0];
				Board[x][y][0]=0;
			}
		}
	}while ((input!=27)||(DetectBingo(Board)));
	//Declare Variables
	//Input
	//Calculations
	//Output
	return 0;
}
Last edited on
Topic archived. No new replies allowed.