Sign in to follow this  

Basic Maze Game C++

This topic is 2552 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm a student and I've just started C++, in fact I've just began programming so this might feel to you, like trying to kill a cow with a soup spoon. I apologize profusely in advance.

I've been given an assignment to create a Maze game in C++ and while I know roughly how to do some of the things I have to, I'm having trouble working out how to link it all up. I've basically got the ingredients worked out but can't seem to come up with the recipe. So If you could help me piece this together I would be so grateful. Here's the specifications I've been given and I'll go through them one by one and tell you how I think I should be doing it. If you guys think I could do it a better way(but still sticking to the specs) then please tell me. Stick to helping me with one or two parts at a time please, I'm an absolute beginner and I'm easily confuzed at this stage.


SPECIFICATIONS

1. Create a data structure that will be used to store the maze data.

Okay a Multidimensional(2D) Array is what its meaning here. Yes?

2. Write a function to populate the data structure with data stored in a text file.

Read data from a Text file into game. I'm using the ifstream from the fstream header file. I've got the contents of the file displaying on screen here but its showing the last line twice and I've not put it in a 2d array yet. When you display it using cout.write does it put it in a normal(not 2d) array?

3.Write functions to:
Display the maze using standard or extended ASCII characters
Display the player-character at its current position.

So I've not to use any graphics just ASCII character. I'm okay with that. Now to display the player character do I write a function that takes the rows and columns of the array and puts it in the one co-ordinate function? Or am I talking crap?

4. Write code to accept user input that indicates how to move the character and then update the position to match this.

I've been told I can use getch() from conio.h BUT that means I can't use the cursor keys(doesn't it?) . So what can I use from the standard library?


5.Write a function that analyses a specific move, returns true if its possible, false if its not, so that the character can't move through walls. If the move returns false display an error message.

So i will have a for loop to check to see if the player has completed the game, otherwise check for inputs again, And inside that will be another for loop checking the position, if the array holds a 1 in that variable then the player can move there if not then loop again. I'm unsure about this, I have a feeling I'm not right here.

6.Write code routines to store all the moves made by a character since entering the maze and to replay the entire journey through the maze move by move.

Will this be writing a function that perhaps outputs to a text file, or creates a text file then saves every move which can later be then be read back in when the player requests it. So it will be something from fstream i'm looking to do this job. ofstream perhaps?


7.Create a command system to allow users to control a maze game. This should be menu based to provide commands to 1. Load a new maze from file. 2. Play the Game. 3. Replay last game. 4. QUIT.

Haven't really thought about this part but it seems simple enough.


additional marks for:
neatness of code, use of sensible names for variables, structs, constants etc, good use of constants, functions, data structures, users manual

LIBRARY SUPPORT
You may use the standard library code. If you wish to include any other code(eg. from an internet source) you will need to acknoledge it and indicate where it came from in the documentation. Failure to do so will cause us to assume you are passing the code off as your own, and can lead to disciplinary procedures against you.

I'll post up some code btw when I've got the way it has to be done. People have been telling me that the initial planning should be done before you begin coding. Want to get into the right mindset before I start on larger projects. Again thanks to all who help.

Share this post


Link to post
Share on other sites
I'm assuming your Text file consists of Walls and Hallways.

1.
Sounds good

2.
You can output it with cout however you want

3.

Might as well loop through each row, and display the columns as "#"," " and "x" based on Wall, Empty Space and Player.

Would look like this:

#######################
#### #############
#### # # ############
#### # # # # ###
#### # ############ ###
#### x ###
#######################


4.
Never used getch() before, but why not use WASD?

5.
Don't use a For loop for this. Use While or do while.

for example:

- While game hasn't ended
- Check User Input
- Process User Input
- If Q -> End Game
- If W
- Check if There is a Wall above, if not, move there, else, stay put
- If S
- Check if there is a Wall below....
- If A
.
.
.
- "Flush" the old Scene ( Clear the Screen )
- Redraw the Entire Maze
- Repeat ;)

