C 输入零时函数循环

C 输入零时函数循环,c,matrix,type-conversion,C,Matrix,Type Conversion,我有一个user_get_movement_index函数,提示用户输入一个0到8的位置,作为tic-tac-toe游戏的一部分 此移动索引被传递到is_position_empty,在该位置确定移动索引是否无效或移动索引已被获取,两者都显示错误消息并返回false以触发用户_get_movement_索引的递归 当输入两次相同的数字时,函数正确循环,当输入除0以外的任何其他数字时,函数的行为与预期相同 问题是当输入0时,它会导致无效位置错误消息的循环。 我不明白它是如何从内部循环的。 它如何不

我有一个user_get_movement_index函数,提示用户输入一个0到8的位置,作为tic-tac-toe游戏的一部分

此移动索引被传递到is_position_empty,在该位置确定移动索引是否无效或移动索引已被获取,两者都显示错误消息并返回false以触发用户_get_movement_索引的递归

当输入两次相同的数字时,函数正确循环,当输入除0以外的任何其他数字时,函数的行为与预期相同

问题是当输入0时,它会导致无效位置错误消息的循环。 我不明白它是如何从内部循环的。 它如何不在每个循环上提示用户输入? 为什么0会导致这个循环

是因为我们在比较0<0的位置是空的吗

我不熟悉C和堆栈溢出,所以请原谅我的格式、理解和糟糕的代码

//--------------------------------------------------
// 05. FUNCTION my_getchar (IMPLEMENTED)
//--------------------------------------------------
char my_get_char() {
//1. We create the variable to be returned
char res = ' ';

//2. We create an extra variable to consume any other characters entered until a return is pressed
boolean line_consumed = False;
char dummy_char = ' ';

//3. We consume the first char entered, i.e., the one we are interested at
res = getchar();

//4. While still there are remaining characters
while (line_consumed == False) {
    //4.1. We consume the next character
    dummy_char = getchar();

    //4.2. If the new character is the end of line one, we can ensure we have consumed the entire line.
    if (dummy_char == '\n')
        line_consumed = True;
}

//5. We return res
return res;
}

//------------------------------------
//  06. FUNCTION is_position_empty 
//------------------------------------
boolean is_position_empty(game* g, int pos) {
//1. We create the variable to be returned
boolean res = False;

//2. We check if the index is a valid one and if the board is empty at that index.
//If it is valid and free, we return True. 
//Otherwise, we return False and write a warning message.
int row= pos/3;
int column = pos%3;

if (pos<0 || pos>8){
    printf("\t Invalid Position. Try again!\n\n");
    return res;
}
else if (g->board[row][column]=='X' || g->board[row][column]=='O'){
    printf("\t This postion is already busy. Try Again!\n\n");
    return res;
}
else{

    res=True;
    return res;
}

}

//---------------------------------------
//  07. FUNCTION user_get_movement_index 
//---------------------------------------
int user_get_movement_index(game* g) {

//2. We create a boolean variable to control that we have received a valid movement index. 
boolean validMove=False;

//3. We create a char variable to control the index we are receiving by keyboard.
char indexChar;
int indexInt;

//We print a message asking for a new movement. 
printf("         Enter a position 0 to 8: ");

//We call to my_get_char to get the index and we convert it to an integer. 
indexChar=my_get_char();
indexInt=indexChar-'0';

//We call to is_position_empty to check that the index is a valid one.
validMove=is_position_empty(g, indexInt);

  if (validMove==True)
      return indexInt;
  else
      return user_get_movement_index(g);
}
当我将矩阵的所有元素初始化为“a”时,问题仍然存在。 当输入有效的移动时,调用process_movement函数,并将板的相应元素初始化为“X”或“O”

char mark;

if (g->status==1)
    mark='X';
else
    mark='O';

int row = pos/3;
int column = pos%3;

g->board[row][column]=mark;

通过在is_position empty中添加额外的printf,我可以看出整个函数正在循环,但它似乎没有退出is_position,因为它返回给用户的printf没有被打印。这怎么可能?在“用户获取”移动中只有一个循环,在“位置”中没有一个循环是空的,并且只有0的循环?

以下建议的代码:

#include <stdio.h>   // getchar()
#include <stdbool.h> // bool, true, false
#include <ctype.h>   // isdigit()

// prototypes
int my_get_char( void );
bool is_position_empty(game* g, int pos);
int user_get_movement_index(game* g);


//--------------------------------------------------
// 05. FUNCTION my_getchar (IMPLEMENTED)
//--------------------------------------------------
int my_get_char()
{
    //1. We create the variable to be returned

    //3. We consume the first char entered, i.e., the one we are interested at
    int res = getchar();

    //4. While still there are remaining characters
    while ( '\n' != getchar() );

    //5. We return res
    return res;
}


//------------------------------------
//  06. FUNCTION is_position_empty
//------------------------------------
bool is_position_empty(game* g, int pos)
{
    //2. We check if the index is a valid one and if the board is empty at that index.
    //If it is valid and free, we return True.
    //Otherwise, we return False and write a warning message.
    int row= pos/3; = 0
    int column = pos%3; = 0

    if (pos<0 || pos>8)
    {
        printf("\t Invalid Position. Try again!\n\n");
        return false;
    }

    else if (g->board[row][column]=='X' || g->board[row][column]=='O')
    {
        printf("\t This postion is already busy. Try Again!\n\n");
        return false;
    }

    return true;
}


