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
|
#include <iostream>
#include <vector>
#include <string>
#include <map>
void DirectionalError();
class Map;
//Can promote to a character class when i learn virtual, then inherit from there.
class Player {
public:
enum class Direction { UP = 1, DOWN = 2, LEFT = 3, RIGHT = 4 };
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 GetPlayer() const {
return mPlayer;
}
void Movement(Player::Direction, Map& m);
private:
const char mPlayer { 'O' };
size_t mPosX { };
size_t mPosY { };
};
class Map {
public:
Map(const std::string& mapName, size_t mapRows, size_t mapColumns, char mapTile) :
mMapName(mapName), mMapRows(mapRows), mMapColumns(mapColumns), mMapTile(mapTile) {}
//Comment this out for now so we don't mess up the working code while testing
void InitializeMap(const Player& p) {
mGameMap.assign(mMapRows, std::vector<char>(mMapColumns, mMapTile));
mGameMap[p.GetPositionY()][p.GetPositionX()] = p.GetPlayer();
}
void update(size_t oldx, size_t oldy, size_t newx, size_t newy, char player) {
mGameMap[oldy][oldx] = mMapTile;
mGameMap[newy][newx] = player;
}
void DrawMap() const {
for (const auto& cl : mGameMap) {
for (const auto& el : cl)
std::cout << el;
std::cout << '\n';
}
}
size_t MaxRows() const {
return mGameMap.size();
}
size_t MaxColumns() 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 { '+' };
std::vector<std::vector<char>> mGameMap;
};
void Player::Movement(Player::Direction choice, Map& map) {
switch (choice) {
case Direction::UP:
if (mPosY) {
const auto oldy { mPosY-- };
map.update(mPosX, oldy, mPosX, mPosY, mPlayer);
return;
}
break;
case Direction::DOWN:
if (mPosY < map.MaxRows() - 1) {
const auto oldy { mPosY++ };
map.update(mPosX, oldy, mPosX, mPosY, mPlayer);
return;
}
break;
case Direction::LEFT:
if (mPosX) {
const auto oldx { mPosX-- };
map.update(oldx, mPosY, mPosX, mPosY, mPlayer);
return;
}
break;
case Direction::RIGHT:
if (mPosX < map.MaxColumns() - 1) {
const auto oldx { mPosX++ };
map.update(oldx, mPosY, mPosX, mPosY, mPlayer);
return;
}
break;
default:
std::cout << "Invalid Input\n";
return;
}
DirectionalError();
}
void DirectionalError() {
std::cout << "Cannot go any further in this direction\n";
}
int main() {
Player Hero('O', 7, 5);
Map Field("Field", 20, 50, '-');
//Courtyard.InitializeMap();
Field.InitializeMap(Hero);
while (true) {
Field.DrawMap();
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";
std::cout << "Enter choice: ";
int choice { };
std::cin >> choice;
Hero.Movement(static_cast<Player::Direction>(choice), Field);
}
}
|