6.

Yeah, you could use fstream for this. Another approach would be to use a dynamic array (perhaps a vector) and save all moves and output them to a file when the Game ends.

7.

Yeah ;)

Share this post


Link to post
Share on other sites
You assume correctly. walls, hallways, the player, a starting point and an end point which will be indicated by its own character, not sure what though.

3. I like how you display the maze. I forgot you could leave the space blank, I'm an absolute beginner :) I'll probably use that way of displaying walls, looks good. Although if I've got the time I was planning on using extended ASCII characters to make blocks. http://www.theasciicode.com.ar/ascii-table-codes/ascii-codes-219.html Don't know how to do it yet though.

As for using WASD it was just the way the lecturer was talking about it, I think there is a few marks in it if we can work out how to use the cursor keys. Although thats not essential so I'll probably add that if I've got the time.

Thanks for your help. It has made it clearer already. Hopefully there are others on here who can tell me about their way of doing it.

Share this post


Link to post
Share on other sites
Quote:
Original post by pat_stevenson
Although if I've got the time I was planning on using extended ASCII characters to make blocks. http://www.theasciicode.com.ar/ascii-table-codes/ascii-codes-219.html Don't know how to do it yet though.


It's very easy to use any ASCII character! What you do is set up a char variable and assign it the decimal value of your desired ASCII character. Then just print it out :) Like this:

/* char variable to hold the ascii character */
char asciiThing;

/* Assign it with the value of an ascii char */
asciiThing = 219;

/* Print it out! */
printf("%c", asciiThing);




As for using the arrow keys, it's pretty simple aswell. It's all about getting the correct ASCII values and check if they have been pressed :) You need to be carefull though, cause the ASCII values for key presses are the same as for some normal characters.

/* Key ASCII chars */
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77

char input;

/* Keep checking for input untill we press ESC */
while (input != 27) {
/* Store userinput in our input variable */
input = getch();

/* Check for arrow key presses */
switch (input) {
case KEY_UP:
printf("You've pressed KEY UP!\n");
break;

case KEY_DOWN:
printf("You've pressed KEY DOWN!\n");
break;

case KEY_LEFT:
printf("You've pressed KEY LEFT!\n");
break;

case KEY_RIGHT:
printf("You've pressed KEY RIGHT!\n");
break;
}
}




[Edited by - Zomgbie on November 30, 2010 2:46:54 AM]

Share this post


Link to post
Share on other sites
Zomgbie thanks I now understand how to print out ascii characters. But how do I put them in a 2d array, bearing in mind that I'm reading the values from a txt file outwith the program? So do I have to write a function that changes, for example a #(wall) to an ascii block character when printing to the screen?

Share this post


Link to post
Share on other sites
There is no need to change any characters at all! :) What you have in your file is exactly what will be placed into the 2D array. If you want the ascii block character (Decimal value 219) you just place it inside the file, easy as that!

Or well, the only thing that's not easy is getting that character into the file. For that, i suggest you write a function that generates the map, and save it to a text file.

Or you can use go ahead and replace. It might actually be easier :) Would probably look something like this:

char map[height][width];

/* reading your file stuff here... */
if (characterToRead == '#')
map[y][x] = 219;



Not sure i got your question right though. Was i close? :p

Share this post


Link to post
Share on other sites
Zomgbie, I'll try that when I get the basics sorted out. I'll probably go with the second option since it means I can either do it or leave it depending on whether I have the time. I'm still trying to get my head around the simplest of things at the moment, only been programming(in any language for about a month} so thanks for the help.

Share this post


Link to post
Share on other sites
No worries man, i love to help. Wrote an example function of how to load a map file into a 2D array, with some ASCII character replacing. I'll get it up here tomorrow, when i get back to work :>

[edit]
So this is what i did for loading a map into an array. The map file looks like this:

