Thread: tick- tack- toe

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    tick- tack- toe

    do you know the game? "x or o" is another name for it. You need to form a continuity of the sign you choose- x or o. I need to write a function that will get a bi dimensional array as a parameter, & check if in this array x won or o.
    this is what I wrote:

    Code:
    int TickTackToe(char A[N][N],int n)
    {
    	int i,j;
    	char x='x',o='o',flag;
    	
    	for (i=j=0;i<n;i++,j++)
    		if (A[i][j]==A[i+1][j+1]==A[i+2][j+2])
    		 break;
    
    		for (i=n-1,j=n-1-i;i>=0;i--,j++)
    		   if (A[i][j]==A[i-1][j+1]==A[i-2][j+2])
    			  break;
    
    			for(i=0;i<n;i++)
    			   for(j=0;j<n;j++)
    				if (A[i][j]==A[i+1][j]==A[i+2][j])		
                   break;
    			
    				   for(j=0;j<n;j++)
    					   for(i=0;i<n;i++)
    				   	     if (A[i][j]==A[i][j+1]==A[i][j+2])
    
    						flag=A[i][j];
    
    						if (flag==x)
    							return (1);
    					else 
    						if (flag==o)
    							return (o);
    
    					else
    							return (-1);
    
    
    }
    problems:
    1. It allways give me 1 as return value.
    2. can you think of way to make it look nicer?

    TIA,

    Ronen

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    return (o) or return(0);???

    You can't do this:
    A[i][j]==A[i+1][j+1]==A[i+2][j+2]
    What's happenign is:
    A[i][j]==A[i+1][j+1] return 1 if true or 0 if false then:
    1:0==A[i+2][j+2]
    which I think is always false
    do instead
    A[i][j]==A[i+1][j+1] && A[i+1][j+1]==A[i+2][j+2]

    More
    Code:
    for (i=j=0;i<n;i++,j++)
        if (A[i][j]==A[i+1][j+1]==A[i+2][j+2])
            break;
    ... why do you do this?? Should you place return 1 instead break? Like it is now it simply runs a cicle and breaks. The i and j variables are then re-defined, and nothing is done regarding the previous cicle.
    Last edited by xErath; 06-23-2004 at 03:55 AM.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    I do the "break" cause I need the flag veriable activated.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    But if you have one cicle after another the values of i and j are redefined... if the break statment is reached, no more for()s should be parsed. Instead of break write return A[i][j]==x.
    Basicly it'll be like this:
    Code:
    int TickTackToe(char A[N][N],int n)
    {
    	int i,j;
    
    	for (i=j=0;i<n;i++,j++)//check diagonal up,left->right,down
    		if (A[i][j]==A[i+1][j+1]==A[i+2][j+2])
     			return A[i][j]=='x';
    
    	for (i=n-1,j=0;i>=0;i--,j++)//check diagonal right,up->down,left
       		if (A[i][j]==A[i-1][j+1]==A[i-2][j+2])
      			return A[i][j]=='x';
    
    	for(i=0;i<n;i++)//check vertical lines
    	   	for(j=0;j<n;j++)
    			if (A[i][j]==A[i+1][j]==A[i+2][j])
    				return A[i][j]=='x';
    
    	for(j=0;j<n;j++)//check horizontal lines
    		for(i=0;i<n;i++)
    			if (A[i][j]==A[i][j+1]==A[i][j+2])
    				return A[i][j]=='x';
    	//return statment should have been reached... if not no line was detected
    	return -1;
    }
    Hope this one helps.
    At the end you had:
    if (flag==x)
    return (1);
    else if (flag==o)
    return (0);

    return A[i][j]=='x'; does the same stuff: if that char is 'x' return 1, else 0. If no line is detected the function at the end return -1.
    Last edited by xErath; 06-23-2004 at 04:30 AM.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    This is also works...
    Thank you, xErath.

    Code:
    int TickTackToe(char A[N][N],int n)
    {
    	int i,j;
    	char x='x',o='o',flag;
    	
    	for (i=j=0;i<n;i++,j++)
    		if (A[i][j]==A[i+1][j+1]&&A[i+1][j+1]==A[i+2][j+2])
    		 break;
    
    	for (i=n-1,j=n-1-i;i>=0;i--,j++)
    	   if (A[i][j]==A[i-1][j+1]&&A[i-1][j+1]==A[i-2][j+2])
    	                 break;
    
    	for(i=0;i<n;i++)
    	   for(j=0;j<n;j++)
    	       if (A[i][j]==A[i+1][j]&&A[i+1][j]==A[i+2][j])		       break;
    
    			
    	 for(j=0;j<n;j++)
    	   for(i=0;i<n;i++)
    	       if (A[i][j]==A[i][j+1]&&A[i][j+1]==A[i][j+2])
    
    	flag=A[i][j];
                    	if (flag==x)
    		    return (1);
    	     else 
    		if (flag==o)
    		   return (0);
                         else
    		return (-1);
    }

    Ronen
    Last edited by ronenk; 06-23-2004 at 04:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 06:25 PM
  2. client server tick tack toe
    By a02227 in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 11-20-2004, 03:58 PM
  3. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  4. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 12:52 PM
  5. Replies: 22
    Last Post: 11-08-2001, 11:01 PM