Trying to figure this problem out (arrays I think...)

So, for a project I have to make a logical maze, I have a txt file stored into a array set up like this

1
2
3
      37 W 45
      02 S 89
      -1 R 14


and so on (it fills a 10 x 10 array) the letter in the middle is there to be stored into a stack in move order, which will create a anagram of a password the player must enter to win the game.

The problem is when I come to the players choice, they can only go left or right, they always start at 0,0 in the array (which is set as 37 W 45) but unsure of what to do next to move the player to the respective choice (so if they choose left they would end up at row 3, col 7 and so on from there)

Issue is, I have no idea where to start with this after the Player Choice

This is what I have for the txt file array, I have it displaying as well just fine, so I do know its working
1
2
3
4
5
6
7
8
9
10
11
12
13
 void wordmaze(node maze[10][10])
{
	int row, col;//file>>array>>.left/right/data
	ifstream wordmaze;
		wordmaze.open("wordmaze.txt");
	for(row=0;row<10;row++)
		for(col=0;col<10;col++)
	{
		wordmaze>>maze[row][col].left;
		wordmaze>>maze[row][col].data;
		wordmaze>>maze[row][col].right;
	}
}



Thanks in advance for any help on this
Last edited on
Issue is, I have no idea where to start with this after the Player Choice
Neither do we from the information you've given.

Can you start at the beginning by describing what you're trying to do.
OK, this is the question in full:

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.
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.
Last edited on
Thank you

I'm starting to understand, if I show my code so far, I've started on it, some I don't find difficult, some I just don't get what I'm supposed to do or how to explain it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
using namespace std;


class node
{
	public:
		int left;
		char data;
		int right;
};

void wordmaze(node maze[10][10])
{
	int row, col;//file>>array>>.left/right/data
	ifstream wordmaze;
		wordmaze.open("wordmaze.txt");
	for(row=0;row<10;row++)
		for(col=0;col<10;col++)
	{
		wordmaze>>maze[row][col].left;
		wordmaze>>maze[row][col].data;
		wordmaze>>maze[row][col].right;
	}
}

/*void letterlist(char stack[20],int &tos)
{
	int row, col;
	node maze[10][10];
	wordmaze(maze);
 if (tos==-1)
	 cout<<"Stack overflow Error"<<endl;
 else 
	{	
		tos--;
		cin>>maze[row][col].data;
		cin>>stack[tos];
	}

}*/


void play()
{
	node maze[10][10];
	wordmaze(maze);
	int row, col, moves;
	char playerchoice, Left, Right;	

	cout<<"Please choose a direction: Left or Right:"<<endl;
		cin>>playerchoice;
	if(playerchoice==Left&&moves<20)
	{
		cout<<maze[row][col].left;
		moves++;
		cout<<moves;	
		do
		{
			for(row=0;row<10;row++)
				for(col=0;col<10;col++)
		}
		while();
		//move player to row/col on left
	}
	if(playerchoice==Right&&moves<20)
	{
		cout<<maze[row][col].right;
		moves++;
		cout<<moves;
		//pass maze.data to stack
		//move player to row,col on right
	}
}

void wordmazeshow()
{
	ifstream wordmaze("wordmaze.txt");
	int left;
	string letter;
	int right;

	while(wordmaze>>left>>letter>>right)
	{
		cout<<left<<"   "<<letter<<"   "<<right<<endl;
	}

}

void main()
{
	char option;
	char stack[20];
	int tos=-1;

  do
  {
	 cout<<endl<<endl<<endl;
	 cout<<"Menu"<<endl<<endl;
	 cout<<"1.  Play Game"<<endl;
	 cout<<"2.  Show Maze"<<endl;
	 cout<<"Q.  Quit system"<<endl<<endl;
	 cout<<"Please enter option choice :";
	 cin>>option;
	 cin.ignore();
	 switch (option)
	 {
		case '1': play();
				 break;
		case '2': wordmazeshow();
				 break;
		case 'q':cout<<"Quitting system"<<endl;
				 break;
		default: cout<<"you have entered an incorrect option choice please try again"<<endl<<endl;
	 }
  }
  while (option!='Q');


	system("pause");

}



I've not added anything to it from what you've told me so far, the class is what I've already done, that displays fine when testing, its the "player movement" that I'm really struggling with
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.



This part is where I'm at, so far I have this in class node:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class node
{
	public:
		int left;
		char data;
		int right;
};

void wordmaze(node maze[10][10])
{
	int row, col;//file>>array>>.left/right/data
	ifstream wordmaze;
		wordmaze.open("wordmaze.txt");
	for(row=0;row<10;row++)
		for(col=0;col<10;col++)
	{
		wordmaze>>maze[row][col].left;
		wordmaze>>maze[row][col].data;
		wordmaze>>maze[row][col].right;
	}
}


this is in it's own procudure for now for now, I realize I may need to add this to the class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
void play()
{
	node maze[10][10];
	wordmaze(maze);
	int row, col, moves, leftmove,rightmove, backtrack;
	char playerchoice, Left, Right;	
	char letterlist;

	cout<<"Please choose a direction: Left or Right:"<<endl;
		cin>>playerchoice;
	if(playerchoice==Left)
	{
		cout<<maze[row][col].left;
		moves++;
		cout<<moves;	
		do
		{
			for(row=0;row<10;row++)
				for(col=0;col<10;col++)
					leftmove=maze[row][col].left;
					leftmove=leftmove/10;
					leftmove=leftmove%10;
					maze[row][col].data=letterlist; 
					return;
		}
		while(moves!=20);
		//move player to row/col on left
	}
	if(playerchoice==Right)
	{
		cout<<maze[row][col].right;
		moves++;
		cout<<moves;
		//pass maze.data to stack
		//move player to row,col on right
	}
}
Last edited on
added new modulus to movement option
Topic archived. No new replies allowed.