Go Back   Science Forums > Physical Sciences Forums > Computer Science and Technology
Reply
 
LinkBack Thread Tools
Old 05-24-2006   #1 (permalink)
hiteshlohani's Avatar
Curious


 
hiteshlohani is an unknown quantity at this point
 



Not Ranked  0 score     
Can anyone help me explaining a program code

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

Last edited by hiteshlohani; 05-24-2006 at 10:41 AM..
Reply With Quote
Old 05-24-2006   #2 (permalink)
Pyrotex's Avatar
Slaying Bad Memes

Moderator
Editor

Location:
Houston, Texas
Latest blog entry:
 
Pyrotex has a reputation beyond reputePyrotex has a reputation beyond reputePyrotex has a reputation beyond reputePyrotex has a reputation beyond reputePyrotex has a reputation beyond reputePyrotex has a reputation beyond reputePyrotex has a reputation beyond reputePyrotex has a reputation beyond reputePyrotex has a reputation beyond reputePyrotex has a reputation beyond reputePyrotex has a reputation beyond repute
Send a message via MSN to Pyrotex
 



Not Ranked  0 score     
Re: Can anyone help me explaining a program code

Quote:
Originally Posted by hiteshlohani
...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. 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.


----------------
Hypography Forums Moderator
-- - - - - -
What concerns me is not the way things are, but rather the way people think things are.
Epictetus, Greek Philosopher
The map is NOT the territory.
Korzybski, Polish-American Philosopher

Last edited by Pyrotex; 05-25-2006 at 09:03 AM..
Reply With Quote
Old 05-24-2006   #3 (permalink)
alexander's Avatar
Dedicated Smart-ass

Senior Moderator
Gallery Curator
Dev Team Member

Location:
Just before 0xAA55
 
alexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond repute
Send a message via AIM to alexander
 



Not Ranked  0 score     
Re: Can anyone help me explaining a program code

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 [code][/code] 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:
Code:
#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();
be nice to your readers

no sweat, and pyro
nice job


----------------
Microsoft, the leader in using innovative tactics to promote irksome experience, coupled with antiquated technology that's held together by a pyramid of makeshift afterthoughts.

Apple, the leader in using irksome tactics to promote innovative experience, coupled with an antiquated core that's enhanced by state-of-the-art afterthoughts.

Linux, the leader in not using any tactics to promote user-defined experience, coupled with state-of-the-art core enhanced by innovative afterthoughts.


Last edited by alexander; 05-24-2006 at 05:46 PM..
Reply With Quote
Old 05-24-2006   #4 (permalink)
hiteshlohani's Avatar
Curious


 
hiteshlohani is an unknown quantity at this point
 



Not Ranked  0 score     
thanx Pyro, but............

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;
}
Reply With Quote
Old 05-25-2006   #5 (permalink)
Pyrotex's Avatar
Slaying Bad Memes

Moderator
Editor

Location:
Houston, Texas
Latest blog entry:
 
Pyrotex has a reputation beyond reputePyrotex has a reputation beyond reputePyrotex has a reputation beyond reputePyrotex has a reputation beyond reputePyrotex has a reputation beyond reputePyrotex has a reputation beyond reputePyrotex has a reputation beyond reputePyrotex has a reputation beyond reputePyrotex has a reputation beyond reputePyrotex has a reputation beyond reputePyrotex has a reputation beyond repute
Send a message via MSN to Pyrotex
 



Not Ranked  0 score     
Re: Can anyone help me explaining a program code

Quote:
Originally Posted by alexander
excellent, pyro beat me to it, but it's nice to see someone else explain the code other then myself, thumbs up Pyro ...
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? And can you answer hiteshlohani's 2nd question?


----------------
Hypography Forums Moderator
-- - - - - -
What concerns me is not the way things are, but rather the way people think things are.
Epictetus, Greek Philosopher
The map is NOT the territory.
Korzybski, Polish-American Philosopher

Last edited by Pyrotex; 05-25-2006 at 09:10 AM..
Reply With Quote
Old 05-25-2006   #6 (permalink)
alexander's Avatar
Dedicated Smart-ass

Senior Moderator
Gallery Curator
Dev Team Member

Location:
Just before 0xAA55
 
alexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond repute
Send a message via AIM to alexander
 



Not Ranked  0 score     
Thumbs up Re: Can anyone help me explaining a program code

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

