PDA

View Full Version : C problem...can't figure out error.



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.

Green Killer Puppet
23rd May 2006, 01:19 AM
I remember making mine sweeper, somehow, it's sort of like "Hello World," and everyone in my computer sciences class had to do one for the intro class' final.

To tell you the truth, I don't ever use C for the reasons of stupid syntax rules and too lazy to type the extra stuff in. C++ all the way, which is basically the same, except not.

So, to help you with your problem, I have no clue. Haha.

Umm... I just don't understand the compiler message.

Which one are you using?

Earendil
23rd May 2006, 01:49 PM
It's called JFE/GCC.

I'm pretty sure it's the one you can download from the gnu compiler collection. JFE stands for Jen's File Editor.

I've never even seen that error message before, and I've made a lot of errors on previous projects...

Green Killer Puppet
23rd May 2006, 11:52 PM
Yea, I was just thinking that most compilers have the same errror messages.

I really have no clue what that means. Either because I haven't touched programming in a year or so, or I'm just plain too noob for it, I'm not famaliar with that error.

Hopefully, someone else can help you out.

Carnage
24th May 2006, 08:54 AM
i ran it thru my C compiler and got the following errors:


c:\docume~1\chris\mydocu~1\untitl~1.cpp:7: declaration of `' as multidimensional array
c:\docume~1\chris\mydocu~1\untitl~1.cpp:7: must have bounds for all dimensions except the first
c:\docume~1\chris\mydocu~1\untitl~1.cpp:8: declaration of `' as multidimensional array
c:\docume~1\chris\mydocu~1\untitl~1.cpp:8: must have bounds for all dimensions except the first
c:\docume~1\chris\mydocu~1\untitl~1.cpp: In function `void playgame(int, int, int)':
c:\docume~1\chris\mydocu~1\untitl~1.cpp:85: passing `int (*)[((width - 1) + 1)]' to argument 1 of `check(int, int)' lacks a cast
c:\docume~1\chris\mydocu~1\untitl~1.cpp:7: too many arguments to function `int check(int, int)'
c:\docume~1\chris\mydocu~1\untitl~1.cpp:85: at this point in file
c:\docume~1\chris\mydocu~1\untitl~1.cpp: At top level:
c:\docume~1\chris\mydocu~1\untitl~1.cpp:107: declaration of `array' as multidimensional array
c:\docume~1\chris\mydocu~1\untitl~1.cpp:107: must have bounds for all dimensions except the first
c:\docume~1\chris\mydocu~1\untitl~1.cpp: In function `int check(int, int)':
c:\docume~1\chris\mydocu~1\untitl~1.cpp:108: `array' undeclared (first use this function)
c:\docume~1\chris\mydocu~1\untitl~1.cpp:108: (Each undeclared identifier is reported only once
c:\docume~1\chris\mydocu~1\untitl~1.cpp:108: for each function it appears in.)


i've not tried deciphering them yet, but they may be more familier to you. . .

Xull
24th May 2006, 02:30 PM
from what I figure, the arithmetic its referring to is comparison... you are comparing an int to a char. see what happens if you cast one type to the other, so they match.

Earendil
24th May 2006, 05:11 PM
Well, by including the character within single quotes, that gives you the ascii value.

So if you have like
int i = 'm';
printf("%d",i);

It'll display 109. Do you suggest a (char)array[x][y] cast, or a (int)(m) cast? Not sure if it makes a difference, but I've never used a (char) cast. I'll try to give these ideas a whirl later tonight.

Xull
26th May 2006, 10:55 AM
So, any success?

I can't exactly check, since my compiler doesn't like most of what you're doing :tongue:
(I find it kind of weird that gcc would allow it... gcc is supposed to be quite strict - you sure thats the only issue?)

also, have you considered passing explicit pointers and doing the indexing yourself?

Flocito
26th May 2006, 06:44 PM
Why not make the code easier, since you defined your array as an int and have a 0=no mine and a 1=mine? That way you don't have to deal with any of this stuff where you are trying to store a character into an int array.