I have a program that uses 2-D arrays to complete, and possibly some created fun
ID: 3546186 • Letter: I
Question
I have a program that uses 2-D arrays to complete, and possibly some created functions. Because it would be tedious for me to write down all of the code instructions, they can be accessed through this link:
https://www.dropbox.com/s/vr48tklf4fejis5/program_rules.docx
The document may seem long, but it is mostly just filled with example test cases so you can get a clearer view of how exactly the program should look like.
I would really appreciate if the answer to the program is provided through a link in which I can view it with all of its indentations (chegg doesn't show indentations).
Explanation / Answer
please rate - thanks
if you don't normally use #include <conio.h> and getch(); just remove them
http://ideone.com/6LFiEh
error checking
your data
#include <stdio.h>
#include <conio.h>
int games();
void play(int);
void initialize(int[][8]);
void fillboard(int[][8]);
void print(int,int[][8]);
int bombs();
void placebombs(int[][8],int);
int main()
{
int i,j,n;
n=games();
for(i=1;i<=n;i++)
{play(i);
}
getch();
return 0;
}
int games()
{int n;
printf("How many games do you want to play? ");
scanf("%d",&n);
return n;
}
int check(int i,int j,int board[][8])
{if(board[i][j]==9)
return 1;
return 0;
}
void fillboard(int board [][8])
{int i,j,count;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
{count=0;
if(board[i][j]!=9)
{if(i!=0&&j!=0)
count+=check(i-1,j-1,board);
if(i!=0)
count+=check(i-1,j,board);
if(i!=0&&j!=7)
count+=check(i-1,j+1,board);
if(j!=0)
count+=check(i,j-1,board);
if(j!=7)
count+=check(i,j+1,board);
if(i!=7&&j!=0)
count+=check(i+1,j-1,board);
if(i!=7)
count+=check(i+1,j,board);
if(i!=7&&j!=7)
count+=check(i+1,j+1,board);
board[i][j]=count;
}
}
}
void print(int game,int board[][8])
{int i,j;
for(i=0;i<8;i++)
{for(j=0;j<8;j++)
if(board[i][j]==9)
printf(" *");
else
printf("%2d",board[i][j]);
printf(" ");
}
}
void play(int game)
{int board[8][8],n;
printf("Game #%d ",game);
initialize(board);
n=bombs();
placebombs(board,n);
fillboard(board);
print(game,board);
}
void initialize(int board[][8]) //initialize all loacations to 0
{int i,j;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
board[i][j]=0;
}
int bombs()
{int n;
printf("How many bombs are on the board? ");
scanf("%d",&n);
return n;
}
void placebombs(int board[][8],int n)
{int i,j;
printf("What are the locations of each bomb? ");
while(n>0) //get bomb locations
{scanf("%d",&i);
scanf("%d",&j);
if(i<0||i>7||j<0||j>7)
printf("invalid location re-enter ");
else if(board[i][j]==9)
printf("bomb already placed there re-enter ");
else
{board[i][j]=9;
n--;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.