初学者C程序分割故障(核心倾倒)



该程序的编译很好,但我一直收到一个输出:

Segmentation fault (code dumped)

该程序旨在与人类和计算机一起玩一个随机空间的人和计算机的简单游戏。我很好奇这个信息的含义,并觉得它与我的指针有关,但我不确定如何或在哪里。任何帮助都非常感谢。这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Initializing board
void clear_table(char board [3][3])
{
int i, j;
//Outer loop for rows
for(i=0; i<3; i++)
{
    //Inner loop for columns
    for(j=0; j<3; j++)
    {
       board[i][j] = ' ';
    }
  }
}
// prints the board in nice format
void display_table (char board [3][3])
{
int i, j;
printf("nn The board is: n");
//Outer loop for rows
for(i=0; i<3; i++)
{
    //Inner loop for columns
    for(j=0; j<3; j++)
    {
        //Printing character
        printf(" %c", board[i][j]);
    }
    printf(" ");
    //Printing footer
    printf("n _ _ _ n");
 }
}
//Validates the move
int check_legal_option(char board[3][3], int row, int col)
{
//Checking position
if(board[row][col] == ' ')
{
   //Available
   return 1;
}
return 0;
}
//Function that generates a valid move for player 2
void generate_player2_move(char board[3][3], int *row, int *col)
{
//Validating move
while(1)
{
    //Generating computer position
    *row = rand() % 3;
    *col = rand() % 3;
    //Checking for empty position
    if(check_legal_option(board, *row, *col) == 1)
        break;
  }
}
//Function that checks whether board is full or not
int check_table_full(char board[3][3])
{
int i, j;
//Outer loop for rows
for(i=0; i<3; i++)
{
    //Inner loop for columns
    for(j=0; j<3; j++)
    {
        //Board is not Full
       if(board[i][j] == ' ')
            return 0;
    }
}
//Board Full
return 1;
}
// win returns true if the given player has won on the
// given board, else it returns false
int win (char board [3][3], char player)
{
return (board[0][0] == player && board[0][1] == player && board[0][2] == player) ||
       (board[1][0] == player && board[1][1] == player && board[1][2] == player) ||
       (board[2][0] == player && board[2][1] == player && board[2][2] == player) ||
       (board[0][0] == player && board[1][0] == player && board[2][0] == player) ||
       (board[0][1] == player && board[1][1] == player && board[2][1] == player) ||
       (board[0][2] == player && board[1][2] == player && board[2][2] == player) ||
       (board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
       (board[0][2] == player && board[1][1] == player && board[2][0] == player);
}
//Function that checks for winner
int check_three_in_a_row(char board[3][3], char player1, char player2)
{
//Checking for winning of player
if(win(board, 'X') == 1 && win(board, 'O') == 1)
{
    return 2;
}
//Checking for winning of player 1
else if(win(board, 'O') == 1)
{
    return 0;
}
//Checking for winning of player 2
else if(win(board, 'X') == 1)
{
    return 1;
}
//Tie
else
{
    return -1;
}
}
//Function that plays the game
void playGame(char board[3][3], char player1, char player2)
{
int row, col;
int winner;
//Loop till board is full
while(check_table_full(board) != 1)
{
    //Player turn
    while(1)
    {
        //Reading positions from user
        printf("n Player 1 enter your selection [row,col]: ");
        scanf("%d %d", &row, &col);
        //Making suitable for array indexing
        row = row - 1;
        col = col - 1;
        //Checking for empty position
        if(check_legal_option(board, row, col) == 1)
           break;
       else
           printf("n Invalid choice... Try again!! n ");
    }
    //Storing in array
    board[row][col] = player1;
    //Printing board
    display_table(board);
    //Finding winner
    winner = check_three_in_a_row(board, player1, player2);
    //If either of winner is found
    if(winner >= 0 && winner <= 2)
    {
        //Printing winner
        switch(winner)
        {
            //Displaying winner
            case 0: printf("n Player 1 won the game... n"); break;
            case 1: printf("n Player 2 won the game... n"); break;
            case 2: printf("n Game Tie ... n"); break;
        }
        return;
    }
    //Generating a move
    generate_player2_move(board, &row, &col);
    //Storing in array
    board[row][col] = player2;
    printf("n Player 2 has entered [row,col]: %d,%d n", row+1, col+1);
    //Printing board
    display_table(board);
    //Finding winner
    winner = check_three_in_a_row(board, player1, player2);
    //If either of winner is found
    if(winner >= 0 && winner <= 2)
    {
        //Printing winner
        switch(winner)
        {
            //Displaying winner
            case 0: printf("n Player 1 won the game... n"); break;
            case 1: printf("n Player 2 won the game... n"); break;
            case 2: printf("n Game Tie ... n"); break;
        }
        return;
    }
  }
}
//Main function
int main()
{
//Board
char board[3][3];
//Player characters
char player1Character='O', player2Character='X';
//Initializing random function
srand(time(NULL));
//Initializing board
clear_table(board);
//Printing board
display_table(board);
//Playing game
playGame(board, player1Character, player2Character);
return 0;
}

问题就在这里,在游戏开始时:

//Reading positions from user
printf("n Player 1 enter your selection [row,col]: ");
scanf("%d %d", &row, &col);

提示似乎建议您输入两个逗号分隔的数字,例如2,2以获取中心位置。

但是,格式字符串 scanf("%d %d"不说逗号,因此输入失败, col永远不会获得值。

相关内容

  • 没有找到相关文章

最新更新