Jump to content
Science Forums

Can anyone help me explaining a program code


Recommended Posts

First of all a note of thanks to Alexander.

LUCKILY I HAV FOUND A PROGRAM CODE OF TIC-TAC-TOE, AND I HAV ALSO IMPLEMENTED IT USING CLASS AND OBJECTS IN C++

BUT I AM NOT GETTING SOME FUNCTIONS, CAN ANYONE EXPLAIN ME THESE FUNCTIONS i.e findmove(),immediatewin(),findwinner

the program is as follows:

#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 == ' ')

{

MakeMove(b,i,pl);

winner = FindWinner(:hihi:;

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 == ' ')

*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 == ' ')

return 0;

return 1;

}

 

/*----------------------------------------------------------------------------

FIND THE WINNER

----------------------------------------------------------------------------*/

 

PlayerType display::FindWinner(char b[])

{

int i;

 

for (i = 0; i < 8; i++)

if(b[p[0]] != ' ' && b[p[0]] == b[p[1]] && b[p[0]] == b[p[2]])

return ((b[p[0]] == 'X')?COMPUTER:HUMAN);

return NONE;

}

void display::InitializeBoard(char b[])

{

int i;

for (i = 0; i < 9; i++)

b = ' ';

}

 

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 player:public 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 computer:public 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(:hihi:)

{

*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 == ' ')

{

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:borlandcbgi");

/*----------------------------------------------------------------------------

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();

}

Link to comment
Share on other sites

...CAN ANYONE EXPLAIN ME THESE FUNCTIONS i.e findmove(),immediatewin(),findwinner()...

 

 

findwinner()

This routine is called to determine if there is or is not a three-in-a-row pattern of identical values on the board. Returns either the values COMPUTER, HUMAN, NONE. Does this by checking each winning pattern in p[] against the board b[]. The first winning pattern (three-in-a-row) on the board that contains identical non-null values (either X or O) is declared a win. If the value is X, returns COMPUTER, if the value is O, returns HUMAN. If no winning pattern is found, returns NONE.

 

immediatewin()

This routine is called on the behalf of either COMPUTER or HUMAN to search for a winning move. This routine does not actually MAKE any permanent move. (It makes every possible move, but undoes them all.) It is called by findmove() to search for immediate wins for EITHER the HUMAN or the COMPUTER. An "immediate win" would be a three-in-a-row like XX- or X-X or -XX or OO- or O-O or -OO, where "-" is an unplayed position. Routine does this be checking each position of the board b[] looking for a null (unplayed position). When it finds one, it "makes a move" in that position, then calls findwinner() to see if playing an X or O actually creates a three-in-a-row win. If a winning move is found, the board position for that win is returned in BestMove and the actual "move" made by this routine is undone; routine returns 1. However, if there is NO move that wins for either side, then routine returns 0.

 

findmove()

Calls fullboard(); if return is non-null then game is declared a "draw" (tie) and is over.

Else, calls immediatewin(); if there is an immediately winning move for either COMPUTER or HUMAN, then that move is returned. If its the COMPUTER's play, that move is made.

If no immediate win, then here is where it gets very tricky. This routine is "recursive"; it calls itself. No wonder you were confused. :hihi: The routine basically looks for immediate-wins for the HUMAN, and if the blocking position is open (null), then the COMPUTER plays there. If there are no plays for the HUMAN that are immediate wins, then it searches for the first position that gives an immediate win for the COMPUTER. Another way of looking at it is this: This routine looks for an immediate win for itself. If there isn't one, then it looks for immediate wins for the HUMAN and returns a blocking move. If there isn't that, then it plays in every empty position and asks if THAT creates a winning (two-in-a-row with empty) position for HUMAN or COMPUTER. By calling itself, findmove can start from every empty position, and look one move ahead to the next empty position to be played after that.

Link to comment
Share on other sites

excellent, pyro beat me to it, but it's nice to see someone else explain the code other then myself, thumbs up Pyro :)

 

oh and hit, next time when you post code, use the [noparse]

[/noparse] tags, that makes code a bit more readable, and throws it into a scrollable window rather then having it take like 5 minutes to scroll through the post, something like this:
#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(:eek:;
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(:))
{
*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:borlandcbgi");
/*----------------------------------------------------------------------------
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();

be nice to your readers :)

 

no sweat, and pyro

nice job
/forums/images/smilies/banana_sign.gif
Link to comment
Share on other sites

Thanx a lot Pyro, I hav understood the two functions findwinner() & immediatewin() but in findmove();as its tricky can u explain to me

which line of code is actually selecting the empty position for the computer’s first move, after the first move of the player .And also at what condition does it escape the recursive function call and also what are these assignments for:

if ((p == COMPUTER) && (opv > *v))

{

*move = i;

*v = opv;

}

else if ((p == HUMAN) && (opv < *v))

{

*move = i;

*v = opv;

}

Link to comment
Share on other sites

excellent, pyro beat me to it, but it's nice to see someone else explain the code other then myself, thumbs up Pyro :beer:...

You are very welcome and thanks for the kudo. I had an hour with nothing to do, so it was like working a crossword puzzle. BTW, what language is that code written in? :P And can you answer hiteshlohani's 2nd question?

Link to comment
Share on other sites

it looks like either C++ or Java, and i bet C++, but it uses crazy libraries to do the job it does...

 

as to the questions, i got some time, maybe i'll try and explain them to hit :D

 

ok, the recursive function will exit when b is not equal to nothing whatever b is. whoever wrote this code needs to get a good wack in their head, cuz using variables with meaningfull names is not only a good, it is a required practice by REAL programmers. also adding a comment or two may perhaps make the code readable and understandable... leme try to fill in a few blanks:

//includes
#include<iostream.h> //input output streams
#include<conio.h> //http://brickos.sourceforge.net/docs/APIs/html-c/conio_8h.html
#include<stdlib.h> //standard library
#include<graphics.h> //graphics
#include<dos.h>  //http://www.digitalmars.com/rtl/dos.html

typedef enum { NONE,COMPUTER, HUMAN } PlayerType; //an emumerated array for PlayerType

enum ValueType { CompLoss = -1, Draw, CompWin }; //another enumerated array for ValueType 

/*----------------------------------------------------------------------------
WINNING BOARD PATTERN
---------------------------------------------------------------------------*/
//this declares every possible winning position on the field
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}
                                  };

