Earendil
22nd May 2006, 09:11 PM
So, I'm bored, and I'm writing this code for what is essentially minesweeper. Sure, I could just play the game, but whatever. I'm a nerd.
What's happening is this: I've got the main function where you can choose the dimensions, and how many mines you want. It then moves to a playgame function, where I'm writing the game. An array is initiated for the playing board, and then user is prompted to enter a position to check for a mine. The check function checks if the value in the array is 'm' or not. If it's m, it's supposed to return a value so that the program says you hit a mine. Otherwise, it returns the number of surrounding mines.
Now, I've only been tinkering with this today, so no comments on the efficiency/method/the fact part of the code is missing...As it is, I figure it should return the value 9 and end the game if I pick a mine. If not, it should print a 4.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAX 500
void playgame(int, int, int);
int check(int[][], int, int);
int count (int[][], int, int);
int main ()
{
int height,width,nummines;
char confirm;
srand(time(NULL));
printf("Welcome to minesweeper!\n");
redo:
printf("Please enter dimensions (height width): ");
scanf("%d %d",&height,&width);
printf("Please enter number of mines: ");
scanf("%d",&nummines);
printf("A %d x %d board with %d mines? Y/N? ",height,width,nummines);
fflush(stdin);
confirm=getchar();
if(confirm=='Y'||confirm=='y')
{
if(height*width<=nummines)
{
printf("Too many mines, please re-enter your data.\n");
goto redo;
}
else
{
printf("Alright, let's begin!\n");
//PLAY GAME
playgame(height,width,nummines);
}
}
else if (confirm=='N'||confirm=='n')
{
printf("\nPlease re-enter your data!\n");
goto redo;
}
else
printf("Invalid response, please try again.\n");
return 0;
}
void playgame(int height, int width, int nummines)
{
int array[height][width],guesses=0,checked;
int i,j,k,x,y;
int newline=width-1;
for(i=0;i<height;i++) //Display grid
{
for(j=0;j<width;j++)
{
array[i][j]='x';
printf("%c ",array[i][j]);
if(j==newline)
printf("\n");
}
}
k=0;
while(k<=nummines) //Place mines
{
if(array[rand()%(height)][rand()%(width)]=='x')
{
array[rand()%(height)][rand()%(width)]='m';
k++;
}
}
printf("\n"); //Spacer
while (guesses <= ( (height*width) - nummines ) )
{
printf("Enter a position to guess. ");
scanf("%d %d",&x,&y);
checked=check(array,x,y);
if(checked==9)
{
printf("You hit a mine! \n");
break;
}
else
{
printf("%d \n",checked);
guesses++;
}
}
//Call check function
/*
if checked = 9; You lose, break.
*/
}
int check (int array[][],int x,int y)
{
if (array[x][y]=='m')
return count(array,x,y);
else return 3;
}
int count (int array[][MAX],int x,int y)
{
return 4;
}
My problem arises here:
if (array[x][y]=='m')
return count(array,x,y);
The compiler gives me the message "C:\DOCUME~1\PAULHI~1\Desktop\PROGRA~1\MINESW~1:108 : arithmetic on pointer to an incomplete type"
I'm not sure what that means. I've tried changing variable names in case there was an addressing issue, using pointers instead of simple array notation, and testing that kind of statement in a simple function which seemed to work, although it's possible I misinterpreted the way the functions were interacting with the arrays and wrote a wrong tester program.
An explanation of why this isn't working would be great. I have the hunch it has something to do with addressing of arrays and I've tinkered too deeply into their magics with my scarce knowledge of the language...
When it comes to my own fun programs which I won't be using elsewhere, I'm usually lazy with documentation too. If you have a question about what I'm doing (I doubt that, it's fairly simple code), feel free to PM me or something.
What's happening is this: I've got the main function where you can choose the dimensions, and how many mines you want. It then moves to a playgame function, where I'm writing the game. An array is initiated for the playing board, and then user is prompted to enter a position to check for a mine. The check function checks if the value in the array is 'm' or not. If it's m, it's supposed to return a value so that the program says you hit a mine. Otherwise, it returns the number of surrounding mines.
Now, I've only been tinkering with this today, so no comments on the efficiency/method/the fact part of the code is missing...As it is, I figure it should return the value 9 and end the game if I pick a mine. If not, it should print a 4.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define MAX 500
void playgame(int, int, int);
int check(int[][], int, int);
int count (int[][], int, int);
int main ()
{
int height,width,nummines;
char confirm;
srand(time(NULL));
printf("Welcome to minesweeper!\n");
redo:
printf("Please enter dimensions (height width): ");
scanf("%d %d",&height,&width);
printf("Please enter number of mines: ");
scanf("%d",&nummines);
printf("A %d x %d board with %d mines? Y/N? ",height,width,nummines);
fflush(stdin);
confirm=getchar();
if(confirm=='Y'||confirm=='y')
{
if(height*width<=nummines)
{
printf("Too many mines, please re-enter your data.\n");
goto redo;
}
else
{
printf("Alright, let's begin!\n");
//PLAY GAME
playgame(height,width,nummines);
}
}
else if (confirm=='N'||confirm=='n')
{
printf("\nPlease re-enter your data!\n");
goto redo;
}
else
printf("Invalid response, please try again.\n");
return 0;
}
void playgame(int height, int width, int nummines)
{
int array[height][width],guesses=0,checked;
int i,j,k,x,y;
int newline=width-1;
for(i=0;i<height;i++) //Display grid
{
for(j=0;j<width;j++)
{
array[i][j]='x';
printf("%c ",array[i][j]);
if(j==newline)
printf("\n");
}
}
k=0;
while(k<=nummines) //Place mines
{
if(array[rand()%(height)][rand()%(width)]=='x')
{
array[rand()%(height)][rand()%(width)]='m';
k++;
}
}
printf("\n"); //Spacer
while (guesses <= ( (height*width) - nummines ) )
{
printf("Enter a position to guess. ");
scanf("%d %d",&x,&y);
checked=check(array,x,y);
if(checked==9)
{
printf("You hit a mine! \n");
break;
}
else
{
printf("%d \n",checked);
guesses++;
}
}
//Call check function
/*
if checked = 9; You lose, break.
*/
}
int check (int array[][],int x,int y)
{
if (array[x][y]=='m')
return count(array,x,y);
else return 3;
}
int count (int array[][MAX],int x,int y)
{
return 4;
}
My problem arises here:
if (array[x][y]=='m')
return count(array,x,y);
The compiler gives me the message "C:\DOCUME~1\PAULHI~1\Desktop\PROGRA~1\MINESW~1:108 : arithmetic on pointer to an incomplete type"
I'm not sure what that means. I've tried changing variable names in case there was an addressing issue, using pointers instead of simple array notation, and testing that kind of statement in a simple function which seemed to work, although it's possible I misinterpreted the way the functions were interacting with the arrays and wrote a wrong tester program.
An explanation of why this isn't working would be great. I have the hunch it has something to do with addressing of arrays and I've tinkered too deeply into their magics with my scarce knowledge of the language...
When it comes to my own fun programs which I won't be using elsewhere, I'm usually lazy with documentation too. If you have a question about what I'm doing (I doubt that, it's fairly simple code), feel free to PM me or something.