Piotr Osiewicz - 1 year ago 86
C Question

# Why does this function return 0 all the time?

I've got a rather weird problem with my tick-tac-toe game. I've set up a very simple mechanism which allows for (almost) flawless gameplay. At the moment of writing this post, I struggle with preventing the user from using input for inappropiate purposes (such as overwriting taken spots). I've tried to put a simple if statement in my function called ruch, which is responsbile for overwriting the array wynik(which by the way stores the data). Here's the function itself:

``````int ruch(char pole, bool gracz){
int i=0;
for(i=0;i<9;i++)
{
if(gracz==true)
{
if(pole==i+1)
{
if(wynik[i]=='#')
{
wynik[i]='X';
return 3;
}
else if(wynik[i]!='#')
return 0;
}
}
else if(gracz==false)
{
if(pole==i+1)
{
if(wynik[i]=='#')
{
wynik[i]='O';
return 3;
}
else if(wynik[i]!='#')
return 0;
}
}
}
}
``````

And the whole code: http://pastebin.com/zNHHpHA1

As you can see, the logic behind the return is rather smple- if ruch notices an unwanted behaviour, it will prompt user to re-evaluate his move:

``````    while(ruch(input,ruc)==0){
scanf("%d", &input);
ruch(input, ruc);}
``````

However, the ruch function returns 0 regardless of the input. I think that is the only obstacle that holds my code back from functioning (rather) properly. Thank you in advance for any help.
I am not a native english speaker, so bear with me.

In your pastebin, in lines 44-46 and 54-58, you call `ruch` thrice with the same input. Hence the second and the third call will always return 0.

Change

``````ruch(input,ruc);
printf("%d", ruch(input,ruc));
while(ruch(input, ruc)==0) {
scanf("%d", &input);
ruch(input, ruc);
}
``````

to something like

``````int result = ruch(input, ruc);
printf("%d", result);
while(result == 0) {