Most ridiculous bool condition ever?(member array indexing)

Hi all

This program is just me experimenting/trying stuff for the sole purpose of learning. I fully intend to move to SFML later when I'm actually able to write some half decent code. The single file is intentional at this stage.

All I wanted here is a very simple program that gives the player a list of numbered choices of where to go, and locations where each one takes you. The player chooses and goes there.

I have deliberately not used the traditional N,S,E,W method and tile map, as eventually I wanted to try out different map layouts etc, but mostly it's just me experimenting. I'm going to change the whole thing to use more classes, and eventually try using states.

I am happy to hear suggestions of better ways to do this whole program, but would still like this one thing solving:

The intention was to have a 'path' between rooms that could be visible or invisible, and while not visible the program wouldn't list it as an option. I uh, got a bit carried away with trying to get the member array index right, and ended up with a hilariously long condition in the if statement at around line 143. It *nearly* works, but I just thought I'd better stop before I went nuts.

I want the options always numbered 1, 2, 3 etc, and not just to choose the room number, and to get at the bool member pathVisible[] that corresponds to the choice. This is complicated by the nested arrays that store data.

Here's the code. I'm using Visual Studio 17/2019? Whatever the latest one is...

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#include <string>

class Room
{
private:

public:
	std::string name;
	int index;
	std::string text;
	int openExits;
	int nextRoom[10];
	bool pathVisible[10];
};

enum TheRooms { 
	ROOM_START, 
	ROOM_CORRIDOR, 
	ROOM_W_CELL, 
	ROOM_E_CELL, 
	ROOM_STAIRS,
	ROOM_HALL, 
	ROOM_COURTYARD, };

// create the room data
Room world[] =
{
	{// Room 0
		"Prison Cell",
		ROOM_START,
		"You are in a Prison Cell",
		1,		
		{ROOM_CORRIDOR},
		{true}

	},
	{	// Room 1
		"The Corridor",
		ROOM_CORRIDOR,
		"\nYou are in the corridor.\n",
		4,
		{ROOM_W_CELL, ROOM_E_CELL, ROOM_STAIRS, ROOM_START}, 
		{false, true, true, true}
	},
	{	// Room 2
		"The West Cell",
		ROOM_W_CELL,
		"The West Cell.\n",
		1,
		{ROOM_CORRIDOR},
		{true}
	},
	{	// Room 3
		"The East Cell",
		ROOM_E_CELL,
		"The East Cell.\n",
		1,
		{ ROOM_CORRIDOR },
		{true}
	},
	{	// Room 4
		"The Stairs",
		ROOM_STAIRS,
		"You are on the stairs leading up to the Courtyard.\n",
		2,
		{ROOM_HALL, ROOM_CORRIDOR},
		{true, true}
	},
	{	// Room 5
		"The Asylum Hall",
		ROOM_HALL,
		"You are in The Great Hall of The Asylum\n",
		2,
		{ROOM_COURTYARD, ROOM_STAIRS},
		{true, true}
	},
	{
		// Room 6
		"The Courtyard",
		ROOM_COURTYARD,
		"You are in the Asylum Courtyard\n",
		1,
		{ROOM_HALL},
		{true}
	}

};

void clearScreen();

int doRoom(int room);

int main()
{
	bool alive = true;
	while (alive)
	{
		int currentRoom = ROOM_START;
		while (currentRoom >= 0)			
		{
			currentRoom = doRoom(currentRoom);
		}
	}
	std::cout << "I do believe you just died.";

	return 0;
}

void clearScreen()
{
	std::cout << std::endl;
	system("cls");
}

int doRoom(int room)
{
	std::cout << world[room].text;

	if (world[room].openExits < 0)	
	{
		return -1;
	}

	bool valid = false;
	int selection = 0;
	while (!valid)
	{
		//clearScreen();

		std::cout << "\nYour choices are:\n\n";

		for (int i = 0; i < world[room].openExits; ++i)
		{
			std::cout << i + 1 << ". " << world[world[room].nextRoom[i]].name << "\n";
		}

		std::cin >> selection;

		if(world[world[room].nextRoom[selection]].pathVisible[world[world[room].nextRoom[selection]].index] == true)	// :D
		{
			if (selection<1 || selection>world[room].openExits)
			{
				std::cout << "There is no path that way. Unless you like banging\n"
					"your head against walls.\n\n" << std::endl;
				std::cin.ignore(32767, '\n');
			}
			else
			{
				valid = true;
			}
		}
		else
		{
			std::cout << "(Path exists, but not visible)\n" << std::endl;
			std::cin.ignore(32767, '\n');
			//std::cin.get();
		}
	}
	return world[room].nextRoom[selection - 1];		
}
Last edited on
FWIW - a completely random set of directions - so no guarantee of escape or even visiting each location even once.

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
#include <iostream>
#include <fstream>

void show_location(std::string[], int);
int select_direction(int[][6], int);

const int MAX_ROOMS{100};

int main()
{
    int room_no[MAX_ROOMS]{0};
    std::string room_name[MAX_ROOMS];
    
    int direction[MAX_ROOMS][6]{0};
    int COUNT{0};
    
    std::ifstream myfile ("game.txt");
    if (myfile.is_open())
    {
        while ( myfile >> room_no[COUNT])
        {
            for(int i = 0; i < 6; i++)
            {
                myfile >> direction[COUNT][i];
            }
            getline(myfile,room_name[COUNT]);
            
            COUNT++;
        }
        
        myfile.close();
    }
    else
    {
        std::cout << "Unable to open file";
        return -1;
    }
    
    // START
    int current_place{0};
    
    while(true)
    {
        show_location(room_name, current_place );
        current_place = select_direction(direction, current_place);
    }
    return 0;
}

void show_location(std::string r_name[], int r_no)
{
    std::cout << "You are at the" << r_name[r_no] << '\n';
}

int select_direction(int dir[][6], int loc_no )
{
    int way{0};
    std::cout << "You can go in the following directions:\n";
    
    for(int i = 0; i < 6; i++)
    {
        if(dir[loc_no][i] > 0)
            std::cout << i << ' ';
    }
    std::cout << '\n';
    
    std::cout << "Enter selection: ";
    std::cin >> way;
    std::cout << '\n';
    
    return way;
}


game.txt

0 0 1 1 2 0 0 Prison Cell
1 0 1 0 2 0 0 The Corridor
2 0 2 0 0 0 0 The West Cell
3 5 0 1 0 0 0 The East Cell
4 0 0 0 0 5 6 The Stairs
5 0 2 0 1 0 4 The Asylum Hall
6 1 0 5 4 2 0 The Courtyard


You are at the Prison Cell
You can go in the following directions:
1 2 3 
Enter selection: 2

You are at the The West Cell
You can go in the following directions:
1 
Enter selection: 1

You are at the The Corridor
You can go in the following directions:
1 3 
Enter selection: 1

You are at the The Corridor
You can go in the following directions:
1 3 
Enter selection: 3
Well that's a random way of navigating an area, but ok! I want to keep a definite map, but something like this might be useful somewhere.

I really do need to start reading data in rather than having it in the code...
The route mapping in this case wa random but a structure way through and around is just a matter of choosing the directions in the list
Topic archived. No new replies allowed.