I am having some proplems with my code - I am told to create a mouse and apple game and I am having some trouble with using the array in different funtions.
Here is a short version of my code -
//defining the size of the grid
const int SIZEY (11); //vertical dimension
const int SIZEX (19); //horizontal dimension
const int MAXAPPLES (6); //number of apples
//defining symbols used for display of the grid & content
const char MOUSE ('@'); //mouse
const char TUNNEL (' '); //tunnel
const char WALL ('#'); //border
const char APPLE ('o'); // APPLE
const char STORE ('+'); //Store
//defining the command letters to move the mouse on the maze
const int UP (72); //up arrow
const int DOWN (80); //down arrow
const int RIGHT (77); //right arrow
const int LEFT (75); //left arrow
//defining the other command letters
const char QUIT ('Q'); //to end the game
//---------------------------------------------------------------------------
//----- run game
//---------------------------------------------------------------------------
int main()
{
//function declarations (prototypes)
void initialiseGame( char g[][ SIZEX+1], char m[][ SIZEX+1], int mouse[], int apple[][2]);
void paintGame( const char g[][ SIZEX+1], string& mess);
bool isArrowKey( int k);
int getKeyPress();
void moveMouse( const char g[][ SIZEX+1], int m[], int apple[][2] , int key, string& mess);
void updateGrid( char g[][ SIZEX+1], const char m[][ SIZEX+1], int mouse[], int apple[][2]);
void endProgram();
//----------------------------------------------------------------------------------< LOCAL VARIABLES >
//local variable declarations //arrays that store ...
char grid[ SIZEY+1][ SIZEX+1]; //grid for display
char maze[ SIZEY+1][ SIZEX+1]; //structure of the maze
int mouse[2]; //mouse's position ([y-coordinate][x-coordinate])
string message( "LET'S START..."); //current message to player
int apple[MAXAPPLES+1][2]; //This is an array of 6 apples
//action...
Clrscr();
Seed(); //seed the random number generator
initialiseGame( grid, maze, mouse, apple); //initialise grid (incl. walls & mouse)
paintGame( grid, message); //display game info, modified grid & messages
int key( getKeyPress()); //read in selected key: arrow or letter command
while (key != QUIT) //while user does not want to quit
{
if ( isArrowKey( key))
{
int dx( 0), dy( 0);
moveMouse( grid, mouse, apple, key, message); //move mouse in that directio
updateGrid( grid, maze, mouse, apple); //update grid information
}
else
message = "INVALID KEY!"; //set 'Invalid key' message
paintGame( grid, message); //display game info, modified grid & messages
key = getKeyPress(); //display menu & read in next option
}
cout << "\nPLAYER WANTS TO QUIT.";
endProgram(); //display final message
return 0;
} //end main
//---------------------------------------------------------------------------
//----- initialise game state
//---------------------------------------------------------------------------
void initialiseGame( char grid[][ SIZEX+1], char maze[][ SIZEX+1], int mouse[], int apple[][2])
{ //initialise grid & place mouse in middle
void setInitialMazeStructure( char g[][ SIZEX+1]);
void setInitialMouseCoordinates( int mouse[]);
void updateGrid( char g[][ SIZEX+1], const char m[][ SIZEX+1], int mouse[], int apple[][2]);
void setIntialAPPLECoordinates(int apple[][2]);
setInitialMazeStructure( maze); //initialise maze
setInitialMouseCoordinates( mouse);//initialise mouse's position
setIntialAPPLECoordinates(apple); // intialise apple's position
updateGrid( grid, maze, mouse, apple); //prepare grid
//setIntialAPPLECoordinates(int apple[]);
} //end of initialiseGame
void setInitialMazeStructure( char maze[][ SIZEX+1])
{ //set the position of the walls in the maze
//initialise maze configuration
char initialMaze[ SIZEY+1][ SIZEX+1] //local array to store the maze structure
= { {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X','X','X','X', 'X','X','X','X','X','X',},
{'X', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#','#','#','#','#','#','#','#','#',},
{'X', '#', '#', '#', '#', '#', ' ', ' ', ' ', '#', '#', '#','#','#','#','#','#','#','#','#',}, // need a apple
{'X', '#', '#', '#', '#', '#', ' ', ' ', ' ', '#', '#', '#','#','#','#','#','#','#','#','#',}, // need a apple
{'X', '#', '#', '#', '#', '#', ' ', ' ', ' ', '#', '#', '#','#','#','#','#','#','#','#','#',}, // need 2
{'X', '#', '#', '#', '#', '#', ' ', ' ', ' ', ' ', '#', '#','#','#','#','#','#','#','#','#',},
{'X', '#', '#', '#', ' ', '#', ' ', '#', '#', ' ', '#', '#','#','#','#','#','#','#','#','#',},
{'X', '#', ' ', ' ', ' ', '#', ' ', '#', '#', ' ', '#', '#','#','#','#',' ',' ','+','+','#',},
{'X', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ',' ',' ',' ',' ','+','+','#'},
{'X', '#', '#', '#', '#', '#', ' ', '#', '#', '#', ' ', '#',' ','#','#',' ',' ','+','+','#',},
{'X', '#', '#', '#', '#', '#', ' ', ' ', ' ', ' ', ' ', ' ','#','#','#','#','#','#','#','#',},
{'X', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#','#','#','#','#','#','#','#','#',} };
//This is the function that places apples on the grid
void placeapple(char g[] [SIZEX + 1], int apple[][2])
{
g[ apple[1][0]] [apple[1][1]] = APPLE;
g[ apple[2][0]] [apple[2][1]] = APPLE;
g[ apple[3][0]] [apple[3][1]] = APPLE;
g[ apple[4][0]] [apple[4][1]] = APPLE;
g[ apple[5][0]] [apple[5][1]] = APPLE;
g[ apple[6][0]] [apple[6][1]] = APPLE;
}
// This is the function that checks which apple is which
void Identifyapple(const char g[][ SIZEX+1], int m[], int apple[][2],int dx, int dy, int n)
{
int ax = m[1]+dx; // it's the mouses x poisition + key diplacement
int ay = m[0]+dy;//it's the mouses y poisition + key diplacement
//-------------------------<>
// test each apple and identify which 1 matches the coordinates
for ( int i = 0; i < MAXAPPLES; i++)
{
if ((apple[1][0]= ay) && ((apple[1][1] = ax))) // testing if the apple1 matches the ax and ay
{
n = 0; // mouse is next to apple 1
}
//---------------<>
if ((apple[2][0] = ay) && (apple[2][1] = ax)) // testing if the apple2 matches the ax and ay
{
n = 2; // mouse is next to apple 2
}
if ((apple[3][0] = ay) && (apple[3][1] = ax)) // testing if the apple3 matches the ax and ay
{
n = 4; // mouse is next to apple 3
}
if ((apple[4][0] = ay) && (apple[4][1] = ax)) // testing if the apple4 matches the ax and ay
{
n = 6; // mouse is next to apple 4
}
if ((apple[5][0] = ay) && (apple[5][1] = ax)) // testing if the apple5 matches the ax and ay
{
n = 8; // mouse is next to apple 1
}
if ((apple[6][0] = ay) && (apple[6][1] = ax)) // testing if the apple6 matches the ax and ay
{
n = 10; // mouse is next to apple 1
}
//This is the bit of code that I'm having proplems with, when I ever I run it, I get an error that says '+=' left operand must be a l-value
case APPLE:
//if ( the apple can move)
if ( appleCanMove( apple, g, dx, dy,n))
{
//then move the apple
apple[0+n] += dy; //go in that Y direction //<<<=======FAULT---->>
apple[1+n]+= dx;//go in that X direction
//then move the mouse
m[0] += dy; //go in that Y direction
m[1] += dx; //go in that X direction
//This is the function definition to check if the apple has hit the mouse and if the mouse can move the apple and place it in a storage area.
bool appleCanMove( int apple[][2], const char g[][ SIZEX+1], int dx, int dy, int n)
{
bool canMove;
switch( g[n+dy][n+dx])//<>=======FAULT=====<>
{ //...depending on what's on the target position in grid...
case TUNNEL: //can move
canMove = true;
break;
default:
canMove = false;
}
return canMove;
}