Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Can Anyone Explain Me Some Functions Of A Program Code (http://www.go4expert.com/forums/explain-functions-program-code-t832/)

hiteshlohani 24May2006 23:03

Can Anyone Explain Me Some Functions Of A Program Code
 
A HAV A PROGRAM CODE OF TIC-TAC-TOE, AND I HAV IMPLEMENTED IT USING CLASS AND OBJECTS IN C++
BUT I AM NOT GETTING SOME FUNCTIONS, CAN ANY PROGRAMMING GENIUS EXPLAIN ME THESE FUNCTIONS i.e findmove(),immediatewin(),findwinner
the program is as follows:
Code: CPP

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<dos.h>
typedef enum { NONE,COMPUTER, HUMAN } PlayerType;
enum ValueType { CompLoss = -1, Draw, CompWin };

/*----------------------------------------------------------------------------
WINNING BOARD PATTERN
---------------------------------------------------------------------------*/

const static int p[8][3] = {
{0,1,2},
{3,4,5},
{6,7,8},
{0,3,6},
{1,4,7},
{2,5,8},
{0,4,8},
{2,4,6}
};
class display
{
public:
char b[];

void InitializeBoard(char b[]);
void MakeMove(char b[],int pos, PlayerType p) ;
void disp_matrix(void) ;
void UndoMove(char b[], int pos) ;
void PrintBoard(char b[]) ;
// void InitDisplay();
PlayerType FindWinner(char b[]);
int FullBoard(char b[]);
void play_next(void);

// void GetPlayerMove ();

int ImmediateWin(char b[], PlayerType pl,int *BestMove);
};
void display::PrintBoard(char b[])
{
gotoxy(28,12);
cout<<b[0];
gotoxy(32,12);
cout<<b[1];
gotoxy(36,12);
cout<<b[2];
gotoxy(28,14);
cout<<b[3];
gotoxy(32,14);
cout<<b[4];
gotoxy(36,14);
cout<<b[5];
gotoxy(28,16);
cout<<b[6];
gotoxy(32,16);
cout<<b[7];
gotoxy(36,16);
cout<<b[8];
}

int display::ImmediateWin(char b[], PlayerType pl,int *BestMove)
{
int i;
PlayerType winner;

for (i = 0; i < 9; i++)
{
if(b[i] == ' ')
{
MakeMove(b,i,pl);
winner = FindWinner(b);
if((winner == COMPUTER && pl == COMPUTER) || (winner == HUMAN && pl == HUMAN))
{
*BestMove = i;
UndoMove(b,i);
return 1;
}
UndoMove(b,i);
}
}

/*for (i = 0; i < 9; i++)
if (b[i] == ' ')
*BestMove = i; */

return 0;

}




/*----------------------------------------------------------------------------
CHECK IF THE BOARD IS FULL
----------------------------------------------------------------------------*/


int display::FullBoard(char b[])
{
int i;
for (i = 0; i < 9; i++)
if(b[i] == ' ')
return 0;
return 1;
}

/*----------------------------------------------------------------------------
FIND THE WINNER
----------------------------------------------------------------------------*/


PlayerType display::FindWinner(char b[])
{
int i;

for (i = 0; i < 8; i++)
if(b[p[i][0]] != ' ' && b[p[i][0]] == b[p[i][1]] && b[p[i][0]] == b[p[i][2]])
return ((b[p[i][0]] == 'X')?COMPUTER:HUMAN);
return NONE;
}
void display::InitializeBoard(char b[])
{
int i;
for (i = 0; i < 9; i++)
b[i] = ' ';
}

void display::MakeMove(char b[],int pos, PlayerType p)
{
if(p == COMPUTER)
b[pos] = 'X';
else
b[pos] = '0';
}


void display::UndoMove(char b[], int pos)
{
b[pos] = ' ';
}


class playerublic display
{
public: int m;
int GetPlayerMove(char b[]);
};
int player :: GetPlayerMove(char b[])
{

while(1)
{
gotoxy(20,22);
cout<<"Enter your move: ";
gotoxy(62,2);
cout<<"MOVE=0 to exit ";
gotoxy(38,22);
cin>>m;
if (m < 0 || m > 9)
cout<<"Illegal move";
if(m==0)
exit(0);
else if (b[m-1] != ' ')
cout<<"The room is filled";
else
break;
}
return (m-1);
}


class computerublic display

{
public:
void FindMove(char b[],PlayerType p,int *move,int *v);
};
void computer::FindMove(char b[],PlayerType p,int *move,int *v)
{
int opv;
int i;
int Dc;

if (FullBoard(b))
{
*v = Draw;
}
else if (ImmediateWin(b,p,move))
{
if(p == COMPUTER)
*v = CompWin;
else
*v = CompLoss;
}
else
{
if(p==COMPUTER)
*v = CompLoss;
else
*v = CompWin;

for (i = 0; i < 9; i++)
{
if (b[i] == ' ')
{
MakeMove(b,i,p);
FindMove(b,((p==COMPUTER)?HUMAN:COMPUTER),&Dc,&opv );
UndoMove(b,i);
if ((p == COMPUTER) && (opv > *v))
{
*move = i;
*v = opv;
}
else if ((p == HUMAN) && (opv < *v))
{
*move = i;
*v = opv;
}
}
}
}
}
void main()
{
display d;
player p;
computer c;


int s=DETECT,n;
initgraph(&s,&n,"c:\\borlandc\\bgi");
/*----------------------------------------------------------------------------
DEFINE THE WELCOME PAGE
----------------------------------------------------------------------------*/


start:
char Board[9];
int move,NumMoves;
int value;
PlayerType CurPlayer, winner;
c.InitializeBoard(Board);
NumMoves =0;
setcolor(LIGHTBLUE);
setlinestyle(0,1,3);
line(235,170,235,250); /*virticle line*/
line(265,170,265,250); /*virticle line*/
line(200,200,300,200); /*horizental line*/
line(200,230,300,230); /*horizental line*/
{
do
{
CurPlayer = HUMAN;

move=p.GetPlayerMove(Board);



c.MakeMove(Board,move,CurPlayer);


c.PrintBoard(Board);
NumMoves++;
if((winner = d.FindWinner(Board)) != NONE)
break;
if (NumMoves >= 9)
break;
CurPlayer = COMPUTER;
c.FindMove(Board,CurPlayer,&move,&value);
c.MakeMove(Board,move, CurPlayer);
NumMoves++;

c.PrintBoard(Board);

if((winner = d.FindWinner(Board)) != NONE)
break;
} while (NumMoves < 9 && winner == NONE);
if (winner == COMPUTER)
{
gotoxy(4,11);
cout<<"You Loose the Game";
setlinestyle(0,1,1); /*for sad face*/
circle(100,100,50);
circle(70,85,5);
circle(130,85,5);
ellipse(100,130,0,180,10,5);

}
else if (winner == HUMAN)
{
gotoxy(6,11);
cout<<"You won the game";
outtextxy(200,400,"You are Amaizaing");

}
else
{
setcolor(RED);
setlinestyle(0,1,1);
gotoxy(12,11);
cout<<"Draw";
circle(100,100,50); /*for happy face*/
circle(70,85,5);
circle(130,85,5);
ellipse(100,130,180,0,10,5);
settextstyle(4,0,3);
outtextxy(150,300,"Better Luck Next Time!");
getch();

} } //exit(0);

char play;
play='y';
do
{
gotoxy(20,22);
//sleep(1);
cout<<"you want to play again(y/n):\a ";
cin>>play;
switch(play)
{
case 'y':
case 'Y':
goto start;
case 'n':
case 'N':
setcolor(9);
outtextxy(140,430,"Bye! Bye! Have a Nice Day!");
//sleep(2);
exit(0);
break;
default:
cout<<"ENTER 'Y' OR 'N'";
}
}while (play == 'y' || play == 'Y');


getch();
}


shabbir 25May2006 14:58

Re: Can Anyone Explain Me Some Functions Of A Program Code
 
Please read the Before you make a query thread.


All times are GMT +5.5. The time now is 07:10.