Code:
# 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;
}