need help for assignment,Please!!!

This is a discussion on need help for assignment,Please!!! within the C++ Programming forums, part of the General Programming Boards category; Hiya I have an assignment to hand in next week and I am still struggling with it. It is a ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    14

    Red face need help for assignment,Please!!!

    Hiya
    I have an assignment to hand in next week and I am still struggling with it.
    It is a game called MOUSE & APPLE. mouse(@) have to store apples(O) in storages(+).I don't know how the mouse can push the apples.I mean how can I move apples?!it is my first time I am working on arrays.I am a beginner.

    I put my codes and 2 headers that I used.I don't have enough time for it and I don't know what to do. need help
    any suggestions would be appreciated.I dont want any codes.I need help about the way to do it not the codes. I think my problem is in "case APPLE" in function moveMouse.Am I right!!

    Thanks

    here is my codes:

    Code:
    //include standard libraries
    #include <iostream>		//for output and input: getch(), cout <<, cin >>
    #include <conio.h>		//for kbhit
    #include <iomanip> 		//for formatted output
    #include <string>		//for string
    using namespace std;
    
    //include our own libraries
    #include "RandomUtils.h"	//for Seed, Random,
    #include "ConsoleUtils.h"	//for Clrscr, Gotoxy, etc.
    
    //---------------------------------------------------------------------------
    //----- define constants
    //---------------------------------------------------------------------------
    
    //defining the size of the grid
    #define SIZEY   (11)		//vertical dimension
    #define SIZEX   (19)     	//horizontal dimension
    //defining symbols used for display of the grid & content
    #define MOUSE   ('@')    	//mouse
    #define APPLE   ('O')		//apple
    #define TUNNEL  (' ')    	//tunnel
    #define WALL    ('#')    	//border
    #define STORAGE ('+') 	    //store area
    //defining the command letters to move the mouse on the maze
    #define UP      (72)	//up arrow
    #define DOWN    (80) 	//down arrow
    #define RIGHT   (77)	//right arrow
    #define LEFT    (75) 	//left arrow
    //defining the other command letters
    #define 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[]);
    	void paintGame( const char g[][ SIZEX+1], string& mess);
    	bool isArrowKey( int k);
    	void setKeyDirection( int k, int& dx, int& dy);
    	int  getKeyPress();
    	void moveMouse( const char g[][ SIZEX+1], int m[], int dx, int dy, string& mess);
    	void updateGrid( char g[][ SIZEX+1], const char m[][ SIZEX+1], int mouse[]);
    	void endProgram();
    
    	//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 key;				//arrow or letter command
    
    
     
    	//action...
    	Clrscr();
    	Seed();				//seed the random number generator
    	initialiseGame( grid, maze, mouse);	//initialise grid (incl. walls & mouse)
    	paintGame( grid, message);	//display game info, modified grid & messages
    	key = getKeyPress(); 	//display menu & read in selected option
    	while (key != QUIT)	//while user does not want to quit
    	{
    		if ( isArrowKey( key))
    		{
    			int dx( 0), dy( 0);
    			setKeyDirection( key, dx, dy); 	//find direction indicated by key
    			moveMouse( grid, mouse, dx, dy, message);	//move mouse in that direction
    			updateGrid( grid, maze, mouse);	//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
    	}
    	endProgram();		//display final message
    	return 0;
    } //end main
    
    
    //---------------------------------------------------------------------------
    //----- initialise game state
    //---------------------------------------------------------------------------
    
    void initialiseGame( char grid[][ SIZEX+1], char maze[][ SIZEX+1], int mouse[])
    { //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[]);
    
    	setInitialMazeStructure( maze);	//initialise maze
    	setInitialMouseCoordinates( mouse);	//initialise mouse's position
    	updateGrid( grid, maze, mouse); 	//prepare grid
    } //end of initialiseGrid
    
    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', '#', '#', '#', '#', '#', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
    		{'X', '#', '#', '#', '#', '#', 'O', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
    		{'X', '#', '#', '#', '#', '#', ' ', ' ', 'O', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
    		{'X', '#', '#', '#', ' ', ' ', 'O', ' ', 'O', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
    		{'X', '#', '#', '#', ' ', '#', ' ', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
    		{'X', '#', ' ', ' ', ' ', '#', ' ', '#', '#', ' ', '#', '#', '#', '#', '#', ' ', ' ', '+', '+', '#'},
    		{'X', '#', ' ', 'O', ' ', ' ', 'O', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '+', '+', '#'},
    		{'X', '#', '#', '#', '#', '#', ' ', '#', '#', '#', ' ', '#', ' ', '#', '#', ' ', ' ', '+', '+', '#'},
    		{'X', '#', '#', '#', '#', '#', ' ', ' ', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
    		{'X', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}};
    
    	
    	// with '#' for wall, ' ' for tunnel and 'X' for unused part of array
    	//copy into maze structure
    	for ( int row( 1); row <= SIZEY; ++row)	//for each row (vertically)
    		for ( int col( 1); col <= SIZEX; ++col)	//for each column (horizontally)
    			maze[row][col] = initialMaze[row][col];
    } //end of setMazeStructure
    
    void setInitialMouseCoordinates( int mouse[])
    { //calculate mouse's coordinates at beginning of game
    	mouse[0] = 9;  		//y-coordinate: vertically
    	mouse [1] = 15; 		//x-coordinate: horizontally
    } //end of setInitialMouseCoordinates
    
    
    //---------------------------------------------------------------------------
    //----- update grid state
    //---------------------------------------------------------------------------
    
    void updateGrid( char grid[][ SIZEX+1], const char maze[][ SIZEX+1], int mouse[])
    { //update grid configuration after each move
    	void setMaze( char g[][ SIZEX+1], const char m[][ SIZEX+1]);
    	void placeMouse( char g[][ SIZEX+1], int mouse[]);
    
    	setMaze( grid, maze);	//reset the empty maze configuration into grid
    	placeMouse( grid, mouse);	//set mouse in grid
    } //end of updateGrid
    
     
    
    void setMaze( char grid[][ SIZEX+1], const char maze[][ SIZEX+1])
    { //reset the empty/fixed maze configuration into grid
    	for ( int row( 1); row <= SIZEY; ++row)	//for each row (vertically)
    		for ( int col( 1); col <= SIZEX; ++col)	//for each column (horizontally)
    			grid[row][col] = maze[row][col];
    } //end of setFrame
    
    void placeMouse( char g[][ SIZEX+1], int m[])
    { //place mouse at its new position in grid
    	g[m[0]][m[1]] = MOUSE;
    } //end of placeMouse
    
    
    //---------------------------------------------------------------------------
    //----- move the mouse
    //---------------------------------------------------------------------------
    
    void moveMouse( const char g[][ SIZEX+1], int m[], int dx, int dy, string& mess)
    { //move mouse in required direction
    	//check new target position in grid & update mouse coordinates if move is possible
    	switch( g[m[0]+dy][m[1]+dx])
    	{			//...depending on what's on the target position in grid...
    		case TUNNEL:	//can move
    			m[0] += dy;	//go in that Y direction
    			m[1] += dx;	//go in that X direction
    			break;
    		case WALL:  	//hit a wall & stay there
    			cout << '\a';	//beep the alarm
    			mess = "CANNOT GO THERE!";
    			break;
    		case STORAGE:    //can move and store
    			m[0] += dy;	 //go in that Y direction
    			m[1] += dx;	 //go in that X direction
    			break;
    		case APPLE:
    			m[0] += dy;	 //go in that Y direction
    			m[1] += dx;	 //go in that X direction
    			break;
    
    
    	}
    } //end of moveMouse
    
    //---------------------------------------------------------------------------
    //----- process key
    //---------------------------------------------------------------------------
    
    void setKeyDirection( int key, int& dx, int& dy)
    { //
    	switch( key)		//...depending on the selected key...
    	{
    		case LEFT:  	//when LEFT arrow pressed...
    			dx = -1;	//decrease the X coordinate
    			dy = 0;
    			break;
    		case RIGHT: 	//when RIGHT arrow pressed...
    			dx = +1;	//increase the X coordinate
    			dy = 0;
    			break;
    		case UP: 		//when UP arrow pressed...
    			dx = 0;
    			dy = -1;	//decrease the Y coordinate
    			break;
    		case DOWN: 	//when DOWN arrow pressed...
    			dx = 0;
    			dy = +1;	//increase the Y coordinate
    			break;
    	}
    } //end of setKeyDirection
    
    int getKeyPress()
    { //get key or command selected by user
    	int keyPressed( getch());	//read in the selected arrow key or command letter
    	while ( keyPressed == 224) 	//ignore symbol following cursor key
    		keyPressed = getch();
    	keyPressed = toupper( keyPressed); 	//put it in uppercase
    	return( keyPressed);
    } //end of getKeyPress
    
    bool isArrowKey( int key)
    {	//check if the key pressed is an arrow key (also accept 'K', 'M', 'H' and 'P')
    	return ((key == LEFT) || (key == RIGHT) || (key == UP) || ( key == DOWN));
    }
    
     
    //---------------------------------------------------------------------------
    //----- display info on screen
    //---------------------------------------------------------------------------
    void paintGame( const char gd[][ SIZEX+1], string& mess)
    { //display game title, messages, maze, mouse & apples on screen
    	void paintGrid( const char g[][ SIZEX+1]);
    
    	Clrscr();
    
    	//display game title
    	SelectTextColour( clYellow);
    	Gotoxy(0, 0);
    	cout << "___MOUSE AND APPLES GAME___\n" << endl;
    	SelectBackColour( clWhite);
    	SelectTextColour( clRed);
    	Gotoxy(40, 0);
    	cout << "Anna";
    
    	// display grid contents
    	paintGrid( gd);
    
    	//display menu options available
    	SelectBackColour( clRed);
    	SelectTextColour( clYellow);
    	Gotoxy(40, 3);
    	cout << "TO MOVE USE KEYBOARD ARROWS ";
    	Gotoxy(40, 4);
    	cout << "TO QUIT ENTER 'Q'           ";
    
    	//print auxiliary messages if any
    	SelectBackColour( clBlack);
    	SelectTextColour( clWhite);
    	Gotoxy(40, 8);
    	cout << mess;	//display current message
    	mess = "";		//reset message to blank
    } //end of paintGame
    
    void paintGrid( const char g[][ SIZEX+1])
    { //display grid content on screen
    	SelectBackColour( clBlack);
    	SelectTextColour( clWhite);
    	Gotoxy(0, 2);
    	for ( int row( 1); row <= SIZEY; ++row)	//for each row (vertically)
    	{
    		for ( int col( 1); col <= SIZEX; ++col)	//for each column (horizontally)
    		{
    			cout << g[row][col];	//output cell content
    		} //end of col-loop
    		cout << '\n';
    	} //end of row-loop
    } //end of paintGrid
    
    
    void endProgram()
    {
    	SelectBackColour( clRed);
    	SelectTextColour( clYellow);
    	Gotoxy(40, 8);
    	//hold output screen until a keyboard key is hit
    	cout << "PRESS ANY KEY TO END PROGRAM!";
    	while (!_kbhit())  /* do nothing */;
    }

    header-ConsoleUtils.h:

    Code:
    /*
    **A simple library of console routines (from http://sunlightd.virtualave.net/Windows/FAQ.html) 
    ---------------------------------------
    ** Clrscr() - clear the screen, and reset cursor to (0, 0)
    ** Gotoxy(x, y) - move the text cursor to position (x, y)
    ** SelectBackColour(colour) - select a background colour from the colour constants list
    ** SelectTextColour(colour) - select a text colour from the colour constants list
    ** NOTE:Assume that whenever you write text to the screen the attributes will apply to all text
    ** sent to the screen from that point until the next point at which you select different text 
    ** or background colour, and output text with the new attributes.
    */
    
    #include <windows.h>
    
    
    
    Code:
    WORD backColour = 0;
    WORD textColour = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
    WORD textAttributes = backColour | textColour;
    
    //colour constants for translation
    const WORD clBlack = 0;
    const WORD clDarkRed = 1;
    const WORD clDarkGreen = 2;
    const WORD clDarkBlue = 3;
    const WORD clDarkCyan = 4;
    const WORD clDarkMagenta = 5;
    const WORD clDarkYellow = 6;
    const WORD clDarkGrey = 7;
    const WORD clGrey = 8;
    const WORD clRed = 9;
    const WORD clGreen = 10;
    const WORD clBlue = 11;
    const WORD clCyan = 12;
    const WORD clMagenta = 13;
    const WORD clYellow = 14;
    const WORD clWhite = 15;
    
    
    //-------------
    //clear the screen, and reset cursor to (0, 0)
    void Clrscr(void)
    {
    	//from web-site
    	COORD coordScreen = { 0, 0 }; 
    	DWORD cCharsWritten; 
    	CONSOLE_SCREEN_BUFFER_INFO csbi; 
    	DWORD dwConSize; 
    	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); 
    	GetConsoleScreenBufferInfo(hConsole, &csbi); 
    	dwConSize = csbi.dwSize.X * csbi.dwSize.Y; 
    	FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, 
    	coordScreen, &cCharsWritten); 
    	GetConsoleScreenBufferInfo(hConsole, &csbi); 
    	FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, 
    	coordScreen, &cCharsWritten); 
    	SetConsoleCursorPosition(hConsole, coordScreen); 
    }
    
    //-------------
    //move the text cursor to position (x, y)
    void Gotoxy(int x, int y)
    {
    	//from web-site
    	COORD coord;
    	coord.X = x;
    	coord.Y = y;
    	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    
    //-------------
    //select a background colour from the colour constants list
    void SelectBackColour(int colour)
    {
    	void SelectAttributes(void);
    	switch (colour)
    	{
    		case clBlack: backColour = 0; break;
    		case clDarkRed: backColour = BACKGROUND_RED; break;
    		case clDarkGreen: backColour = BACKGROUND_GREEN; break;
    		case clDarkBlue: backColour = BACKGROUND_BLUE; break;
    		case clDarkCyan: backColour = BACKGROUND_GREEN | BACKGROUND_BLUE; break;
    		case clDarkMagenta: backColour = BACKGROUND_RED | BACKGROUND_BLUE; break;
    		case clDarkYellow: backColour = BACKGROUND_RED | BACKGROUND_GREEN; break;
    		case clDarkGrey: backColour = BACKGROUND_INTENSITY; break;
    		case clGrey: backColour = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE; break;
    		case clRed: backColour = BACKGROUND_INTENSITY | BACKGROUND_RED; break;
    		case clGreen: backColour = BACKGROUND_INTENSITY | BACKGROUND_GREEN; break;
    		case clBlue: backColour = BACKGROUND_INTENSITY | BACKGROUND_BLUE; break;
    		case clCyan: backColour = BACKGROUND_INTENSITY | BACKGROUND_GREEN | BACKGROUND_BLUE; break;
    		case clMagenta: backColour = BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE; break;
    		case clYellow: backColour = BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN; break;
    		default: backColour = BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
    	}
    	SelectAttributes();
    }
    
    //-------------
    //select a text colour from the colour constants list
    void SelectTextColour(int colour)
    {
    	void SelectAttributes(void);
    	switch (colour)
    	{
    		case clBlack: textColour = 0; break;
    		case clDarkRed: textColour = FOREGROUND_RED; break;
    		case clDarkGreen: textColour = FOREGROUND_GREEN; break;
    		case clDarkBlue: textColour = FOREGROUND_BLUE; break;
    		case clDarkCyan: textColour = FOREGROUND_GREEN | FOREGROUND_BLUE; break;
    		case clDarkMagenta: textColour = FOREGROUND_RED | FOREGROUND_BLUE; break;
    		case clDarkYellow: textColour = FOREGROUND_RED | FOREGROUND_GREEN; break;
    		case clDarkGrey: textColour = FOREGROUND_INTENSITY; break;
    		case clGrey: textColour = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; break;
    		case clRed: textColour = FOREGROUND_INTENSITY | FOREGROUND_RED; break;
    		case clGreen: textColour = FOREGROUND_INTENSITY | FOREGROUND_GREEN; break;
    		case clBlue: textColour = FOREGROUND_INTENSITY | FOREGROUND_BLUE; break;
    		case clCyan: textColour = FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE; break;
    		case clMagenta: textColour = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE; break;
    		case clYellow: textColour = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN; break;
    		default: textColour = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
    	}
    	SelectAttributes();
    }
    
    //-------------
    //select the text attributes
    void SelectAttributes(void)
    {
    	textAttributes = backColour | textColour;
    	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), textAttributes);
    }
    
    //-------------
    //get the heigth of the screen (console window)
    int screenHeight(void)
    {
    	CONSOLE_SCREEN_BUFFER_INFO csbi;
    	GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
    	return csbi.srWindow.Bottom + 1;
    }
    
    //-------------
    //get the width of the screen (console window)
    int screenWidth(void)
    {
    	CONSOLE_SCREEN_BUFFER_INFO csbi;
    	GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
    	return csbi.srWindow.Right + 1;
    }
    header-RandomUtils.h: /* **A simple library of random numbers routines --------------------------------------------- ** Seed() - seed the random number generator from current system time ** Random( max) - produce a random number in range [1..max] */ #include <time.h> //for time used in random number routines #include <assert.h> //for assert //------------- //seeds the random number generator from current system time //must be called once (beginning of program) //so that the numbers generated by the Random function are different every time void Seed( void) { srand( (unsigned)time( NULL ) ); } //------------- //produces a random number in range [1..max] //max must be positive int Random( int max) { assert( max > 0); return ( rand() &#37; max) + 1; }
    Last edited by jojo13; 03-25-2008 at 05:12 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1. Don't put (big chunks of) code in header files.

    2. If you post a LOT of code, and no direct questions, you are unlikely to get much of a response, particularly with a rather unclear question of:
    I don't know how can mouse can push the apples.
    3. We do not do your homework.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    14

    Thumbs up

    Thanks mat
    I dont ask for doing my homework I just asked for help!!
    If you read my explaination I said I don't want any codes I just need suggestion about moving apple when mouse is pushing it.
    anyway thanks for responding I preffer to sort it out on my own Thank you.
    and by the way If I wanted to cheat I did not say it is my assignment, did I!?
    Last edited by jojo13; 03-25-2008 at 04:56 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The function moveMouse() looks like a good candidate for moving the mouse, don't you think.

    And when it comes to spotting assignments, we're pretty good at spotting the difference between "real work" and "school assignments" for most purposes. So even if you didn't say so, this post would have fallen into the "school assignment" in my mind anyways.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    14
    Thank you.
    I have never cheated and wont. the prob is that it is easter holiday and I cant ask for help from my tutor.If I could I did not post this thread.
    anyway thanks I think I have to work more on it and search for a guid about arrays on the internet.
    It was a mistake.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Btw, you're correct, my first thought would be the APPLE case statement in the moveMouse function. It implies that the square you're moving into contains an apple. So you'll need to find out which coordinates change (i.e. whether you're going up, down, left or right) and move the apple by one into that direction. Moving the apple should be just setting the value of the array to APPLE on the target square and to TUNNEL on the one the mouse is moving to. You'll have to check that you can move the apple though, since it could be sat next to a wall.
    Hope that helps,

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    14

    Thumbs up

    Thank for your help

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    14

    Wink

    I sort it out.no longer need help.Thanks

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21