2014-11-01 4 views
-1

게임 Mancala 코드를 작성하고 테스트하는 중입니다. 지금까지, 나는라는 오류가 무엇입니까 :내 C 코드에서 Segmentation Fault 오류를 수정하는 방법

분할 오류 (코어 덤프)

을 나는 그것을 수정하고 계속 실행하는 방법을 모르겠어요.

#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; 
} 
+4

어떻게 프로그램을 직접 디버깅하려 했습니까? 예를 들어 [gdb] (https://www.gnu.org/software/gdb/) 또는 [Valgrind] (https://en.wikipedia.org/wiki/Valgrind)를 사용해 보셨습니까? – GoBusto

+2

'scanf ("% d", position); ' – wimh

+0

@Wimmel 예, 값으로 var를 전달하면 마술처럼 바꿀 수 있다고 devs가 상상하는 방법은 무엇입니까? –

답변

1

더 작은 조각으로 코드를 분리하고 각 조각을 테스트하고 싶을 수도 있습니다. 또는 아마도 이것은 당신을 도울 수 있습니다 : 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 문으로 인해 참이됩니다.

+0

나는 그것을 시도 할 것이다, 고마워! – Madhatter

+0

실제로 루프가 &&로 변경되는 동안 무한 루프로 끝날 것임을 깨달았습니다. – Madhatter