gpt4 book ai didi

c - 初级 C 程序 - 段错误(核心转储)

转载 作者:行者123 更新时间:2023-11-30 16:38:20 25 4
gpt4 key购买 nike

程序编译得很好,但我不断收到以下输出:

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("\n\n 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;
}

最佳答案

问题就在这里,在 PlayGame 函数的开始处:

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

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

但是,格式字符串 scanf("%d %d" 没有说明任何有关逗号的内容,因此输入失败,并且 col 永远不会获取值。

关于c - 初级 C 程序 - 段错误(核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47519921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com