ok, the recursive function will exit when b[i] is not equal to nothing whatever b[i] 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:
Code:
//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(b); //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(b))
    {
        *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:\\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();


----------------
Microsoft, the leader in using innovative tactics to promote irksome experience, coupled with antiquated technology that's held together by a pyramid of makeshift afterthoughts.

Apple, the leader in using irksome tactics to promote innovative experience, coupled with an antiquated core that's enhanced by state-of-the-art afterthoughts.

Linux, the leader in not using any tactics to promote user-defined experience, coupled with state-of-the-art core enhanced by innovative afterthoughts.


Last edited by alexander; 05-25-2006 at 04:55 PM..
Reply With Quote
Old 05-25-2006   #7 (permalink)
InfiniteNow's Avatar
Suspended


Location:
Austin, TX
 
InfiniteNow has a reputation beyond reputeInfiniteNow has a reputation beyond reputeInfiniteNow has a reputation beyond reputeInfiniteNow has a reputation beyond reputeInfiniteNow has a reputation beyond reputeInfiniteNow has a reputation beyond reputeInfiniteNow has a reputation beyond reputeInfiniteNow has a reputation beyond reputeInfiniteNow has a reputation beyond reputeInfiniteNow has a reputation beyond reputeInfiniteNow has a reputation beyond repute
 



Not Ranked  0 score     
Re: Can anyone help me explaining a program code

Actually, you're missing an x.
Reply With Quote
Old 05-25-2006   #8 (permalink)
alexander's Avatar
Dedicated Smart-ass

Senior Moderator
Gallery Curator
Dev Team Member

Location:
Just before 0xAA55
 
alexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond repute
Send a message via AIM to alexander
 



Not Ranked  0 score     
Re: Can anyone help me explaining a program code

missing an x, infy?


----------------
Microsoft, the leader in using innovative tactics to promote irksome experience, coupled with antiquated technology that's held together by a pyramid of makeshift afterthoughts.

Apple, the leader in using irksome tactics to promote innovative experience, coupled with an antiquated core that's enhanced by state-of-the-art afterthoughts.

Linux, the leader in not using any tactics to promote user-defined experience, coupled with state-of-the-art core enhanced by innovative afterthoughts.

Reply With Quote
Old 05-25-2006   #9 (permalink)
InfiniteNow's Avatar
Suspended


Location:
Austin, TX
 
InfiniteNow has a reputation beyond reputeInfiniteNow has a reputation beyond reputeInfiniteNow has a reputation beyond reputeInfiniteNow has a reputation beyond reputeInfiniteNow has a reputation beyond reputeInfiniteNow has a reputation beyond reputeInfiniteNow has a reputation beyond reputeInfiniteNow has a reputation beyond reputeInfiniteNow has a reputation beyond reputeInfiniteNow has a reputation beyond reputeInfiniteNow has a reputation beyond repute
 



Not Ranked  0 score     
Re: Can anyone help me explaining a program code

Quote:
Originally Posted by alexander
missing an x, infy?
Old programming joke from when I was still in High School. We'd mess with the dumb kids who would show us a huge set of code and ask, "Does this look right?" We'd say, "Actually, you're missing an x."


Cheers.
Reply With Quote
Old 05-25-2006   #10 (permalink)
alexander's Avatar
Dedicated Smart-ass

Senior Moderator
Gallery Curator
Dev Team Member

Location:
Just before 0xAA55
 
alexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond reputealexander has a reputation beyond repute
Send a message via AIM to alexander
 



Not Ranked  0 score     
Re: Can anyone help me explaining a program code



well, i didnt write the code... i could, and it probably would be better then this code ...
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


----------------
Microsoft, the leader in using innovative tactics to promote irksome experience, coupled with antiquated technology that's held together by a pyramid of makeshift afterthoughts.

Apple, the leader in using irksome tactics to promote innovative experience, coupled with an antiquated core that's enhanced by state-of-the-art afterthoughts.

Linux, the leader in not using any tactics to promote user-defined experience, coupled with state-of-the-art core enhanced by innovative afterthoughts.

Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
urgent: need tic-tac-toe code in c++ using tree hiteshlohani Computer Science and Technology 3 05-22-2006 06:36 PM
deciphering a soap code ronthepon Science Projects and Homework 1 05-17-2006 03:07 PM
Explaining God with Physics HydrogenBond Theology forum 4 03-19-2006 12:12 AM
subliminal program nelton Political sciences 7 10-16-2005 11:39 AM
unbreakable code rileyj Physics and Mathematics 0 04-10-2004 11:46 AM

» Advertisement
» Current Poll
Who's the sexiest man alive? Johnny Depp or Robert Pattinson?
Johnny Depp - 30.00%
3 Votes
Robert Pattinson - 0%
0 Votes
Someone else (please specify) - 40.00%
4 Votes
I'm too macho to think a guy is sexy - 30.00%
3 Votes
Total Votes: 10
You may not vote on this poll.


All times are GMT -8. The time now is 11:18 AM.

Hypography?

Hypography [n.]: A combination of "hyperlink" and "bibliography" - ie, a list of links to electronic documents. Comparable to discography and bibliography, but not cartography.

We have been online since May 2000, and aim to be the best place to find and share science-related content of all kinds.

Share the love!

Please add more science to your life. Use our RSS feeds on your blog, your portal, or your favorite feedreader!


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
Copyright © 2000-2009 Hypography
Part of the Hypography - Science for Everyone Network