我正在为游戏Mancala编写代码,并在此过程中进行测试。到目前为止,我收到一个错误,上面写着:
分段故障(堆芯转储)
我不知道如何修复它并保持它的运行。
#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 Mancalan");
printf("nn 5 4 3 2 1 0 n");
printf("BLUEn");
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(" REDn");
printf(" 6 7 8 9 10 11 nn");
sum=board[6]+board[13];
player_1=first_player();
while(sum!=48)
{
while (player_1 == 1)
{
printf("Player REDn");
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 BLUEn");
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("nn 5 4 3 2 1 0 n");
printf("BLUEn");
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(" REDn");
printf(" 6 7 8 9 10 11 nn");
}
//======================================================================
//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。