################################################################################
#X# # # # # # # # #
# # # # ### # # # ### # # # # # ##### # #
# # # # # # # # ######### # # #
##### # # # ### # ### # # # # ##### #
# # # # # # # # ##### # #
# ##### ### # ### ##### # ##### ### #
# # # # # # ### # #
### # # ##### ## # ## #### #### ###### #
# ##### # # # # # # # # #
# ### # # # ### ## # ############# ###### #
# # # ### ### # # # # # # # #
## #### # # # # ###### # # # # # ## # #
# # ### # ### # # # # # # # # # #
# ### ##### # # # # # # # # ## #
# # # # ### # ###### # ### ####### # #
# # ####### # # # # # # # # #
# # # # ########### # ## # ### ### # # #
# # # # # # # # # # ## ### #
# # ### ##### # # # ### #### # ### # # # #
# # # # # # # # # # # #
# ####### # # # # ### # # #### # # ##### # #
# # # # # # # #
################################################################################



And the code:

#define GAME_WIDTH 80
#define GAME_HEIGHT 24
#define WALL 178
#define PICKUP 254

int yIndex, xIndex, tile;
char gameField[GAME_HEIGHT][GAME_WIDTH];
FILE *level;

/* Starting at x and y 0 */
yIndex = 0;
xIndex = 0;

/* Load up the game file */
level = fopen("level.txt", "r+");

/*
* As long as our buffer is not pointing at the end of the
* file, we'll keep reading characters from the file.
*/

while (tile != EOF) {
/*
* Read the character where our internal file position
* indicator is, then move it to the next chararacter.
* fgetc() returns an int, but that's okey since we can
* assign decimal values to a char variable!
*/

tile = fgetc(level);

/*
* Check what value our tile has, and based on that
* we might want to do some specific things, like
* replacing it with an extended ASCII character!
*/

switch (tile) {
/* Wall tile, replace with a cool ascii char */
case '#':
tile = WALL;
break;

/* Pickup tile, replace with cool ascii char */
case '!':
tile = PICKUP;
break;

/* Player spawn position! */
case 'X':
playerX = xIndex;
playerY = yIndex;
break;

/* New line, increment the yIndex and reset xIndex */
case '\n':
yIndex++;
xIndex = 0;

/* Skip to the next iteration */
continue;
break;
}

/*
* This is where we add the character to our map array.
* The map array is an array of chars, and the tile now
* holds an decimal value. But once again, that's okey!
*/

gameField[yIndex][xIndex] = tile;

/*
* Now that we've added one tile to the map array
* we'll want to advance to the next x position.
*/

xIndex++;
}

/* Close the level file */
fclose(level);



[Edited by - Zomgbie on December 2, 2010 1:31:20 AM]

Share this post


Link to post
Share on other sites
pat_stevenson, this is a great first game example and one I done when I was in university.

As this is a game, you will need a game loop (EVERY game has one), you have probably written one and not realised but this is a very basic example of a tidy game loop:

GameDev - The Basic Game Loop

and here's how that might look in your game (Sorry not correct C++ syntax):


While(not EXIT)
{
getPlayerInput();
updateGameLogic();
updateDisplay();
}




At some point you will want to set a flag for breaking out of the loop. Maybe when you check getch() to see if esc was pressed?? just a hint

There are extra marks on offer for "neatness of code", so I think this at least covers that issue.

Hope this helps

Share this post


Link to post
Share on other sites
Hey guys, sorry its been a while, but its getting very close to the time where all my assignments for different classes have to be handed in so have been prioritizing on other things. Anyway thanks for all your help, I'm slowly getting the hang of it.


I've tried to take on board what everyone here has said, but still having a few problems. Would anyone mind having a look at my code so far and pointing out how to fix some of the things I've done wrong. I don't want to post up the full code in case someone steals it and passes it off as their own, as its for an assignment so would anyone mind me PMing them it to look over?


Thanks

Pat.

Share this post


Link to post
Share on other sites
I am doing something similar in java, does anyone know how to place a character in map. I can get the array to display but keep having problems with the movement around maze.

Share this post


Link to post
Share on other sites

This topic is 2552 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this