//display class
class display
{
   public:
   char b[]; //b is declared as an array (and should be named something else)

   //prototypes for the classes methods
   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);
};

//method Print Board takes an array as input
void display::PrintBoard(char b[])
{
   //goes to these coordinates on the screen and prints the values of the corresponding b values in the fields in the tic-tac-toe matrix
   gotoxy(28,12); //1:1
   cout<<b[0];
   gotoxy(32,12); //1:2
   cout<<b[1];
   gotoxy(36,12); //1:3
   cout<<b[2];
   gotoxy(28,14); //2:1
   cout<<b[3];
   gotoxy(32,14); //2:2
   cout<<b[4];
   gotoxy(36,14); //2:3
   cout<<b[5];
   gotoxy(28,16); //3:1
   cout<<b[6];
   gotoxy(32,16); //3:2
   cout<<b[7];
   gotoxy(36,16); //3:3
   cout<<b[8];
}
//Stopped here, lunch is over, gotta work for a little before i can finish... :)
int display::ImmediateWin(char b[], PlayerType pl,int *BestMove)
{
   int i; // iterator
   PlayerType winner; //declares the player type to winner

   for (i = 0; i < 9; i++)
   {
       if(b[i] == ' ') // this looks for an empty slot
       {
           MakeMove(b,i,pl); //makes a move in the b array at i for the player pl
           winner = FindWinner(;); //looks for winner
           //if the move is a winning move for either computer or player
           if((winner == COMPUTER && pl == COMPUTER) || (winner == HUMAN && pl == HUMAN))
           {
               *BestMove = i; //declares the BestMove pointer to be equal to the value of i
               UndoMove(b,i); //then it undoes the move
               return 1; //and returns positive
           }
           UndoMove(b,i); // This undoes the made move
   }
}

/*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] == ' ') //if it finds an empty space, it exists
return 0;
return 1;
}

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

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

   //horrible, the use of brackets can make this so much more legible...
   for (i = 0; i < 8; i++) loops 9 times
   if(b[p[i][0]] != ' ' && b[p[i][0]] == b[p[i][1]] && b[p[i][0]] == b[p[i][2]]) //checks for winning combinations
   return ((b[p[i][0]] == 'X')?COMPUTER:HUMAN); //another example of bad coding practices... this says if the position of b at p[i][0] is X then the computer won, if not then human

   //if the criteria above were not met, then nobody won yet :)
   return NONE;
}

void display::InitializeBoard(char b[])
{
   //just goes through and makes sure that the board is clean for the game, setting all 9 fields of b to nothing
   int i;
   for (i = 0; i < 9; i++)
   b[i] = ' ';
}

void display::MakeMove(char b[],int pos, PlayerType p)
{
   if(p == COMPUTER) //puts an X for computer or 0 for player at pos
   b[pos] = 'X';
   else
   b[pos] = '0';
}


void display::UndoMove(char b[], int pos)
{
   b[pos] = ' '; //just sets the position of b to nothing
}

//display class

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

int player :: GetPlayerMove(char b[]) //function
{

   while(1) // loop forever (usually good for user input)
   {
       gotoxy(20,22); //sets the location to row 20, line 22
       cout<<"Enter your move: ";
       gotoxy(62,2);
       cout<<"MOVE=0 to exit ";
       gotoxy(38,22);
       cin>>m; //gets input
       if (m < 0 || m > 9) //checks for illegal move
       cout<<"Illegal move";
       if(m==0) // checks for exit (and should really check for exit before checking for illegal move...
       exit(0);
       else if (b[m-1] != ' ') //checks if the space is already filled
       cout<<"The room is filled";
       else
       break; //if no condition for error is satisfied the loop will exit
   }
   return (m-1); //returns position-1 = position in the b array
}


class computerublic display

{
   public:
            void FindMove(char b[],PlayerType p,int *move,int *v); //function implementation
};

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

   if (FullBoard(;))
   {
       *v = Draw; //if the board is full, then its a draw
   }
   else if (ImmediateWin(b,p,move)) //if there is an win
   {
       if(p == COMPUTER) //if the computer won, then v pointer is set to computer win
           *v = CompWin;
       else //obviously if the winner isnt the computer and its not a draw, but there is a win, then it must be the user, hence computer looses... lucky bastard
           *v = CompLoss;
   }
   else
   {
       // this is weird as it checks for computer win again, but i guess there could be a win already... just in case, but values are reversed... weird...
       if(p==COMPUTER)
           *v = CompLoss;
       else
           *v = CompWin;

       for (i = 0; i < 9; i++) //loop through positions
      {
           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:borlandcbgi");
/*----------------------------------------------------------------------------
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();

Link to comment
Share on other sites

:)

 

well, i didnt write the code... i could, and it probably would be better then this code :eek_big:...

anyhow, i gotta run an errand, i'll be back later to finish my documentation project, if anyone wants to finish the commenting for me, copy the part of code that is not yet commented and finish it, i'll paste it with mine after i come back in an hour or so

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...