[ create a new paste ] login | about

Link: http://codepad.org/LhDnIWvt    [ raw code | output | fork ]

C++, pasted on Sep 24:
//Bingo GAME : http://programmingpraxis.com/2009/02/19/bingo/
//rahul nanda
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
class Card
{
      int numbers[5][5];
      bool marking[5][5];
      public:
             Card()
             {
                   bool numbersAssigned[75];
                   for(int i=0;i<75;i++)
                   numbersAssigned[i]=false;
                   
                   for(int i=0;i<5;i++)
                   {
                           for(int j=0;j<5;j++)
                           {
                                   marking[i][j]=false;//initialize marking...
                                   int temp;
                                   do{
                                   temp=(rand()%15)+(i*15+1);
                                   }while(numbersAssigned[temp-1]==true);
                                   numbersAssigned[temp-1]=true;
                                   numbers[i][j]=temp;//initialize numbers on the card...
                           }        
                   }
             }
             
             void CheckAndMark(int calledNumber)
             {
                   for(int i=0;i<5;i++)
                   {
                           for(int j=0;j<5;j++)  
                           {
                                   if(numbers[i][j]==calledNumber)
                                   marking[i][j]=true;        
                           }      
                   }       
             }
             
             bool checkBingo()
             {
                  bool d=true;//for diagonal...
                  
                  for(int i=0;i<5;i++)
                  {
                          bool r=true;;
                          for(int j=0;j<5;j++)
                          {
                                      r=r&&marking[i][j];  
                                      
                                      d=d&&marking[j][j];
                          }
                          if(r) return true; //check Row i
                          if(d) return true; 
                          bool c=true;
                          for(int j=0;j<5;j++)
                          {
                                      c=c&&marking[j][i];    
                          }
                          if(c) return true; //check column i
                          
                                
                  } 
                  return false;    
             }
};

int generateUniqueCall(bool numbersCalled[])
{
    int calledNumber;
    do
    {
        calledNumber=(rand()%75)+1;    
    }while(numbersCalled[calledNumber-1]==true);
    numbersCalled[calledNumber-1]=true;
    
    return calledNumber;
        
}
int main()
{
     srand(time(0));
     int sum=0;
     int players;
     cout<<"Number of players=500\n";
     //cin>>players;
     players=500;
     cout<<"Games played=500 (over which average taken)\n";
     int games;
     //cin>>games;
     games=500;
     
     for(int count=0;count<games;count++){
     int winner;
     bool numbersCalled[75];
     for(int i=0;i<75;i++)
     numbersCalled[i]=false; //initializing numbersCalled...
     
     Card *obj=new Card[players];  
     int calls=0; //calls refers to the number of calls made
     bool gameOver=false;
     do //game begins !
     {
          int calledNumber=generateUniqueCall(numbersCalled);
          for(int i=0;i<players;i++)
          {
                  obj[i].CheckAndMark(calledNumber);
                  if(obj[i].checkBingo())
                  {
                          gameOver=true;
                          winner=i+1;
                          break;
                  }
          }
          calls++;
     }while(gameOver==false);
     
     //system("cls");
     //cout<<"\ngame "<<count+1<<" won by player "<<winner;
     sum+=calls;
     //cout<<"Number of calls made : "<<calls;
     }
     cout<<"\nAverage Number of calls made : "<<sum/games;
     
}


Output:
1
2
3
4
Number of players=500
Games played=500 (over which average taken)

Average Number of calls made : 15


Create a new paste based on this one


Comments: