Word Maze is a guessing game that involves the player trying to work their way from an entry point (0,0) to an exit (9,9) by guessing the direction they should head in at any time. The play occurs in a 10x10 grid and at each point in the grid the player is able to choose to go either left or right or backtrack to their previous position. As the player moves through the maze they collect letters. When the player gets to the exit square they are asked to enter a password. The password should be the letters they have collected as they moved through the maze. If the password is correct then the player wins. If the password is not correct or the number of moves a player makes exceeds 20 then the player loses. The maze may contain some loops (where players end up going through the same squares repeatedly) and dead ends - when the path they wish to follow is closed (this is indicated by a -1 for the direction of the path). In order to avoid cheating by just looking quickly at the text file moving left (or right) will not move the player physically onto the next left point on the grid but they will move logically using a left pointer (or right pointer). There are many incorrect solutions to the maze and only one correct one.
The text file wordmaze.txt contains the maze. Each row of the text file contains the contains of one square of the grid and has a number, a letter and a second number, for example: 27 H 4 - here the left pointer, 27, indicates that going left will take the player to row 2 column 7, the letter to collect is H and if the player goes right they will move to row 0 column 4. The grid is stored row by row – that is the first 10 rows of the text file will form the 10 squares of the first row of the maze. |
Cool. So, you haven't really started at the beginning. The start is the game loop. It's an important pattern as many interactive systems have such a loop.
1 2 3 4 5 6 7 8 9 10 11 12
|
const unsigned int max_move_count = 20;
int main()
{
unsigned int move_count = 0;
while (move_count < max_move_count)
{
// game logic
++move_count; // increment move count
}
}
|
I present a few examples. Don't worry if you don't understand the details, but the loop should become familiar.
A Windows message loop. See the commented section:
// Start the message loop.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms644928%28v=vs.85%29.aspx#creating_loop
Here's a BSD event loop.
https://wiki.netbsd.org/tutorials/kqueue_tutorial/#index3h2
Here's a discussion on game loops using SDL.
https://www.gamedev.net/topic/578007-proper-game-loop-in-sdl/
The next thing to consider is the word you create from the letters you collect. You should use a string to hold the word. So have a look at the documentation to see how to
add a letter and
remove that letter as you go forward and backward thru the maze.
http://en.cppreference.com/w/cpp/string/basic_string
Finally, navigation thru the maze. This seems pretty complicated. To stop you cheating by looking at the source data, that data is encrypted. As it involves a fair amount of processing, it's best to wrap that up in a class.
What do we want from the class? We want:
* The position.
* The letter.
What does the class need?
* The set of (left/letter/right) values from the file.
* The direction selected by the user (left/right/back). Left or right may be inaccessible if that coordinate is -1.
Anything else?
* The file stores the grid in row major order.
* The file numbers are 2-digit coordinates. So, 4 means (0, 4).
* A coordinate of -1 means that direction is inaccessible.
* The file holds 100 values, one for each coordinate on the grid.
You may ask, why bother with a class for that? It makes the main code logic very small and easy to very. All the complicated stuff is elsewhere. It can be fixed separately without impacting the rest of the program.
Is there any of this you don't understand? Please let us know, we can't go on without understanding each step. Also, let us know what string methods you've decided to use to add and remove a letter.