选择坐标时,应将其替换为“〜”。但是,它被〜的ascii值代替(126)。我尝试了几种不同的方法,但是我总是得到126而不是〜。有任何想法吗?
谢谢您的帮助!

int board_is_empty(int N, int board[ROWS][COLS])
{
    int i = 0, j = 0;
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
        {
            if (board[i][j] != '~')
            {
                return 0;
            }
        }
    }
    return 1;
}

//updates the board to replace each selected coordinate with a ~.
//returns nothing
void update_board (int board[ROWS][COLS], int row_target, int column_target)
{

    board[row_target][column_target] = '~';
}

int main(void)
{
    int game_board[ROWS][COLS] = {0};
    int rows, columns = 0;
    int players_turn = 1, target_column = -1, target_row = -1, value = 0;
    int row_selection = 0, column_selection = 0;
    int i = 0;

    initialize_game_board(game_board);
    display_board(game_board);
    generate_starting_point(game_board, &rows, &columns);

    printf ("\nPlease hit <Enter> to continue.\n");
    getchar ();

    while (board_is_empty(ROWS, game_board) != 1)
    {
        select_target (&target_row, &target_column, players_turn);
        value += game_board[target_row][target_column];
        update_board (game_board, target_row, target_column); //should cause the coordinates at target_row && target_column to be replaced with a ~
        display_board(game_board);
    }
    printf("\n%d", value);

}

最佳答案

“〜”是一个字符,您已将board声明为二维整数数组。

因此,当您编写board [row_target] [column_target] ='〜'时;

它将'〜'转换为整数,即其ascii值为126
然后变成board [row_target] [column_target] = 126

我建议将板子作为二维字符数组。希望它将解决您的问题。

如果只想将其作为整数,则将126视为特殊的否,这意味着通过声明“〜”

10-08 04:18