Thursday 26 January 2012

GOOGLE NIM GAME





                                                             GOOGLE NIM GAME

THEY HAVE ASKED TO MAKE A NIM GAME AND PRESENT IT IN A GOOD WAY SO IN THIS QUES PRESENTATION ALSO HAS ITS OWN MARKS SO I HAVE WRITTEN THE SIMPLE CODE FOR THIS...

#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<iomanip.h>
#include<dos.h>
void selection();
void display();
void moving();
void ending();
void rules();
int *a;
char x='A';
char p1[20] , p2[20];
int turn = 0;
void main()
{
clrscr();
gotoxy(24,10);
cout<<"WELCOME TO THE NIM GAME MADE BY SAURABH "<<endl;
gotoxy(24,11);
cout<<"________________________________________"<<endl;
cin.get();
clrscr();
rules();
selection();

getch();
}
void rules()
{
cout<<" THIS GAME WILL HAVE 2 PLAYERS "<<endl;
cout<<" " <<endl;
cout<<" FIRST OF ALL PLZ ON YOUR CAPSLOCK KEY "   <<endl;
cout<<" "<<endl;
cout<<" IN THIS YOU HAVE TO TAKE OBJECTS FROM THE HEAP (A , B , C , D) TURN WISE "<<endl;
cout<<" "<<endl;
cout<<" THE PLAYER WHO TAKE OUT THE LAST OBJECT WILL BE THE WINNER OF THE GAME "<<endl;
cout<<" " <<endl;
cout<<" FOR THIS YOU HAVE TO FIRST INPUT THE NAME OF HEAP AND THAN NUMBER OF "<<endl;
cout<<"  ELEMENTS YOU WANT TO TAKE FROM THAT HEAP "<<endl;
cout<<" " << endl;
cout<<" ONE HEAP CAN TAKE MAXIMUM OF 9 OBJECTS SO INPUT THE NUMBER OF OBJECTS CAREFULLY"<<endl;
cout<<" BEFORE STARTING THE GAME "<<endl;
cout<< " "<<endl;
cout<<" YOU CAN PRESS 'E' FOR EXIT AT ANY TIME BETWEEN THE GAME " <<endl;
cout<<" "<<endl;
cout<<" PRESS ENTER TO PROCEED FURTHER "<<endl;
cin.get();
clrscr();
}
void selection()
{
int obj;
char y;
cout<<"ENTER THE NAME OF PLAYER 1 "<<endl;
cin.get(p1,15);
cout<<"ENTER THE NAME OF PLAYER 2 "<<endl;
cin>>p2;

a = new int[3];
cout<<"MAXIMUM NUMBER OF OBJECTS A HEAP CAN HAVE IS 9 "<<endl;
cout<<" "<<endl;
for(int i=0;i<3;i++)
{
 y = x+i;
cout<<"ENTER THE OBJECTS IN HEAP "<<y<<endl;
cin>>obj;
a[i] = obj;
}
cin.get();
clrscr();
gotoxy(24,10);
cout<<p1<<" WILL START THE GAME "<<endl;
cout<<""<<endl;
gotoxy(24,11);
cout<<"BEST OF LUCK"<<endl;
cout<<""<<endl;
gotoxy(24,12);
cout<<"PRESS ENTER TO START THE GAME "<<endl;
cin.get();
clrscr();
display();
}
void display()
{

cout<<"SIZES OF HEAP"<<setw(30)<<"MOVES" <<endl;
cout<<" "<<endl;
cout<<"A"<<setw(5)<<"B"<<setw(5)<<"C";
cout<<endl;
cout<<a[0]<<setw(5)<<a[1]<<setw(5)<<a[2];
moving();
}
void moving()
{
int elements;
char ch;
char c = getch();
switch(c)
{
case 'A':
ch = 'A';
if(a[0] == 0)
{
moving();
}
else
{
elements = getch();
elements = elements - 48;
a[0] = a[0] - elements;
}
break;
case 'B':
ch = 'B';
if(a[1] == 0)
{
moving();
}
else
{
elements = getch();
elements = elements - 48;
a[1] = a[1] - elements;
}
break;
case 'C':
ch = 'C';
if(a[2] == 0)
{
moving();
}
else
{
elements = getch();
elements = elements - 48;
a[2] = a[2] - elements;
}
break;
case 'E':
exit(0);
break;
default:
moving();
}
turn++;
if(turn % 2 == 0)
{
cout<<setw(21)<<p2<<" HAS MOVED "<<elements<<" FROM " << ch <<endl;
}
else
{
cout<<setw(21)<<p1<<" HAS MOVED "<<elements<<" FROM "<<  ch<<endl;
}
cout<<a[0]<<setw(5)<<a[1]<<setw(5)<<a[2];
if((a[0] ==  0) && (a[1] == 0) && (a[2] == 0) )
{
sleep(1);
ending();
}
else
{
moving();
}
}
void ending()
{
clrscr();
if( turn % 2 ==0)
{
gotoxy(24,10);
cout<<p2<< " HAS WON THE GAME "<<endl;
}
else
{
gotoxy(24,10);
cout<<p1<<" HAS WON THE GAME "<<endl;
}
gotoxy(24,11);
cout<<"THANKS FOR PLAYING NIM GAME "<<endl;
gotoxy(24,12);
cout<<"REGARDS : SAURABH BHATIA CSE (2010 - 2014) "<< endl;
}


BY:  SAURABH  BHATIA (GNDU , RC JAL)

Wednesday 25 January 2012

FACEBOOK ENGINEERING PUZZLE QUESTION WITH ITS SOLUTION

QUES:
YOU ARE GIVEN A STRING AND A VECTOR OF STRING YOU HAVE TO FIND THE POSITION IN A STRING FROM WHICH ALL THE ELEMENTS OF VECTOR STRING IS THERE..
FOR EG:
"srfhdbeabhjyeabcdkilop"

{"a" , "b" , "c" , "d" , "e" }

RETURN : 12
1
AS IN STRING THE STARTING POSITION OF THE ELEMENTS OF VECTOR IS 12


SOLUTION:


#include<iostream>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<list>
#include<iterator>
#include<string>
#define vs vector<string>
#define vi vector<int>
#define pb push_back
#include<map>
#include<algorithm>
#include<sstream>
#include<stack>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<cctype>
using namespace std;
class facebook
{
      public:
             int pos(vs a , string b)
             {
                 int position = 0 , position1 = 0 , count = 0 ;
                 for(int i= 0 ; i <a.size() ; i++)
                 {
                         position = 0;
                         while(position != string::npos)
                         {
                          position = b.find(a[i],position+1);
                          count = 1;
                       
                          if(position != string::npos)
                          for(int j = 0 ; j < a.size() ; j++)
                          {
                                  if( i != j)
                                  {
                                      position1 = b.find(a[j],position+1);
                                   
                                      if(position1 != string::npos && (position1 <(position+a.size())))
                                      {
                                                   count++;
                                      }
                                  }
                          }
                       
                          if(count == a.size())
                          return position;
                          }
                       
                 }
                 return 0;
             }
};
int main()
{
    facebook p;
    string s = "pokoblpmanksecbda";
    vs a;
    a.pb("a");
    a.pb("b");
    a.pb("c");
    a.pb("d");
    a.pb("e");
 
    int c = p.pos(a,s);                                        
    cout<<c<<endl;
    cin.get();
    return 0;
}                                                                          
                               
                     BY: SAURABH BHATIA (GNDU RC , JAL )