Dungeon Crawl Question

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)

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

#include<iostream>
using namespace std;

void grid(int);

int main()
{
   int c;
    cout<<"press 0 to start"<<endl;
    cin>>c;
    grid(c);

   return 0;
}

void grid(int c)
{
    int a , b; //for movement initially 0
     if(c == 0)
     a = b = 0;
      if(c == 5)
    {a = 1; b =0;}

      if(c == 1)
     {a = -1; b =0;}
      if(c == 2)
          {a = 0; b =1;}
      if(c == 3)
         {a = 0; b = 1;}


        for(int i = 0; i < 5; i++) //displaying grid
    {
        for(int j = 0; j < 5; j++)
            {
                 if( (i==3 && j == 2) || (i == 0 && j==4))
                cout<<"E ";
                else if( i==(0 + a) && j == (2 + b)) //adding a and b to move
                cout<<"Y ";
                else if( (i==4 && j == 4))
                cout<<"X ";
                else
                cout<<"_ ";
            }
        cout<<endl;
    }
     int p;
    cout<<" press 5 (up), 1(left), 2(down) or 3(right) to move"<<endl;
    cin>>p;
    grid(p);


}



I saw this question on the site recently in beginner exercises. tried to solve it but it seems i don't know what i'm doing. there's answers to it but they looked more complex than it should be and the topic is closed. so can you at least give a hint for a simple solution?
create a 2d array:
1
2
3
4
5
6
char map[5][5] = //5x 5grid
{{ 0 , 0, 0 , 0 ,0 } , 
{ 0 , 0, 0 , 0 ,0 } , 
{ 0 , 'T', 0 , 0 ,0 } , 
{ 0 , 0, 0 , 0 ,'X' } , 
{ 0 , 0, 'T' , 0 ,0 } };


and create a player:
1
2
3
4
5
6
7
typedef struct
{
  int X;//location horizontal
  int Y;//olcation vertical
}Player;

Player player = {0,0};


when a key is pressed (any key you want) increase/decrease the Y or the X veriable inside the player:
1
2
//example
player.X++;


just compare the location of the player with the grid:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch(map[player.X][player.Y] )
{
case 0:
//nothing
break;
case 'X':
//you win
break;
case 'T':
//you die
break;
default:
//something unkknown happened
break;
}


and print the map and on the location of the player just print a 'G'.. or print the map before checking... whatever you want..
Last edited on
Topic archived. No new replies allowed.