//---------------------------------------
//  07. FUNCTION user_get_movement_index
//---------------------------------------
int user_get_movement_index(game* g)
{
    //3. We create a char variable to control the index we are receiving by keyboard.
    int indexInt;

    do
    {
        //We print a message asking for a new movement.
        printf("         Enter a position 0 to 8: ");

        //We call to my_get_char to get the index and we convert it to an integer.
        indexInt = my_get_char();
        if( isdigit( indexInt ) )
        {
            indexInt -= '0';
        }

        else
        {
            printf( "entry was not in the inclusive range: 0...8\n" );
            continue;
        }

        //We call to is_position_empty to check that the index is a valid one.
    } while( !is_position_empty(g, indexInt) );

    return indexInt;
}
  • 缺少
    main()
    函数
  • 缺少确定是否有获胜者以及获胜者的功能
  • 缺少游戏的定义
  • 没有任何意外的循环
  • 避免因使用“递归”函数而导致的问题
  • 现在建议的守则是:

    #include <stdio.h>   // getchar()
    #include <stdbool.h> // bool, true, false
    #include <ctype.h>   // isdigit()
    
    // prototypes
    int my_get_char( void );
    bool is_position_empty(game* g, int pos);
    int user_get_movement_index(game* g);
    
    
    //--------------------------------------------------
    // 05. FUNCTION my_getchar (IMPLEMENTED)
    //--------------------------------------------------
    int my_get_char()
    {
        //1. We create the variable to be returned
    
        //3. We consume the first char entered, i.e., the one we are interested at
        int res = getchar();
    
        //4. While still there are remaining characters
        while ( '\n' != getchar() );
    
        //5. We return res
        return res;
    }
    
    
    //------------------------------------
    //  06. FUNCTION is_position_empty
    //------------------------------------
    bool is_position_empty(game* g, int pos)
    {
        //2. We check if the index is a valid one and if the board is empty at that index.
        //If it is valid and free, we return True.
        //Otherwise, we return False and write a warning message.
        int row= pos/3; = 0
        int column = pos%3; = 0
    
        if (pos<0 || pos>8)
        {
            printf("\t Invalid Position. Try again!\n\n");
            return false;
        }
    
        else if (g->board[row][column]=='X' || g->board[row][column]=='O')
        {
            printf("\t This postion is already busy. Try Again!\n\n");
            return false;
        }
    
        return true;
    }
    
    
    //---------------------------------------
    //  07. FUNCTION user_get_movement_index
    //---------------------------------------
    int user_get_movement_index(game* g)
    {
        //3. We create a char variable to control the index we are receiving by keyboard.
        int indexInt;
    
        do
        {
            //We print a message asking for a new movement.
            printf("         Enter a position 0 to 8: ");
    
            //We call to my_get_char to get the index and we convert it to an integer.
            indexInt = my_get_char();
            if( isdigit( indexInt ) )
            {
                indexInt -= '0';
            }
    
            else
            {
                printf( "entry was not in the inclusive range: 0...8\n" );
                continue;
            }
    
            //We call to is_position_empty to check that the index is a valid one.
        } while( !is_position_empty(g, indexInt) );
    
        return indexInt;
    }
    
    #包括//getchar()
    #包括//布尔、真、假
    #include//isdigit()
    //原型
    int my_get_char(void);
    bool为空(game*g,int pos);
    int user_get_movement_index(游戏*g);
    //--------------------------------------------------
    // 05. 函数my_getchar(已实现)
    //--------------------------------------------------
    int my_get_char()
    {
    //1.我们创建要返回的变量
    //3.我们使用输入的第一个字符,即我们感兴趣的字符
    int res=getchar();
    //4.虽然仍有剩余字符
    而('\n'!=getchar());
    //5.我们返回res
    返回res;
    }
    //------------------------------------
    //  06. 函数为\u位置\u空
    //------------------------------------
    布尔是空的(游戏*g,整数位置)
    {
    //2.我们检查索引是否有效,以及该索引处的板是否为空。
    //如果有效且免费,则返回True。
    //否则,我们将返回False并写入警告消息。
    int row=pos/3;=0
    int列=位置%3;=0
    如果(pos8)
    {
    printf(“\t位置无效。请重试!\n\n”);
    返回false;
    }
    如果(g->board[row][column]='X'| | g->board[row][column]='O'),则为else
    {
    printf(“\t此帖子已忙。请重试!\n\n”);
    返回false;
    }
    返回true;
    }
    //---------------------------------------
    //  07. 函数用户获取移动索引
    //---------------------------------------
    int user_get_movement_index(游戏*g)
    {
    //3.我们创建一个char变量来控制我们通过键盘接收的索引。
    int指数;
    做
    {
    //我们打印一条消息,要求进行新的运动。
    printf(“输入位置0到8:”);
    //我们调用my_get_char来获取索引,并将其转换为整数。
    indexInt=my_get_char();
    if(isdigit(indexInt))
    {
    指数-='0';
    }
    其他的
    {
    printf(“条目不在包含范围内:0…8\n”);
    继续;
    }
    //我们调用is_position_empty来检查索引是否有效。
    }而(!是位置为空(g,indexInt));
    回归指数;
    }
    
    当用户键入
    '0'
    时,程序将获得
    48
    (最有可能)。您需要将其转换为0..9范围内的数字(通过检查它是否为数字,然后从输入的字符中减去
    '0'
    )。此外,请不要发布输出的图片,粘贴文本并将其放入您的问题中。我在代码中减去了“0”以将字符转换为Interor,但不在此处。Post您是否已正确初始化
    g->board[][]
    ?另外,打印
    indexChar
    的值将有助于调试(您可能需要
    fflush(stdout)
    printf
    之后)。