-
mine sweeper code help..
I have a 2D array i put a '*' char in 0,1 cell.
My program reads the board(2d array) from 1..size
so i subtract 1 from each of the "uncover" input coordinate.
If i dont step on a mine it replaces the value of the cell with the number of the
surrounding cells which has '*' in them.
i wrote this simple case code to demonstrate it
it should print the board with '*' in board[1][0] and '1' in board[0][0]
but it shows me a blank array .
why??
Code:
#include <stdio.h>
#define N 9
void printBoard(char board[N][N],int size);
int unCover(char board[N][N],int size,int x,int y);
int main(){
int index,kndex;
int x;
char board[N][N];
for(index=0;index<N;index++){
for(kndex=0;kndex<N;kndex++){
board[index][kndex]=' ';
}
}
board[1][0]='*';
x=unCover(board,5,1,1);
}
int unCover(char board[N][N],int size,int x,int y){
int count=0;
if (board[y-1][x-1]=='*'){
printf("you lost");
board[y-1][x-1]=='X';
printBoard(board, size);
return 0;
}
else
{
if (board[y][x]!='*'){
if (((y-2)>0) && ((x-2)>0) && (board[y-2][x-2]=='*')){ //counting how many mines surrounds the cell
count++;
}
if (((y-2)>0) && ((x-1)>0) && (board[y-2][x-1]=='*')){
count++;
}
if (((y-2)>0) && ((x)<size) && (board[y-2][x]=='*')){
count++;
}
if (((y)<size) && ((x-2)>0) && (board[y][x-2]=='*')){
count++;
}
if (((y)<size) && ((x-1)>0) && (board[y][x-1]=='*')){
count++;
}
if (((y)<size) && ((x)<size) && (board[y][x]=='*')){
count++;
}
if (((y-1)>0) && ((x-2)>0) && (board[y-1][x-2]=='*')){
count++;
}
if (((y-1)>0) && ((x)<size) && (board[y-1][x]=='*')){
count++;
}
board[y-1][x]=(char)count;
printBoard(board, size);
return 1;
}
}
}
void printBoard(char board[N][N],int size){
int index,kndex;
for(index=0;index<size;index++){
for(kndex=0;kndex<size;kndex++){
printf("+-+");
}
printf("\n");
for(kndex=0;kndex<size;kndex++){
printf("|%c|",board[index][kndex]);
}
printf("\n");
}
for(kndex=0;kndex<size;kndex++){
printf("+-+");
}
}
-
Code:
int unCover(char board[N][N],int size,int x,int y){
// ...
board[y-1][x]=(char)count;
}
You assign the count to the wrong index, plus casting the count may not be suitable here. Try:
Code:
board[y-1][x-1]='0'+count;
Instead.
Next thing are your if statements that check whether or not a surrounding index is out of boundaries. Remember that array index begins at 0.
For example, one of your if statements is as follows:
Code:
if (((y)<size) && ((x-1) > 0) && (board[y][x-1]=='*')){
Try changing all of the inequalities with zeros to something like:
Code:
if (((y)<size) && ((x-1) >= 0) && (board[y][x-1]=='*')){
-
i did your changes
its not working .
if the user prints to uncover 1,1 then because its counting from 1 to size
the the actual coordinate is 0,0.
also the column number is X and the row number is y the input goes like x,y
so "y" in the rows and "x" in cols.
it need to print '1' on the 0,0 cell and show the astrix in board[1][0].
Code:
#include <stdio.h>
#define N 9
void printBoard(char board[N][N],int size);
int unCover(char board[N][N],int size,int x,int y);
int main(){
int index,kndex;
int x;
char board[N][N];
for(index=0;index<N;index++){
for(kndex=0;kndex<N;kndex++){
board[index][kndex]=' ';
}
}
board[1][0]='*';
x=unCover(board,5,1,1);
return 0;
}
int unCover(char board[N][N],int size,int x,int y){
int count=0;
if (board[y-1][x-1]=='*'){
printf("you lost");
board[y-1][x-1]=='X';
printBoard(board, size);
return 0;
}
else
{
if (board[y][x]!='*'){
//if (((y-1)<1)||(((x-1)<1)||((x-1)>size)||((y-1)>size)){
if (((y-2)>=0) && ((x-2)>=0) && (board[y-2][x-2]=='*')){
count++;
}
if (((y-2)>=0) && ((x-1)>=0) && (board[y-2][x-1]=='*')){
count++;
}
if (((y-2)>=0) && ((x)<=size) && (board[y-2][x]=='*')){
count++;
}
if (((y)<size) && ((x-2)>=0) && (board[y][x-2]=='*')){
count++;
}
if (((y)<size) && ((x-1)>=0) && (board[y][x-1]=='*')){
count++;
}
if (((y)<size) && ((x)<size) && (board[y][x]=='*')){
count++;
}
if (((y-1)>=0) && ((x-2)>=0) && (board[y-1][x-2]=='*')){
count++;
}
if (((y-1)>=0) && ((x)<size) && (board[y-1][x]=='*')){
count++;
}
board[y-1][x]='0' +count;
printBoard(board, size);
return 1;
}
}
}
void printBoard(char board[N][N],int size){
int index,kndex;
for(index=0;index<size;index++){
for(kndex=0;kndex<size;kndex++){
printf("+-+");
}
printf("\n");
for(kndex=0;kndex<size;kndex++){
printf("|%c|",board[index][kndex]);
}
printf("\n");
}
for(kndex=0;kndex<size;kndex++){
printf("+-+");
}
}
-
You again assign the count to the wrong index:
Code:
board[y-1][x]='0'+count;
-
ok it prints the number in the right place.
why it doesnt print the astrix bellow it??
Code:
#include <stdio.h>
#define N 9
void printBoard(char board[N][N],int size);
int unCover(char board[N][N],int size,int x,int y);
int main(){
int index,kndex;
int x;
char board[N][N];
for(index=0;index<N;index++){
for(kndex=0;kndex<N;kndex++){
board[index][kndex]=' ';
}
}
board[1][0]='*';
x=unCover(board,5,1,1);
return 0;
}
int unCover(char board[N][N],int size,int x,int y){
int count=0;
if (board[y-1][x-1]=='*'){
printf("you lost");
board[y-1][x-1]=='X';
printBoard(board, size);
return 0;
}
else
{
if (board[y][x]!='*'){
//if (((y-1)<1)||(((x-1)<1)||((x-1)>size)||((y-1)>size)){
if (((y-2)>=0) && ((x-2)>=0) && (board[y-2][x-2]=='*')){
count++;
}
if (((y-2)>=0) && ((x-1)>=0) && (board[y-2][x-1]=='*')){
count++;
}
if (((y-2)>=0) && ((x)<=size) && (board[y-2][x]=='*')){
count++;
}
if (((y)<size) && ((x-2)>=0) && (board[y][x-2]=='*')){
count++;
}
if (((y)<size) && ((x-1)>=0) && (board[y][x-1]=='*')){
count++;
}
if (((y)<size) && ((x)<size) && (board[y][x]=='*')){
count++;
}
if (((y-1)>=0) && ((x-2)>=0) && (board[y-1][x-2]=='*')){
count++;
}
if (((y-1)>=0) && ((x)<size) && (board[y-1][x]=='*')){
count++;
}
board[y-1][x-1]='0' +count;
printBoard(board, size);
return 1;
}
}
}
void printBoard(char board[N][N],int size){
int index,kndex;
for(index=0;index<size;index++){
for(kndex=0;kndex<size;kndex++){
printf("+-+");
}
printf("\n");
for(kndex=0;kndex<size;kndex++){
printf("|%c|",board[index][kndex]);
}
printf("\n");
}
for(kndex=0;kndex<size;kndex++){
printf("+-+");
}
}
-
On line 42:
Code:
if (((y-2)>=0) && ((x)<=size) && (board[y-2][x]=='*')){
You do not want to reach size as that will be beyond the boundary.
Regardless of the above, it does print an asterisk on on my computer.
-
I did your fix
and it worked correctly.
but when i put an astrix on
board[1][0]='*';
board[1][1]='*';
board[0][1]='*';
(surrounded it by astrixes) it needs to show 3 on 0,0 ant the astrixes
but its not showing anything at all
??
Code:
#include <stdio.h>
#define N 9
void printBoard(char board[N][N],int size);
int unCover(char board[N][N],int size,int x,int y);
int main(){
int index,kndex;
int x;
char board[N][N];
for(index=0;index<N;index++){
for(kndex=0;kndex<N;kndex++){
board[index][kndex]=' ';
}
}
board[1][0]='*';
board[1][1]='*';
board[0][1]='*';
x=unCover(board,5,1,1);
return 0;
}
int unCover(char board[N][N],int size,int x,int y){
int count=0;
if (board[y-1][x-1]=='*'){
printf("you lost");
board[y-1][x-1]=='X';
printBoard(board, size);
return 0;
}
else
{
if (board[y][x]!='*'){
//if (((y-1)<1)||(((x-1)<1)||((x-1)>size)||((y-1)>size)){
if (((y-2)>=0) && ((x-2)>=0) && (board[y-2][x-2]=='*')){
count++;
}
if (((y-2)>=0) && ((x-1)>=0) && (board[y-2][x-1]=='*')){
count++;
}
if (((y-2)>=0) && ((x)<size) && (board[y-2][x]=='*')){
count++;
}
if (((y)<size) && ((x-2)>=0) && (board[y][x-2]=='*')){
count++;
}
if (((y)<size) && ((x-1)>=0) && (board[y][x-1]=='*')){
count++;
}
if (((y)<size) && ((x)<size) && (board[y][x]=='*')){
count++;
}
if (((y-1)>=0) && ((x-2)>=0) && (board[y-1][x-2]=='*')){
count++;
}
if (((y-1)>=0) && ((x)<size) && (board[y-1][x]=='*')){
count++;
}
board[y-1][x-1]='0' +count;
printBoard(board, size);
return 1;
}
}
}
void printBoard(char board[N][N],int size){
int index,kndex;
for(index=0;index<size;index++){
for(kndex=0;kndex<size;kndex++){
printf("+-+");
}
printf("\n");
for(kndex=0;kndex<size;kndex++){
printf("|%c|",board[index][kndex]);
}
printf("\n");
}
for(kndex=0;kndex<size;kndex++){
printf("+-+");
}
}
-
Check line number 35:
Code:
if (board[y][x]!='*'){
And tell me if you notice anything wrong.
-
ok i fixed it.
i entered a case where i choose to step on a mine
its supposed to enter X on this spot and print the board.(with that X on this spot)
but its not doing that
why??
Code:
#include <stdio.h>
#define N 9
void printBoard(char board[N][N],int size);
int unCover(char board[N][N],int size,int x,int y);
int main(){
int index,kndex;
int x;
char board[N][N];
for(index=0;index<N;index++){
for(kndex=0;kndex<N;kndex++){
board[index][kndex]=' ';
}
}
board[4][4]='*';
x=unCover(board,5,5,5);
return 0;
}
int unCover(char board[N][N],int size,int x,int y){
int count=0;
if (board[y-1][x-1]=='*'){
printf("you lost");
board[y-1][x-1]=='X';
printBoard(board, size);
return 0;
}
else
{
//if (((y-1)<1)||(((x-1)<1)||((x-1)>size)||((y-1)>size)){
if (((y-2)>=0) && ((x-2)>=0) && (board[y-2][x-2]=='*')){
count++;
}
if (((y-2)>=0) && ((x-1)>=0) && (board[y-2][x-1]=='*')){
count++;
}
if (((y-2)>=0) && ((x)<size) && (board[y-2][x]=='*')){
count++;
}
if (((y)<size) && ((x-2)>=0) && (board[y][x-2]=='*')){
count++;
}
if (((y)<size) && ((x-1)>=0) && (board[y][x-1]=='*')){
count++;
}
if (((y)<size) && ((x)<size) && (board[y][x]=='*')){
count++;
}
if (((y-1)>=0) && ((x-2)>=0) && (board[y-1][x-2]=='*')){
count++;
}
if (((y-1)>=0) && ((x)<size) && (board[y-1][x]=='*')){
count++;
}
board[y-1][x-1]='0' +count;
printBoard(board, size);
return 1;
}
}
void printBoard(char board[N][N],int size){
int index,kndex;
for(index=0;index<size;index++){
for(kndex=0;kndex<size;kndex++){
printf("+-+");
}
printf("\n");
for(kndex=0;kndex<size;kndex++){
printf("|%c|",board[index][kndex]);
}
printf("\n");
}
for(kndex=0;kndex<size;kndex++){
printf("+-+");
}
}
-