I having a problem getting my code to continue working. I am getting a an error
ID: 3566585 • Letter: I
Question
I having a problem getting my code to continue working. I am getting a an error that says "Bus Error (core dumped)" and I can't figure out how to fix it to continue working. I want to fix this so I can move on with building/fixing the rest of my code. I really appreciate the help!!! Please add comments so I know what you did to change it.
#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 ");
printf(" 5 4 3 2 1 0 ");
printf("BLUE ");
printf("=====[%d] [%d] [%d] [%d] [%d] [%d]----- ", board[5], board[4], board[3], board[2], board[1], board[0]);
printf("|%d| |%d| ", board[6], board[13]);
printf("-----[%d] [%d] [%d] [%d] [%d] [%d]===== ", board[7], board[8], board[9], board[10], board[11], board[12]);
printf(" RED ");
printf(" 6 7 8 9 10 11 ");
sum=board[6]+board[13];
player_1=first_player();
while(sum!=48)
{
while (player_1 == 1)
{
printf("Player RED ");
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(" Invalid input, you have to select a bowl from your side. Please select another position ");
scanf("%d",&position);
}
printf("Would you like to move clockwise or counter-clockwise?");
printf(" 1. Clockwise 2. Counter-Clockwise");
scanf("%d",&direction);
while(direction!=1 && direction!=2)
{
printf(" Invalid input, you have to select an integer corresponding to the given options. ");
printf(" 1. Clockwise 2. 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 ");
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(" Invalid input, you have to select a bowl from your side. Please select another position ");
scanf("%d",&position);
}
printf("Would you like to move clockwise or counter-clockwise?");
printf(" 1. Clockwise 2. Counter-Clockwise");
scanf("%d",&direction);
while(direction!=1 && direction!=2)
{
printf(" Invalid input, you have to select an integer corresponding to the given options. ");
printf(" 1. Clockwise 2. 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(" 5 4 3 2 1 0 ");
printf("BLUE ");
printf("=====[%d] [%d] [%d] [%d] [%d] [%d]----- ", board[5], board[4], board[3], board[2], board[1], board[0]);
printf("|%d| |%d| ", board[6], board[13]);
printf("-----[%d] [%d] [%d] [%d] [%d] [%d]===== ", board[7], board[8], board[9], board[10], board[11], board[12]);
printf(" RED ");
printf(" 6 7 8 9 10 11 ");
}
//======================================================================
//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;
}
Explanation / Answer
#include <iostream>
using namespace std;
int board[14];
void move(int board[], int pos, int turnNum);
void update(int board[]);
int main()
{
int pos;
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;
cout<<"Welcome to Mancala! ";
cout<<"To play select a position on the board.";
cout<<" ";
cout<<"-------------------------------- ";
cout<<"| | 5 | 4 | 3 | 2 | 1 | 0 | | ";
cout<<"| 6 |-----------------------| 13| ";
cout<<"| | 7 | 8 | 9 | 10| 11| 12| | ";
cout<<"-------------------------------- ";
int totalM = board[6] + board[13];
int turnNum = 1;
while (totalM < 48)
{
update(board);
if (turnNum == 1)
{
cout<<" Player 1: Select a postion: ";
cin>> pos;
if (pos = 7 || 8 || 9 || 10 || 11 || 12)
{
move(board, pos, turnNum);
}
else
{
cout<<"Please select another position: ";
cin>> pos;
}
}
}
cin.get();
return 0;
}
void move(int board[], int pos, int turnNum)
{
int initPos = pos;
for (int i = 0; i < board[pos] + 1; i++)
{
int curPos = pos + i;
if (curPos >= 14)
curPos -= 14;
else if (curPos >= 28)
curPos -= 28;
if (turnNum == 1)
{
int * current1 = &(board[curPos]);
if (current1 == &(board[6]))
current1 = &(board[7]);
*current1 += 1;
}
else if (turnNum == 2)
{
int * current2 = &(board[curPos]);
if (current2 == &(board[13]))
current2 = &(board[0]);
*current2 += 1;
}
}
board[initPos] = 0;
}
void update(int board[])
{
cout<<" --------------------------------- ";
cout<<"| | "<<board[5]<<" | "<<board[4]<<" | "<<board[3]<<" | "<<board[2]<<" | "<<board[1]<<" | "<<board[0]<<" | | ";
cout<<"| "<<board[6]<<" |-----------------------| "<<board[13]<<" | ";
cout<<"| | "<<board[7]<<" | "<<board[8]<<" | "<<board[9]<<" | "<<board[10]<<" | "<<board[11]<<" | "<<board[12]<<" | |";
cout<<" --------------------------------- ";
}