SlideShow

  • Dharma Teja Reddy

    First Well Lightened & Rendered Image in MAYA 2012
  • An Post in Google+ on Feb-14,2014

    This is a Post for Creative Dude in Google+
  • WallPaper-3

    From http://imgs.abduzeedo.com/
  • Wall Paper 4

    From http://imgs.abduzeedo.com/
  • WallPaper 5

    From http://imgs.abduzeedo.com/

Tuesday, 9 October 2012

Create A Simple Game Using Classes in C++

Hi......
Here I am giving a simple code for a simple game.........

GAME DESCRIPTION

     This simple game will be played by two persons at a time. I actually don't know the game's name but I saw more children playing this game for fun.
It starts in work area having 9 blocks as shown in figure.
Work Area
Two players, say Player-1 and Player-2, will select a notation symbol.
Let Player-1 selects notation "1" and other selects "2". Starting from any one of players, player can fill any one of unfilled Block with his/her notation. Later other player has to do the same. Like wise,alternately, each player fills each empty block at a time with his notation. Game will comes to an end depending on two conditions
i. When a player fills Diagonal or Horizontal or Vertical blocks with      his/her notation that player is declared as winner.
ii. If both players unsuccessful, then game is said to be DRAW.



                                                     
Player-1 wins as He/She filled his/her
notation Diagnally
Player-2 wins as He/She filled his/her
notation Horizontally
No one wins
The Following is C++ Code for this game implementation

CODE:

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
char a[10]="---------";
int pos;
void show();
class game
{
 char name[20];
 char code;
 int ip;
 public:
game();
void read();
void check();
void declare();
};
game::game()
{
 cout<<"Enter Players Name : ";
 cin>>name;
 cout<<"Hi "<<name<<", Enter CHARACTER code : ";
 cin>>code;
 ip=0;
}
void game::read()
{
 show();
 cout<<"\nEnter Position of Block to fill with your character : ";
 cin>>pos;
 if(a[pos-1]!='-' || pos>9 || pos<1)
{
 cout<<"Entered Wrong position. Correct It\n";
 read();
}
 else
{
 a[pos-1]=code;
 ip++;
 if(ip>=3)
check();
}
}
void game::declare()
{
 show();
 cout<<"Congrats "<<name<<". You WIN";
 exit(0);
}

void game::check()
{
 if(a[0]==code && a[1]==code && a[2]==code)
declare();
 if(a[3]==code && a[4]==code && a[5]==code)
declare();
 if(a[6]==code && a[7]==code && a[8]==code)
declare();
 if(a[1]==code && a[4]==code && a[7]==code)
declare();
 if(a[2]==code && a[5]==code && a[8]==code)
declare();
 if(a[0]==code && a[4]==code && a[8]==code)
declare();
 if(a[2]==code && a[4]==code && a[6]==code)
declare();
 if(a[0]==code && a[3]==code && a[6]==code)
declare();
}
void main()
{
 game p[2];
 cout<<"Player-1 Starts's \n";

 p[0].read();

 p[1].read();

 p[0].read();

 p[1].read();

 p[0].read();

 p[1].read();

 p[0].read();

 p[1].read();
 show();
 cout<<"\nMatch Draw\n";
}

void show()
{
 clrscr();
 int e=0;
 for(int i=0;i<9;i++)
 {
e++;
cout<<"  "<<a[i]<<"  ";
if(e==3)
 {
e=0;
cout<<endl;
 }
}
 }


No comments:

Post a Comment