Saturday, October 8, 2011

Demo of Slot Machine

Program Of Making Slot Machine Graphics:-

#include<graphics.h>
#include<stdlib.h>
 #include<time.h>
 #include<conio.h>
 #include<dos.h>
 const int W=15;
 const int MAR=10;
 class shape
 {
 protected:
 int xCo,yCo;
 int linecolor;
 int fillcolor;
 public:
 shape()
 {
 xCo=0;
 yCo=0;
 linecolor=WHITE;
 fillcolor=WHITE;
 }
 void set(int x,int y,int lc,int fc)
 {
 xCo=x;
 yCo=y;
 linecolor=lc;
 fillcolor=fc;
 }
 void draw()
 {
 setcolor(linecolor);
 setfillstyle(SOLID_FILL,fillcolor);
 }
 };
 class ball :virtual public shape
 {
 public:
 ball():shape()
 {}
 void set(int x,int y,int lc,int fc)
 {
 shape::set(x,y,lc,fc);}
 void draw()
 {
 shape::draw();
 circle(xCo,yCo,W);
 floodfill(xCo,yCo,linecolor);
 }
 };
 class rect:virtual public shape
 {
 public:
 rect():shape()
 {}
 void set(int x,int y,int lc,int fc)
 {
 shape::set(x,y,lc,fc);
 }
 void draw()
 {
 shape::draw();
 rectangle(xCo-W,yCo-W,xCo+W,yCo+W);
 floodfill(xCo,yCo,linecolor);
 moveto(xCo-W,yCo+W);
 lineto(xCo+W,yCo-W);
 }
 };
 class tria:virtual public shape
 {// int triarray[];
 public:
 tria()
 {
 shape();
  /*triarray[0] = xCo;
 triarray[1] = yCo-W;
 triarray[2] = xCo+W;
 triarray[3] = yCo+W;
 triarray[4] = xCo-W;
 triarray[5] = yCo+W;
*/ }
 void set(int x,int y,int fc,int lc)
 {
 shape::set(x,y,lc,fc);
 }
 void draw()
 {
 int triarray[] = { xCo, yCo-W, xCo+W, yCo+W, xCo-W, yCo+W};
 shape::draw();
 fillpoly(3,triarray);
 }
 };

 class noshape:virtual public shape
 {//int border[];
 public:
 noshape()
 {/*border[0] = xCo-W-MAR;
 border[1] = yCo-W-MAR;
 border[2]= xCo+W+MAR;
 border[3] = yCo-W-MAR;
 border[4] = xCo+W+MAR;
 border[5] = yCo+W+MAR;
 border[6] = xCo-W-MAR;
 border[7] = yCo+W+MAR;
 */}
 void erase()
 { int border[] = {xCo-W-MAR, yCo-W-MAR, xCo+W+MAR, yCo-W-MAR, xCo+W+MAR, yCo+W+MAR, xCo-W-MAR, yCo+W+MAR};
 setfillstyle(SOLID_FILL,DARKGRAY);
 fillpoly(4,border);
 }
 };
 class cherry:public noshape,public ball
 {
 public:
 cherry():ball()
 {}
 void set(int x,int y)
 {
 ball::set(x,y,WHITE,RED);
 noshape::set(x,y,WHITE,RED);
 }
 void draw()
 {

 erase();
 ball::draw();}
 };
 class Grape:public ball,public noshape
 {
 public:
 Grape():ball()
 {}
 void set(int x,int y)
 {
 ball::set(x,y,WHITE,BLUE);
 noshape::set(x,y,WHITE,BLUE);
 }
 void draw()
 {
 erase();ball::draw();}
 };
 class Square : public rect,public noshape
 {
 public:
 Square():rect()
 {}
 void set(int x,int y)
 {
 rect::set(x,y,WHITE,CYAN);
 noshape::set(x,y,WHITE,CYAN);
 }
 void draw()
 {erase();rect::draw();}
 };
 class Pyramid:public tria,public noshape
 {
 public:
 Pyramid():tria()
 {}
 void set(int x,int y)
 {
 tria::set(x,y,WHITE,GREEN);
 noshape::set(x,y,WHITE,GREEN);
 }
 void draw()
 {
 erase();tria::draw();}
 };
 class Wheel:  virtual public shape, public Pyramid, public Grape, public cherry, public Square
 {
 private:
 cherry ch;
 Grape gr;
 Square sq;
 Pyramid py;
 public:
 Wheel()
 {
 xCo=0;
 yCo=0;
 }
 void set(int x,int y)
 {
 shape::xCo=x;shape::yCo=y;
 ch.set(xCo,yCo);
 gr.set(xCo,yCo);
 sq.set(xCo,yCo);
 py.set(xCo,yCo);
 }
 void draw();
 };
 void Wheel::draw()
 {
 setcolor(WHITE);
 rectangle(xCo-W-MAR,yCo-W-MAR,xCo+W+MAR,yCo+W+MAR);
 switch(random(4))
 {
 case 0:ch.draw();break;
 case 1:gr.draw();break;
 case 2:sq.draw();break;
 case 3:py.draw();break;
 }
 }
 int main(void)
 {
 const int NUMBER=60;
 int driver,mode;
 driver=DETECT;
 initgraph(&driver,&mode,"c:\\TC\\BGI");
 randomize();
 Wheel w1;
 Wheel w2;
 Wheel w3;
 w1.set(100,100);
 w2.set(160,100);
 w3.set(220,100);
 for(int j=0;j<NUMBER;j++)
 {
w1.draw();
w2.draw();
w3.draw();
sound(100);delay(20);nosound();
delay(j*j/20);
}
sound(400);delay(400);
sound(500);delay(800);nosound();
getch();
closegraph();
return 0;
}

Note:- Run this program in Turbo C.

Experience While Doing This Program. 

Its was a very nice experience. I came to know about many graphics functions and use of classes and contructors.But it took lots of timebecause was using graphics in C++ for the first time. So I did n't know how to run the program of C++ including Graphic. By surfing on Net , I came to know about BGI folder contained in Turbo C. This Folder(BGI) contains all the graphics functions. So its path should be rightly given in Main() function.
 initgraph(&driver, &mode, "c:\\TC\\BGI") 
In this, Path is  "c:\\TC\\BGI" 

Second Problem That I faced is "Functions containing aggregate initializers are not expanded inline." It means that I used array in a member function of a class, then I put the values of each element of array altogether included in curly braces. The compiler gave a warning:
Functions containing aggregate initializers are not expanded inline
To remove this warning, I gave values to each element of array separately. Then It again gave me run time error, that was :- at run time, it was showing improper graphics. Triangle were not made proper. So again I switch to aggregate values. Compile it, its shows warning. Even then I ran the program using Alt+R then press enter. It started working.......:-)
((But still a confusion is there, i.e. how to remove that warning then. ))
Its yet to be discovered.....
Overall experience was very nice.


No comments:

Post a Comment