gpt4 book ai didi

c - 如何修复 C 代码中的段错误错误

转载 作者:行者123 更新时间:2023-11-30 19:18:29 25 4
gpt4 key购买 nike

我正在为 Mancala 游戏编写代码,并一路进行测试。到目前为止,我收到一条错误消息:

Segmentation Fault (core dumped)

我不知道如何修复它并保持它运行。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void move_clockwise(int board[], int pos, int player_1);
void move_count_clock(int board[], int pos, int player_1);
void mancala_board(int board[]);
int *current1;
int *current2;
int board[30], player_1, sum, position, direction, i, n, play, initPos, curPos, pos;

//======================================================================

int main(void)
{
int board[30];
board[0] = 4;
board[1] = 4;
board[2] = 4;
board[3] = 4;
board[4] = 4;
board[5] = 4;
board[6] = 0;
board[7] = 4;
board[8] = 4;
board[9] = 4;
board[10] = 4;
board[11] = 4;
board[12] = 4;
board[13] = 0;

printf("Welcome to Mancala\n");
printf("\n\n 5 4 3 2 1 0 \n");
printf("BLUE\n");
printf("=====[%d] [%d] [%d] [%d] [%d] [%d]-----\n", board[5], board[4], board[3], board[2], board[1], board[0]);
printf("|%d| |%d|\n", board[6], board[13]);
printf("-----[%d] [%d] [%d] [%d] [%d] [%d]=====\n", board[7], board[8], board[9], board[10], board[11], board[12]);
printf(" RED\n");
printf(" 6 7 8 9 10 11 \n\n");
sum=board[6]+board[13];
player_1=first_player();

while(sum!=48)
{

while (player_1 == 1)
{
printf("Player RED\n");
printf("Pick a position corresponding to the integer: ");
scanf("%d", position);

while (position!=6 || position!=7 || position!=8 || position!=9 || position!=10 || position!=11)
{
printf("\nInvalid input, you have to select a bowl from your side.\nPlease select another position");
scanf("%d",&position);
}
printf("Would you like to move clockwise or counter-clockwise?");
printf("\n1. Clockwise \n2. Counter-Clockwise");
scanf("%d",&direction);
while(direction!=1 && direction!=2)
{
printf("\nInvalid input, you have to select an integer corresponding to the given options.\n");
printf("\n1. Clockwise \n2. Counter-Clockwise");
scanf("%d",&direction);
}
if (direction==1)
{
position+=1;
move_clockwise(board, position, player_1);
}
if(direction==2)
{
position=+1;
move_count_clock(board, position, player_1);
}
player_1-=1;
mancala_board(board);
}

if (player_1 == 0)
{
printf("Player BLUE\n");
printf("Pick a position corresponding to the integer: ");
scanf("%d", position);
while (position!=0 || position!=1 || position!=2 || position!=3 || position!=4 || position!=5)
{
printf("\nInvalid input, you have to select a bowl from your side.\nPlease select another position");
scanf("%d",&position);
}
printf("Would you like to move clockwise or counter-clockwise?");
printf("\n1. Clockwise \n2. Counter-Clockwise");
scanf("%d",&direction);
while(direction!=1 && direction!=2)
{
printf("\nInvalid input, you have to select an integer corresponding to the given options.\n");
printf("\n1. Clockwise \n2. Counter-Clockwise");
scanf("%d",&direction);
}
if (direction==1)
{
position+=1;
move_clockwise(board, position, player_1);
}
if(direction==2)
{
position=+1;
move_count_clock(board, position, player_1);
}
player_1+=1;
mancala_board(board);
}
sum=board[6]+board[13];
}
}

//======================================================================

int first_player(void)
{
//to determine who will be player 1
play=rand()%2;
return (play);
}

//======================================================================

//Display current board
void mancala_board(int board[])
{
printf("\n\n 5 4 3 2 1 0 \n");
printf("BLUE\n");
printf("=====[%d] [%d] [%d] [%d] [%d] [%d]-----\n", board[5], board[4], board[3], board[2], board[1], board[0]);
printf("|%d| |%d|\n", board[6], board[13]);
printf("-----[%d] [%d] [%d] [%d] [%d] [%d]=====\n", board[7], board[8], board[9], board[10], board[11], board[12]);
printf(" RED\n");
printf(" 6 7 8 9 10 11 \n\n");
}

//======================================================================

//allow player to move again if late marble lands in mancala
//void move_again(int board[], int pos, int player_1)
//{



//}

//======================================================================

//captures the marbles across the current position if player's
//last marble lands on their own bowl with no marbles in
//void capture()
//{



//}

//======================================================================

void move_clockwise(int board[], int pos, int player_1)
{
initPos = pos;
n=board[pos];
pos+=1;
for (i = 0; i < n ; i++)
{
curPos +=1;
board[curPos]+=1;

if (curPos == 14)
curPos -=14;

else if (curPos >= 28)
curPos -= 28;

if (player_1 == 0)
{
current1 = &(board[curPos]);
if (current1 == &(board[6]))
{
current1 = &(board[7]);
current1 += 1;
}
}

if (player_1 == 1)
{
current2 = &(board[curPos]);
if (current2 == &(board[13]))
{
current2 = &(board[0]);
current2 += 1;
}
}
}
board[initPos] = 0;
}

//======================================================================

void move_count_clock(int board[], int pos, int player_1)
{
initPos = pos;
n=board[pos];
pos+=1;
for (i = 0; i < n ; i++)
{
curPos +=1;
board[curPos]+=1;

if (curPos == 14)
curPos -=14;

else if (curPos >= 28)
curPos -= 28;

if (player_1 == 0)
{
current1 = &(board[curPos]);
if (current1 == &(board[6]))
{
current1 = &(board[7]);
current1 += 1;
}
}

if (player_1 == 1)
{
current2 = &(board[curPos]);
if (current2 == &(board[13]))
{
current2 = &(board[0]);
current2 += 1;
}
}
}
board[initPos] = 0;
}

最佳答案

也许您想将代码分成较小的部分并测试每个部分。或者也许这可以帮助您:http://ericlippert.com/2014/03/05/how-to-debug-small-programs/

将 & 放在 scanf 中是正确的建议,我认为评论中就是这个意思。

无论如何,这也很奇怪:

while (position!=6 || position!=7 || position!=8 || position!=9 || position!=10 || position!=11)
{
scanf("%d",&position);
}

以上你期望什么?它总是会进入该循环,并且永远不会出来,因为如果位置是 6,则它不能是 7,因此由于 OR,该语句将为 true。

关于c - 如何修复 C 代码中的段错误错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26694230/

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