Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with a recursive function

Status
Not open for further replies.

KPlateo

Programmer
Mar 14, 2001
6
US
this may take a few moments but it is appreciated....
as astart to a game of noughts and crosses i am using djgpp on win 98, place takes values 1-9, a_1[9] takes values 0-8 as can be seen from the code. board display(a) is just a printf function for a_1[9] to print either 1's,5's or 0's.place_it() just gets a user input.
the problem is if there is already a 5 on the board then
placing a 1 in that position puts a 1 there but you can also
place a 1 elsewhere afterwards because it has been told to
loop unless a nought is there. however if you place a 1 twice or more times and then replace the one it works just
fine. i think it is the recursive function part maybe?
any help would do thanks in advance.

int check_nought( int place, int a_1[] )

{
if ( ((place==1) && (a_1[place - 1]==0)) ||
((place==2) && (a_1[place - 1]==0)) ||
((place==3) && (a_1[place - 1]==0)) ||
((place==4) && (a_1[place - 1]==0)) ||
((place==5) && (a_1[place - 1]==0)) ||
((place==6) && (a_1[place - 1]==0)) ||
((place==7) && (a_1[place - 1]==0)) ||
((place==8) && (a_1[place - 1]==0)) ||
((place==9) && (a_1[place - 1]==0)) )
a_1[place - 1 ] = 1;
else
{
board_display( a );
place = place_it();
check_nought(place, a_1);
}
return a_1[place - 1];

}
 
It would help if you posted the rest of your code - it's hard to see what the logic of check_nought() is without seeing what calls it. There seems to be something screwy with your design though and it's unlikely that recursion is the best solution for this type of problem.

One thing that seems weird is this:

if ( ((place==1) && (a_1[place - 1]==0)) ||
((place==2) && (a_1[place - 1]==0)) ||
((place==3) && (a_1[place - 1]==0)) ||
((place==4) && (a_1[place - 1]==0)) ||
((place==5) && (a_1[place - 1]==0)) ||
((place==6) && (a_1[place - 1]==0)) ||
((place==7) && (a_1[place - 1]==0)) ||
((place==8) && (a_1[place - 1]==0)) ||
((place==9) && (a_1[place - 1]==0)) )
a_1[place - 1 ] = 1;
else

The if statement, given the logic described and assuming that place will always be between 1-9 inclusive, can be written as:

if (a_1[place-1]==0)
a_1[place-1]=1;

Since the 2nd condition is the same for all legal values of place, this is equivalent to all those if's &&'s and ||'s

Post some more code and maybe we'll have better luck helping you.

Russ
bobbitts@hotmail.com
 
here is the complete code if you can help

#include <stdio.h>

int a[9] , square_place = 0, win = 0;
void board_display( int x0y0[] );
int place_it( void );
int check_nought( int place, int a_1[] );
int check_cross( int place, int a_1[] );
int check_win( int a_1[], int wins );

main()

{
while ( win==0 )
{
board_display( a ) ;
square_place = place_it();
a[square_place - 1] = check_nought(square_place, a);
win = check_win( a, win );
board_display( a );
if ( win==0 )
{
square_place = place_it();
a[square_place - 1] = check_cross(square_place, a);
win = check_win( a, win );
board_display( a );
}
}
return 0;
}

void board_display( int x0y0[] )

{
printf(&quot;%d%d%d\n&quot;, x0y0[0], x0y0[1], x0y0[2]) ;
printf(&quot;%d%d%d\n&quot;, x0y0[3], x0y0[4], x0y0[5]) ;
printf(&quot;%d%d%d\n&quot;, x0y0[6], x0y0[7], x0y0[8]) ;
}

int place_it( void )

{
int place = 0;
puts(&quot;Enter sqaure value 1-9\n&quot;);
scanf(&quot;%d&quot;, &place );
return place;
}

int check_nought( int place, int a_1[] )

{
if ( (a_1[place -1] == 0) )
a_1[place - 1 ] = 1;
else
{
board_display( a );
place = place_it();
check_nought(place, a_1);
}
return a_1[place - 1];

}


int check_cross( int place, int a_1[] )

{
if ( (a_1[place - 1] == 0) )
a_1[place - 1 ] = 5;
else
{
board_display( a );
place = place_it();
check_cross(place, a_1);
}
return a_1[place - 1];

}

int check_win( int a_1[], int wins )

{
if ( ((a_1[0]==1) && (a_1[1]==1) && (a_1[2]==1)) ||
((a_1[3]==1) && (a_1[4]==1) && (a_1[5]==1)) ||
((a_1[6]==1) && (a_1[7]==1) && (a_1[8]==1)) ||
((a_1[0]==1) && (a_1[3]==1) && (a_1[6]==1)) ||
((a_1[1]==1) && (a_1[4]==1) && (a_1[7]==1)) ||
((a_1[2]==1) && (a_1[5]==1) && (a_1[8]==1)) ||
((a_1[0]==1) && (a_1[4]==1) && (a_1[8]==1)) ||
((a_1[2]==1) && (a_1[4]==1) && (a_1[6]==1)) ||
((a_1[0]==5) && (a_1[1]==5) && (a_1[2]==5)) ||
((a_1[3]==5) && (a_1[4]==5) && (a_1[5]==5)) ||
((a_1[6]==5) && (a_1[7]==5) && (a_1[8]==5)) ||
((a_1[0]==5) && (a_1[3]==5) && (a_1[6]==5)) ||
((a_1[1]==5) && (a_1[4]==5) && (a_1[7]==5)) ||
((a_1[2]==5) && (a_1[5]==5) && (a_1[8]==5)) ||
((a_1[0]==5) && (a_1[4]==5) && (a_1[8]==5)) ||
((a_1[2]==5) && (a_1[4]==5) && (a_1[6]==5)) ||
((a_1[0]!=0) && (a_1[1]!=0) && (a_1[2]!=0))&&
((a_1[3]!=0) && (a_1[4]!=0) && (a_1[5]!=0)) &&
((a_1[6]!=0) && (a_1[7]!=0) && (a_1[8]!=0)) )
wins = 1;
else
wins = 0;
return wins;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top