Console game missues

Im trying to implement some of the stuff i learned into a program and I am having an issue getting it to work correctly for some reason. I put the issues at the top of the program, everything is in one file for now to make it easier to post questions.

I tried stepping through it with the debugger, and i was able to solve a few issues using it, but i cant seem to figure these two out.

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
162
#include <iostream>
#include <vector>

/*ISSUES
//
// 1. Map cannot have different size values, x cannot b 10 and y be 15 for example, or else it will crash
// 2. Player leaves a trail when going up for some reason
*/

void DirectionalError();

class Map
{
    public:
        Map
        (
            const std::string& mapName, 
            int mapRows, 
            int mapColumns, 
            char mapTile
        ): 
            mMapName(mapName), 
            mMapRows(mapRows), 
            mMapColumns(mapColumns), 
            mMapTile(mapTile)
        {}

        void InitializeMap()
        {
            for (int row = 0; row < mMapRows; row++)
            {
                std::vector<char> v1;

                for (int col = 0; col < mMapColumns; col++)
                {
                    v1.push_back(mMapTile);
                }

                mGameMap.push_back(v1);
            }
        }

        void DrawMap(int posX, int posY, char player)
        {
            if (posX >= mGameMap.size() || posY >= mGameMap.size())
            {
                std::cout << "Critical error drawing map! Map size is too small, please resize it!\n\n";
            }
            else 
            {
                for (int row = 0; row < mMapRows; ++row)
                {
                    for (int col = 0; col < mMapColumns; ++col)
                    {
                        mGameMap[col][row] = mMapTile;
                        mGameMap[posX][posY] = player;

                        std::cout << mGameMap[row][col];
                    }
                    std::cout << '\n';
                }
            }
        }

    private:
        std::string mMapName{ "Map Name" };
        int mMapRows{ 5 };
        int mMapColumns{ 5 };
        const char mMapTile{ '+' };
        const char mMapTransition{ 'Z' }; //UNUSED: Transition from one map to another whn player touches this
        std::vector<std::vector<char>> mGameMap;
};

//Can promote to a character class when i learn virtual, then inherit from there.
class Player
{
    public:
        Player(char player, int posX, int posY): mPlayer(player), mPosX(posX), mPosY(posY)
        {}

        int GetPositionX() const
        {
            return mPosX;
        }

        int GetPositionY() const
        {
            return mPosY;
        }

        char GetPlayerSprite() const
        {
            return mPlayer;
        }

        int Movement(int choice);

    private:
        const char mPlayer{ 'O' };
        int mPosX{ 0 };
        int mPosY{ 0 };
};

int main()
{
    Player Hero('O', 5, 5);
    Map Courtyard("Courtyard", 20, 20, '-');

    Courtyard.InitializeMap();

    while (true)
    {
        Courtyard.DrawMap(Hero.GetPositionX(), Hero.GetPositionY(), Hero.GetPlayerSprite());

        std::cout << "X: " << Hero.GetPositionX() << " Y: " << Hero.GetPositionY() << "\n\n";

        std::cout << "What do you want to do?\n\n";

        std::cout << "1) Move Up\n";
        std::cout << "2) Move Down\n";
        std::cout << "3) Move Left\n";
        std::cout << "4) Move Right\n";

        int choice{ 0 };
        std::cin >> choice;

        Hero.Movement(choice);
    }
}

//No error Handling Yet.
int Player::Movement(int choice)
{
    enum class Direction {UP = 1, DOWN = 2, LEFT = 3, RIGHT = 4};

    switch (choice)
    {
        case static_cast<int>(Direction::UP):
                return mPosX--;
            break;

        case static_cast<int>(Direction::DOWN):
                return mPosX++;
            break;

        case static_cast<int>(Direction::LEFT):
                return mPosY--;
            break;

        case static_cast<int>(Direction::RIGHT):
                return mPosY++;
            break;

        default:
            std::cout << "Invalid Input\n";
    }
}

void DirectionalError()
{
    std::cout << "Cannot go any further in this direction\n";
}
Last edited on
> if (posX >= mGameMap.size() || posY >= mGameMap.size())
1. You should be checking against mapRows and mapColumns. This test assumes the map is square.
2. You also need to check for <0 in each direction, otherwise you step off the grid.

