a little problem in my project

Ok, so I have this project I'm working on and I need help with something in one of the files:

Level.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <windows.h>

#ifndef LEVELS_H_INCLUDED
#define LEVELS_H_INCLUDED

bool levelOne();

#endif // LEVELS_H_INCLUDED 


Level.cpp

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
/**\brief This file will handle the levels of the game and the core parts of it.**/



#include "Levels.h"
#include "Enemy.h"

using namespace std;
extern bool stopGame;
extern int gameSpeed;




bool levelOne() {
    register char Map[10][21] =    {"####################", //Create the first map with two indexes
    /* # is the barrier or walls**/ "#<                 #",
                                    "#                  #",
                                    "#                  #",
                                    "#         P        #",/// P = Minion Enemy
                                    "#                  #",
                                    "#                  #",
                                    "#                  #",
                                    "#         x        #",///x = Player: Starting coordinates: (10,10)
                                    "####################"};



    while (stopGame == false) {
system("cls");

            ///RENDER THE MAP:

             for (int row = 0; row < 10; row++) {                //This iterates the first index (the rows)
            cout << "\t\t\t\t\t";                                           //This centers the map to the screen (or almost centers it -_-)
        for (int column = 0;column < 21; column++) {    //This iterates the second index (the columns)
            cout <<Map[row][column];                 //print the finished result
        }
        cout << endl;                                   //end the line after every row of the column has been sent to cout.
    }


        for (int y = 0; y < 10; y++) {               //This is for the switch statement so that the program will react to certain types /
            for (int x = 0;x < 21; x++) {            //of objects when we print the map.

                switch (Map[y][x])
                {
                    case 'x': {



                        if (GetAsyncKeyState(VK_UP)) {
                        int y2 = (y-1);            // This variable will hold the new increment of the moved player


                            switch (Map[y2][x])
                            {
                            case ' ':
                            {
                                Map[y][x] = ' ';
                                y -= 1;
                                Map[y2][x] = 'x';

                            }break;

                            }
                        }
                         if (GetAsyncKeyState (VK_DOWN))
                        {
                            int y2 = (y+1);             // This variable will hold the new increment of the moved player


                            switch (Map[y2][x])
                            {
                            case ' ':
                            {
                                Map[y][x] = ' ';
                                y += 1;
                                Map[y2][x] = 'x';

                            }break;

                            }
                        }
                         if (GetAsyncKeyState(VK_RIGHT))
                        {
                            int x2 = (x+1);            // This variable will hold the new increment of the moved player

                            switch (Map[y][x2])
                            {
                            case ' ':
                            {
                                Map[y][x] = ' ';
                                x += 1;
                                Map[y][x2] = 'x';
                            }break;

                            }
                        }
                         if (GetAsyncKeyState(VK_LEFT))
                        {
                            int x2 = (x-1);            // This variable will hold the new increment of the moved player

                            switch (Map[y][x2])
                            {
                            case ' ':
                            {
                                Map[y][x] = ' ';
                                x -= 1;
                                Map[y][x2] = 'x';
                            }break;

                            }
                        }
                    }break;
                            case 'P': {
                            int x2 = (x-1);
                            int x2r = (x+1);
                            switch (Map[y][x2])
                            {
                                case ' ': {
                                Map[y][x] = ' ';
                                x -= 1;
                                Map[y][x2] = 'P';
                                }break;
                                switch (Map[y][x2]) {
                                case '#': {
                                Map[y][x] = ' ';
                                x2r += 1;
                                Map[y][x2r] = 'P';

                                }
                                }




                            }

                            };


                    }
                }
            }
            Sleep(gameSpeed);
        }
system("pause");
return true;

    }



The problem is right here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
case 'P': {
                            int x2 = (x-1);
                            int x2r = (x+1);
                            switch (Map[y][x2])
                            {
                                case ' ': {
                                Map[y][x] = ' ';
                                x -= 1;
                                Map[y][x2] = 'P';
                                }break;

                                switch (Map[y][x2]) {
                                case '#': {
                                Map[y][x] = ' ';
                                x2r += 1;
                                Map[y][x2r] = 'P';

                                }
                                }


For some reason, When the enemy "P" moves and hits the "#", it stops moving without starting to move in a different direction. I want a linear pattern when this enemy hits the "#", so it will move left and right. How do I fix this?

Last edited on
closed account (10X9216C)
I think you should have more information than just a character. If i am understanding correctly, you want the monster to change direction when it hits a wall. That means you need something saying which direction it should go and well the data alone "P" can't do that.

You could create some sort of "Entity" class as per usual, and have a board of Entities instead.

Entity* level[10][20] = {};

Having nullptr be an empty space. Then you create a Monster class that derives Entity. If don't know what inheritance is or virtual functions, i would suggest you read up on that.

1
2
3
4
class Monster : public Entity
{
    bool movingLeft; // moving right if false
};


Your entity class would have some sort of "update" function. Really there are a couple ways to do this, this is just one way.
Why do you have a switch statement inside another switch statement? You do realize that that should never occur, because it is part of the case ' ' statement that breaks out early.
@NT3 Why should it not occur? How can I change that?

@myesolar Yes I know about inheritance and virtual functions, I will try that.
Last edited on
If you look at your code, this is what happens:

There is a switch on the character there. If that character is a space, it does things, and then meets the break statement. However, IF that break statement wasn't there, then it could continue on to the next switch, which only has the case of the character being a '#'. Do this instead:
1
2
3
4
5
6
7
8
9
switch (Map[y][x2]) {
    case ' ': {
        // ...
    } break;

    case '#': {
        // ...
    } break;
}
Thank you NT3, Going back to myesolars suggestion: Wouldnt

Entity* level[][];

make a 2d array of Entity objects? How can you display characters like '#'?
closed account (10X9216C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

class Entity
{
public:

    virtual char DisplayChar() const = 0;

};

class Monster : public Entity
{
public:

    char DisplayChar() const override { return 'P'; }

};

// loop through

    if(board[i][j]) std::cout << board[i][j]->DisplayChar();


or you could even just do this:

1
2
3
4
5
6
7
8
9
10
11
12
13

class Entity
{
public:
    char displayChar = 'd'; // default
};

class Monster : public Entity
{
public:
   Monster() { displayChar = 'P'; }
}
Last edited on
If i understand correctly, you create a pure virtual method in Entity and the Monster derivative of Entity has a method that overrides it. But, how does if(board[i][j]) work? Board is a pointer to an array of Entity objects, so how does it return a boolean value (0 or non zero) when the pointer does not point to an integer?
closed account (10X9216C)
board[i][j] returns a pointer, C++'s if statement basically checks if the value isn't zero, then it is considered true.

 
if(-1) std::cout << "always run cause always true";


This is really basic stuff, there is a tutorial on this site you can read to learn more about if statements.

It is equivalent to just doing, essentially.

 
if(board[i][j] != nullptr) { }
Last edited on
Thanks
Topic archived. No new replies allowed.