Bingo game C++ C plus plus source code program
Developed By N. Vivek and Abhas. Financed by Moningi Vivek. Motivation came from Kanishka Anand.
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
class card
{
public:
int arr[5][5];
int move(int x);
int checkwin();
int cardcheck(int,int);
void generate();
};
class player:public card
{
public:
void input();
void display();
};
class computer:public card
{
int q[5];
public:
computer();
void display();
int play();
};
int card::cardcheck(int i,int j)
{
if(arr[i][j]<=0 || arr[i][j]>25)
return 0;
int k,l;
for(k=0;k<=i;k++)
{
for(l=0;l<5;l++)
{
if(k==i && l==j)
return 1;
if(arr[i][j]==arr[k][l])
{return 0;}
}
}
return 1;
}
void player::input()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
cin>>arr[i][j];
if(!card::cardcheck(i,j))
{
cout<<"Invalid Entry:Enter Again !!!";
j--;
}
}
}
}
void player::display(){
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(arr[i][j]!=0)
cout<<setw(3)<<arr[i][j];
else
cout<<" X";
}
cout<<"\n";
}
}
void computer::display(){
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(arr[i][j]!=0)
cout<<" .";
else
cout<<" X";
}
cout<<"\n";
}
}
void card::generate()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
arr[i][j]=rand()%25 +1;
if(!cardcheck(i,j))
{
j--;
}
}
}
/*for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
cout<<arr[i][j]<<" ";
*/
}
int computer::play()
{
int i,j,temp;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(i<3)
{
if(arr[q[i]][j]!=0)
{
temp=arr[q[i]][j];
arr[q[i]][j]=0;
return temp;
}
}
else
{
if(arr[j][q[i]]!=0)
{
temp=arr[j][q[i]];
arr[j][q[i]]=0;
return temp;
}
}
}
}
}
computer::computer()
{
srand(time(0));
for(int i=0;i<5;i++)
{
q[i]=rand()%5;
for(int j=0;j<i;j++)
{
if(q[i]==q[j])
{
i--;
break;
}
}
}
/*for(int i=0;i<5;i++)
cout<<q[i]<<endl; */
}
int card::move(int x)
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(arr[i][j]==x)
{
arr[i][j]=0;
return 1;
}
}
}
return 0;
}
int card::checkwin()
{
int c=0;
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(arr[i][j]==0)
continue;
else
break;
}
if(j==5)
c++;
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(arr[j][i]==0)
continue;
else
break;
}
if(j==5)
c++;
}
if(c==5)
return 1;
else
return 0;
}
int main()
{ int dh;
system("color 2F");
srand(time(0));
int x,ch;
player mycard;
computer compcard;
do
{
compcard.generate();
cout<<"\t\t##########################################"<<endl;
cout<<"\t\t# BINGO #"<<endl;
cout<<"\t\t# Developed by #"<<endl;
cout<<"\t\t# #"<<endl;
cout<<"\t\t# N. Vivek Abhas Tandon #"<<endl;
cout<<"\t\t# #"<<endl;
cout<<"\t\t# 3/11/2011 #"<<endl;
cout<<"\t\t##########################################"<<endl;
cout<<"\n\nEnter 1 for Single Player Game(vs Comp.)";
cout<<"\nEnter 2 for Multiplayer Player Game";
cout<<"\nEnter 3 to Exit\n";
cin>>ch;
if(ch==3)
break;
cout<<"Enter 1 if you want to enter numbers manually\nEnter 2 if you want random card\n";
cin>>dh;
if(ch==2)
{
if(dh==1)
{
cout<<"\n\nPlease Input Your Card\n";
mycard.input();
mycard.display();
}
if(dh==2)
{
mycard.generate();
mycard.display();
}
while(!mycard.checkwin())
{
cout<<"Enter Number To be Striken:";
cin>>x;
while(!mycard.move(x))
{
cout<<"Enter Valid Number To be Striken:";
cin>>x;
}
mycard.display();
}
cout<<"You Won\n";
getch();
}
else if(ch==1)
{
int chance=1,t;
if(dh==1)
{
cout<<"\n\nPlease Input Your Card\n";
mycard.input();
mycard.display();
}
if(dh==2)
{
mycard.generate();
mycard.display();
}
while(mycard.checkwin()==0 && compcard.checkwin()==0)
{
if(chance==1)
{
cout<<"Enter Number To be Striken:";
cin>>x;
while(!mycard.move(x))
{
cout<<"Enter Valid Number To be Striken:";
cin>>x;
}
compcard.move(x);
chance*=-1;
cout<<"Computer's Card\n";
compcard.display();
cout<<"\nYour Card\n";
mycard.display();
}
else
{
t=compcard.play();
cout<<"Computer Has Chosen to strike number "<<t<<endl;
mycard.move(t);
cout<<"Computer's Card\n";
compcard.display();
cout<<"\nYour Card\n";
mycard.display();
chance*=-1;
}
}
if(mycard.checkwin())
cout<<"YOU WIN !!!\n";
else
cout<<"COMPUTER WINS !!! BETTER LUCK NEXT TIME\n";
getch();
}
}while(ch!=3);
}
May I copy the codes?
ReplyDelete