Noughts and Crosses Problem

This is a discussion on Noughts and Crosses Problem within the C++ Programming forums, part of the General Programming Boards category; I have tried to correct this code but couldn't..can any body help me pls..its due on monday morning I did ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    2

    Question Noughts and Crosses Problem

    I have tried to correct this code but couldn't..can any body help me pls..its due on monday morning
    I did enlarged the lines where the debug report says there is a problem:
    Code:
    #include <iostream>
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    #include<ctype.h>
    #include<string.h>
    #include<stdlib.h>
    using namespace std;
    
    #define ENTER_KEY '\r'
    #define ESCAPE_KEY 27
    #define SPECIAL_KEY 0
    #define UP_ARROW 72
    #define DOWN_ARROW 80
    #define LEFT_ARROW 75
    #define RIGHT_ARROW 77
    #define pos_cursor(x,y) gotoxy(x*4+2,y*2+2)
    
    // The following two parameters can be changed as per needed
    const ORDER=3;		// determines the order of the board
    const level=3;		// determines the computer level (1=easiest)
    
    
    char board[ORDER][ORDER];
    int computer=1;
    int pos;
    
    //The functions' purposes are evident from their names
    void display(void);
    void empty_board(void);
    void input(int player);
    int chkboard(void);	//checks if any player has won
    void counter_move(int player, int *best_i, int *best_j, int *best_pos);
    void best_move(int player);
    void clrscr(void);
    int i;
    
    void main(void)
    {
    	clrscr();
    	empty_board();
    
    
    	printf("\n\n\nDo you want to play versus COMPUTER ('n' for no) :");
    	char opt=getch();
    	if ( opt=='n' || opt=='N') computer=0;
    
    	int player=1, win=0,turn=1;
    	display();
    	do
    	{
    		printf("\nPlayer %d move \n", player);
    		printf("\nPress arrow keys to move, then press <Enter> to place your mark");
    		input(player);
    		display();
    		win=chkboard();
    		player=(player==1)?2:1;
    		turn++;
    	}
    	  while ( win==0 && turn<=ORDER*ORDER );
    	if (win) printf("\nPlayer %d won",win);
    	else printf("\nDraw");
    	printf("\n\nPlay again (y for yes) ");
    	opt=getch();
    	if ( opt=='y' || opt=='Y')
    		main();
    	else
    		exit(0);
    	getch();
    
    }
    
    void empty_board(void)
    {
    	for (int i=0;i<ORDER;i++)
    		for (int j=0;j<ORDER;j++)
    			board[i][j]=NULL;
    }
    
    
    void display(void)
    {
    	clrscr();
    	for (int i=0;i<ORDER;i++)
    	{
    		printf("\n");
    		for (int j=0;j<ORDER;j++)
    			printf(" %c |", board[i][j]);
    		printf("\b \n");
    
    		if (i!=ORDER-1)
    			for (j=0;j<ORDER*4-1;j++)
    				printf("ؤ");
    	}
    	printf("\nPress Esc to exit\n");
    }
    
    void input(int player)
    {
    	printf("\nPress S for best move");
    
    	if (player==2 && computer==1)
    	{
    		best_move(player);
    		return;
    	}
    
    	int x=0,y=0;
    	pos_cursor(x,y);		//macro to position cursor at (y,x)th element
    	unsigned char key, key2;
    	do
    	{
    		while( ( key=getch())!=ENTER_KEY )
    		{
    			if (key==ESCAPE_KEY)
    				exit(0);
    			else if (key=='S' || key=='s')
    				{
    					best_move(player);
    					return;
    				}
    			else if (key==SPECIAL_KEY)
    			{
    				key2=getch();
    				switch(key2)
    				{
    					case UP_ARROW: y--; break;
    					case DOWN_ARROW: y++; break;
    					case LEFT_ARROW: x--; break;
    					case RIGHT_ARROW: x++; break;
    
    				}
    				if (x<0) x=0;
    				else if (x>ORDER-1) x=ORDER-1;
    				else if (y<0) y=0;
    				else if (y>ORDER-1) y=ORDER-1;
    				pos_cursor(x,y);
    			}
    		}
    	}
    	  while(board[y][x]!=NULL);
    
    	char mark=(player==1)?'X':'O';
    	board[y][x]=mark;
    
    }
    
    int chkboard(void)
    {
    	int player2_position=0;
    	int player1,player2;
    
    	//rows and cols check
    	for (int k=0;k<2;k++)
    		for (int i=0;i<ORDER;i++)
    		{
    			player1=player2=0;
    
    			for (int j=0;j<ORDER;j++)
    			{
    				if (k==0)		//then check rows
    				{
    					if (board[i][j]=='X')
    						player1++;
    					else if (board[i][j]=='O')
    						player2++;
    				}
    				else		// else check cols
    				{
    					if (board[j][i]=='X')
    						player1++;
    					else if (board[j][i]=='O')
    						player2++;
    				}
    			}		//end loop j
    			if (player1==0)
    				player2_position+=player2;
    			if (player2==0)
    				player2_position-=player1;
    
    			if (player1==ORDER)
    			{
    				pos=-ORDER*ORDER;
    				return 1;
    			}
    			else if (player2==ORDER)
    			{
    				pos=ORDER*ORDER;
    				return 2;
    			}
    
    
    		}	//end loop i
    
    	//diagonal check
    
    	for (k=0;k<2;k++)
    	{
    		player1=player2=0;
    		for (i=0;i<ORDER;i++)
    		{
    			if (k==0)		//then check left diagonal
    			{
    					if (board[i][i]=='X')
    						player1++;
    					else if (board[i][i]=='O')
    						player2++;
    			}
    			else            //else check right diagonal
    			{
    					if (board[i][ORDER-1-i]=='X')
    						player1++;
    					else if (board[i][ORDER-1-i]=='O')
    						player2++;
    			}
    		}		// end loop i
    		if (player1==0)
    				player2_position+=player2;
    		if (player2==0)
    				player2_position-=player1;
    		pos=player2_position;
    
    		if (player1==ORDER)
    		{
    			pos=-ORDER*ORDER;
    			return 1;
    		}
    		else if (player2==ORDER)
    		{
    			pos=ORDER*ORDER;
    			return 2;
    		}
    
    	}
    //	printf("\nPlayer 2 Postion %d", player2_position);
    	::pos=player2_position;
    	return 0;
    }
    
    
    void best_move(int player)
    {
    	int temp_pos=0, tot_pos;
    
    	tot_pos=(player==1)?3276:-3276;
    
    	int best_i,best_j, best_pos;
    	int tmp_i[level],tmp_j[level];
    	char mark[3]={0,'X','O'};
    	int peak=ORDER*ORDER;
    	peak=(player==1)?peak:-peak;
    	int next_player=(player==1)?2:1;
    
    	for (int i=0;i<ORDER;i++)
    		for (int j=0;j<ORDER;j++)
    			if ( board[i][j]==NULL )
    			{
    				board[i][j]=mark[player];
    				temp_pos=0;
    				chkboard();
    				temp_pos+=pos;
    				next_player=player;
    				int cnt=0;
    
    				do
    				{
    					next_player=(next_player==1)?2:1;
    					counter_move(next_player, &tmp_i[cnt], &tmp_j[cnt], &best_pos);
    					temp_pos+=best_pos;
    					if (tmp_i[cnt]!=-1 && (best_pos)!=peak )
    						board[ tmp_i[cnt] ] [ tmp_j[cnt] ] = mark[next_player];
    					cnt++;
    
    				}
    					  while (tmp_i[cnt-1]!=-1 && best_pos!=peak && cnt<level);
    
    				if (cnt==level || tmp_i[cnt-1]==-1 || best_pos==peak)
    					cnt--;
    				if (tmp_i[cnt]==-1)
    					cnt--;
    
    				while(cnt>=0 && tmp_i[cnt]!=-1)
    				{
    					board[tmp_i[cnt]][tmp_j[cnt]]=NULL;
    					cnt--;
    				}
    
    				if (player==2)
    				{
    					if ( tot_pos<temp_pos )
    					{
    						best_i=i;
    						best_j=j;
    						tot_pos=temp_pos;
    					}
    				}
    				else
    					if ( tot_pos>temp_pos )
    					{
    						best_i=i;
    						best_j=j;
    						tot_pos=temp_pos;
    					}
    
    				board[i][j]=NULL;
    			}  	// end if (board==NULL)
    
    	board[best_i][best_j]=mark[player];
    //	printf("i=%d,j=%d,pos=%d", best_i,best_j,best_pos);
    
    }
    
    
    void counter_move(int player, int *best_i, int *best_j, int *best_pos)
    {
    	*best_i=*best_j=-1;
    	char mark=(player==1)?'X':'O';
    	*best_pos=(player==1)?3276:-3276;
    
    	for (int i=0;i<ORDER;i++)
    		for (int j=0;j<ORDER;j++)
    			if ( board[i][j]==NULL )
    			{
    				board[i][j]=mark;
    				chkboard();
    				if (player==2)
    				{
    					if ( *best_pos<pos )
    					{
    						*best_i=i;
    						*best_j=j;
    						*best_pos=pos;
    					}
    				}
    				else
    					if ( *best_pos>pos )
    					{
    						*best_i=i;
    						*best_j=j;
    						*best_pos=pos;
    					}
    
    				board[i][j]=NULL;
    			}  	// end if (board==NULL)
    	if (*best_i==-1)
    		*best_pos=0;//(player==2)?3276:-3276;
    
    	return;
    }
    Attached Files Attached Files
    Last edited by Salem; 01-09-2005 at 01:04 AM. Reason: ADDED CODE TAGS

  2. #2
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    Oooh... Asking people to do stuff for homework is a no-no.

    a) Use code tags
    b) Post errors
    c) Tell us what the program's supposed to do

    Then we'll try to help.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    2

    Exclamation Correction

    At least I didnt lie! I have attached the cpp file, this program supposed to be noughts and crosses game but I get error in lines 109 and 137 saying: 'gotoxy' identifier not found even with argument-dependent look up, I am still not expert with C++ and trying to improve my skills..if any body can help this will be appreciated.

  4. #4
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Then gotoxy is not a defined function.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by pal_london26
    At least I didnt lie! I have attached the cpp file, this program supposed to be noughts and crosses game but I get error in lines 109 and 137 saying: 'gotoxy' identifier not found even with argument-dependent look up, I am still not expert with C++ and trying to improve my skills..if any body can help this will be appreciated.
    The non-standard library function gotoxy() is in Borland's stuff, not Microsoft's.

    Your code won't compile under Borland's compiler for various reasons (Among them: you call main() recursively within main() --- not recommended.) Actually the game works pretty well if you fix the Borland compiler errors.

    Your code compiles but doesn't link with Visual C++ because there is no library function for gotoxy().

    Regards,

    Dave
    Last edited by Dave Evans; 01-09-2005 at 03:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noughts and crosses
    By forzamilan in forum C++ Programming
    Replies: 1
    Last Post: 10-26-2001, 06:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21