Dungeon Crawler

Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
arrays

Make a program that outputs a simple grid based gameboard to the screen using either numbers or characters.
i.e.

. . . . . . . . . .
. G . . . . . . . .
. . . . . . T . . .
. . . . . . . . . .
. . . . T . . . . .
. . . . . . T . . .
. . . . . . . . . X


or

0 0 0 0 0 0 0 0 0 0
0 5 0 0 6 0 0 0 0 0
0 0 0 0 0 0 7 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 7 0 0 0 0 0 0
0 0 0 0 0 0 7 0 0 0
0 0 0 0 0 0 0 0 0 4


Allow the user (marked by G in the example) to move either up, down, left, or right each turn. If the player steps on a trap then they lose. If the make it to the treasure 'X' then they win.

★★ Add enemies that move randomly in any direction once per turn. (enemies just like traps cause the player to lose if touched)

HINT: Don't let the player move off the gameboard! You program will crash if they move off the top or bottom of the board!
(the same holds true for enemies

Above, is the prompt for the game I am programming. I am having trouble making the grid show up as 7 rows by 10 columns. The appears as a really long linear line when I debug it. Also, I want to make sure I am setting the traps, player position, and goal correctly. If its an 2 demensional array, the first row is 0, the second row is 1, etc.. , so is it correct to set the player's beginning position to 0,0 since its the first row and column?
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
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

/*void gridIt (int rows, int columns, char myGrid[7][10]) 
{
	for (rows = 0; rows < 7; ++rows)
	{
		for (columns = 0; columns < 10; ++columns)
			cout << myGrid[rows][columns] << endl;
	}
}*/

int _tmain(int argc, _TCHAR* argv[])
{
	string imput;
	char player = 'G', trap = 'T', goal = 'X';
	char grid[7][10];
	int row, col, posX = 0, posY = 0, skip = 0;
	
	for (row = 0; row < 7; row++)  // creates the grid 
	{
		for (col = 0; col < 10; col++)
			grid[row][col] = '.';
	}

	grid[posX][posY] = player; //Gives player's beginning position 
	grid[2][3] = trap; //Gives trap positions
	grid[3][6] = trap;
	grid[4][9] = trap;
	grid[5][4] = trap;
	grid[6][8] = trap;
	grid[6][9] = goal; //Gives finishing position

	for (row = 0; row < 7; row++)  // Shows grid
	{
		for (col = 0; col < 10; col++)
			cout << grid[row][col];
	}

	//void gridIt (int rows, int columns, char myGrid[7][10]); 

	while (grid[6][9] != player)
	{
		cout << "In which direction do you want to move?\n";
			cout << "up, down, left, right, or quit\n";
			cin >> imput;
		do
		{
			if(imput != "up" || imput != "down" || imput != "left" || imput != "right" || imput != "quit") //Error catching if user doesn't imput valid move
			{
				cout << "Please enter a valid move: up, down, left, right, quit\n";

			}
		}while(imput != "up" || imput != "down" || imput != "left" || imput != "right" || imput != "quit");

		if(imput == "up")
		{
			for ( col = 0; col < 10; col++)
			{
				if(grid[0][0,1,2,3,4,5,6,7,8,9] == player) //gives parameters or bounderies of grid
				{
					cout << "You cannot move up from that position\n";
					skip = 1;
				}
				if(skip == 0) 
				{
					grid[posY][posX] = '.';
					grid[posY - 1][posX] = player; //re-locates player's position if move is valid
				}
			}
		}
		
		if(imput == "down")
		{
			for ( col = 0; col < 10; col++)
			{
				if(grid[6][0,1,2,3,4,5,6,7,8,9] == player) //gives parameters or bounderies of grid
				{
					cout << "You cannot move down from that position\n";
					skip = 1;
				}
				if(skip == 0)
				{
					grid[posY][posX] = '.';
					grid[posY + 1][posX] == player; //re-locates player's position if move is valid
				}
			}
		}

		if(imput == "left")
		{
			for ( row = 0; row < 7; row++)
			{
				if(grid[0,1,2,3,4,5,6][0] == player) //gives parameters or bounderies of grid
				{
					cout << "You cannot move left from that position\n";
					skip = 1;
				}
				if(skip == 0)
				{
					grid[posY][posX] = '.';
					grid[posY][posX - 1] = player; //re-locates player's position if move is valid
				}
			}
		}

		if(imput == "right")
		{
			for ( row = 0; row < 7; row++)
			{
				if(grid[0,1,2,3,4,5,6][9] == player) //gives parameters or bounderies of grid
				{
					cout << "You cannot move right from that position\n";
					skip = 1;
				}
				if(skip == 0)
				{
					grid[posY][posX] = '.';
					grid[posY][posX + 1] = player; //re-locates player's position if move is valid
				}
			}
		}

		if(imput == "quit") //Quits game if player chooses this command
		{
			break;
		}

		//The following is what happens if the player lands on one of the traps
		if(grid[2][3] == player || grid[3][6] == player || grid[4][9] == player || grid[5][4] == player || grid[6][8] == player)
		{
			cout << "It's a trap!\n";
			break;
		}

	for (row = 0; row < 7; row++)  // re-shows the grid 
	{
		for (col = 0; col < 10; col++)
			cout << grid[row][col];
	}

	skip = 0;

	if(grid[6][9] == player) //What happens if player successfully makes it to the goal
	{
		cout << "You successfully escaped the dungeon!\n";
	}
}

	return 0;
}


You just need to add a newline into your grid drawing.

1
2
3
4
5
6
7
for( row = 0; row < 7; row++ )
{
   for( col = 0; col < 10; col++ )
      cout << grid[row][col];

   cout << "\n";
}


I would advise using constants for your maximum row and column values too.
so is it correct to set the player's beginning position to 0,0 since its the first row and column?
Yes it is.

This

if(grid[0,1,2,3,4,5,6][0] == player)

does not what you think it does. it's equivalent to

if(grid[6][0] == player)
@iHutch105

Where do I put the "const" prefix to make the maximum row and column values constants? After I make them constants, does that mean I don't have to keep repeating the "for-loops" for maximum rows and columns?

1
2
for (row = 0; row < 7; row++)
for (col = 0; col < 10; col++)
There are two conventions for this. Either you can use a preprocessor directive to define the value or you can make the object a constant when declaring. I prefer the latter, but here's how to do both.

Preprocessor:
1
2
3
4
5
6
7
8
#define NUM_ROWS 7
#define NUM_COLS 10

// Then, in your loops...
for ( row = 0; row < NUM_ROWS; row++ )
{
   // Do stuff
}


Constants:
1
2
3
4
5
6
7
8
const int NUM_ROWS = 7;
const int NUM_COLS = 10;

// Then, in your loops...
for ( row = 0; row < NUM_ROWS; row++ )
{
   // Do stuff
}


Note, the upper case style is merely convention, though a lot of people follow it.

Kovs95 wrote:
After I make them constants, does that mean I don't have to keep repeating the "for-loops" for maximum rows and columns?

No, you'll still have to repeat the loops, but it's better practice to do it this way. Consider this; what if someone came along and said "Hey, you know what'd be great? If the grid had 16 rows and 24 columns. Can you change it, please?". Given your current method, you'd have to find every loop in your code and change those hard-coded numerics. If you use one of the two methods listed above, it becomes a two line code change.

Hope this helps.
Topic archived. No new replies allowed.