Hi guys,
I'm in need for some help in my homework assignment. I'm kind of new at C programming.
My problem is that for some reason when I print the board, the i counter jumps to astronomical number.
I placed an *?* on that line. (it's down below)
Oh and a major problem that I thought it was me but the debugger shows the board[][] after calling the mainProgram and transferring the board as board[], meaning it now has only 1 dimension (???).
Thanks for helping !
Code:
#include <stdio.h>
#define N 9
#define A 40
void mainProgram(char board[N][N]);
int input_mines(char board[N][N],int size);
void mainMenu();
int insertMine(char board[N][N],int size, char mines[40]);
void printBoard(char board[N][N],int size);
int main()
{
char board[N][N]={'0'};
mainProgram(board);
printf("Bye, please press enter to exit!\n");
getchar();
return 0;
}
void mainProgram(char board[N][N])
{
int i,j,flag=0,check,size;
char tempchar,Choice,input_s;
size=0;
do
{
SW:
mainMenu();
scanf ("%c",&Choice);
getchar();
Choice-=48;
switch (Choice)
{
case 0: //**exit**//
break;
case 1: //**choose board size**//
do
{
if (flag)
{
printf("|-incorrect input value-|\n");
}
printf("Please choose board size : ");
scanf("%c",&size);
getchar();
size-=48;
if (size<2 || size>9) flag=1;
else flag=0;
}
while (size<2 || size>9);
for (i=0;i<=size-1;i++) // '?' - the board //
{
for (j=0;j<=size-1;j++)
{
board[i][j]='?';
}
}
break;
case 2: //insert mines//
if (size!=0)
{
input_mines(board,size);
}
else
{
printf ("you must choose board size first !\n");
}
break;
case 4:
printBoard(board,size);
break;
default:
printf("There's no such option\n");
goto SW;
}
}
while (Choice!=0);
}
int input_mines(char board[N][N],int size)
{
char temp,mines[40];
printf ("enter x,y of the new mines (x1,y1)(x2,y2)...(xn,yn):\n");
scanf ("%s",&mines);
getchar();
insertMine(board,size,mines);
}
void mainMenu()
{
printf ("-------------------\n");
printf ("welcome to the game\n");
printf ("1.Choose board size\n");
printf ("2.Place Mines\n");
printf ("3.Remove Mines\n");
printf ("4.Show mines\n");
printf ("5.Start the game\n");
printf ("0.Exit\n");
printf ("please enter your choice (input controls needed): \n");
}
int insertMine(char board[N][N],int size, char mines[40])
{
char checking_arr[4],coordinates[160];
int i,j,k,flag;
checking_arr[1]='(';
checking_arr[2]=',';
checking_arr[3]=')';
i=0;
j=0;
while (mines[i]!=0) //extracting coordinates to var//
{
flag=0;
for (k=1;k<=3;k++)
{
do
{
i++;
if (checking_arr[k]==mines[i])
{
flag++;
}
else
{
if (48<mines[i]<57)
{
coordinates[j]=mines[i];
j++;
}
}
}
while (mines[i]==' ');
}
}
if (flag!=3) //quality check for input//
{
printf("wrong input\n");
for (i=1;i<=160;i++){ //delete the mines[]//
mines[i]=0;
}
mainProgram(board,size);
}
else //all ok ? put the mines into board//
{
i=0;
do
{
board[coordinates[i]][coordinates[i+1]]='*';
i+=2;
}
while (coordinates[i]!=0);
}
}
void printBoard(char board[N][N],int size)
{
int i,j;
for (i=0;i<=size-1;i++)
{
printf ("+");
for (j=0;j<=size-1;j++)
{
printf ("-+");
}
printf("\n|");
for (j=0;j<=size-1;j++)
{
printf("%s|",board[i][j]); *?*
}
printf ("+");
for (j=0;j<=size-1;j++)
{
printf ("-+");
}
}
printf("\n\n");
}