> mGameMap[col][row] = mMapTile;
> mGameMap[posX][posY] = player;
You need some sense of where the player was previously, in order to restore the map with mGameMap[col][row] = mMapTile;

Maybe
mGameMap[oldX][OldY] = mMapTile;
mGameMap[posX][posY] = player;


Be careful about which way round the indices are
rows increase in the Y axis
cols increase in the X axis


This should be in a separate function called drawMap()
> std::cout << mGameMap[row][col];
You're confusing x, y row and col. Row is y and col is x. As a first refactor consider something like:

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

void DirectionalError();

class Map {
public:
	Map ( const std::string& mapName, size_t mapRows, size_t mapColumns, char mapTile ) :
		mMapName(mapName), mMapRows(mapRows), mMapColumns(mapColumns), mMapTile(mapTile) {}

	void InitializeMap() {
		mGameMap.assign(mMapRows, std::vector<char>(mMapColumns, mMapTile));
	}

	void DrawMap(size_t posX, size_t posY, char player) {
		for (size_t row {}; row < mMapRows; ++row) {
			for (size_t col {}; col < mMapColumns; ++col) {
				mGameMap[row][col] = mMapTile;
				mGameMap[posY][posX] = player;

				std::cout << mGameMap[row][col];
			}
			std::cout << '\n';
		}
	}

	size_t maxRow() const {
		return mGameMap.size();
	}

	size_t maxCol() const {
		return !mGameMap.empty() ? mGameMap[0].size() : 0;
	}

private:
	const std::string mMapName { "Map Name" };
	size_t mMapRows { 5 };
	size_t mMapColumns { 5 };
	const char mMapTile { '+' };
	const char mMapTransition { 'Z' }; //UNUSED: Transition from one map to another whn player touches this
	std::vector<std::vector<char>> mGameMap;
};

//Can promote to a character class when i learn virtual, then inherit from there.
class Player {
public:
	Player(char player, size_t posX, size_t posY) : mPlayer(player), mPosX(posX), mPosY(posY) {}

	size_t GetPositionX() const {
		return mPosX;
	}

	size_t GetPositionY() const {
		return mPosY;
	}

	char GetPlayerSprite() const {
		return mPlayer;
	}

	void Movement(int choice, Map);

private:
	const char mPlayer { 'O' };
	size_t mPosX { };
	size_t mPosY { };
};

int main() {
	Player Hero('O', 7, 5);
	Map Courtyard("Courtyard", 25, 20, '-');

	Courtyard.InitializeMap();

	while (true) {
		Courtyard.DrawMap(Hero.GetPositionX(), Hero.GetPositionY(), Hero.GetPlayerSprite());

		std::cout << "X: " << Hero.GetPositionX() << " Y: " << Hero.GetPositionY() << "\n\n";

		std::cout << "What do you want to do?\n\n";

		std::cout << "1) Move Up\n";
		std::cout << "2) Move Down\n";
		std::cout << "3) Move Left\n";
		std::cout << "4) Move Right\n";

		int choice { };

		std::cin >> choice;
		Hero.Movement(choice, Courtyard);
	}
}

//No error Handling Yet.
void Player::Movement(int choice, Map map) {
	enum class Direction {
		UP = 1, DOWN = 2, LEFT = 3, RIGHT = 4
	};

	switch (choice) {
		case static_cast<int>(Direction::UP):
			if (mPosY) {
				--mPosY;
				return;
			}
			break;

		case static_cast<int>(Direction::DOWN):
			if (mPosY < map.maxRow() - 1) {
				++mPosY;
				return;
			}
			break;

		case static_cast<int>(Direction::LEFT):
			if (mPosX) {
				--mPosX;
				return;
			}
			break;

		case static_cast<int>(Direction::RIGHT):
			if (mPosX < map.maxCol() - 1) {
				++mPosX;
				return;
			}
			break;

		default:
			std::cout << "Invalid Input\n";
			return;
	}

	DirectionalError();
}

void DirectionalError() {
	std::cout << "Cannot go any further in this direction\n";
}

Last edited on
Ok thank you so much, that helped!
Registered users can post here. Sign in or register to post.