Code:
#include <iostream>
#include <vector>
using std::cout;
using std::cin;
using std::vector;
int main()
{
enum { Player='O', MapTile='*', Boulder='@' };
const int Rows{10}, Cols{16};
const int BoulderRow{6}, BoulderCol{6};
vector<vector<char>> map(Rows, vector<char>(Cols, MapTile));
// Initialize map.
for (int r = 0; r < Rows; ++r)
for (int c = 0; c < Cols; ++c)
map[r][c] = MapTile;
map[row][col] = Player;
map[BoulderRow][BoulderCol] = Boulder;
int row{3}, col{3}; // Player position.
bool endGame = false;
while (!endGame)
{
// Draw map.
for (int r = 0; r < Rows; ++r)
{
for (int c = 0; c < Cols; ++c)
cout << map[r][c] << ' ';
cout << '\n';
}
cout << "Player (Row,Col): " << row << ' ' << col << '\n';
cout << "w) UP s) DOWN a) LEFT d) RIGHT q) QUIT\n";
char choice {};
cin >> choice;
// Erase player.
map[row][col] = MapTile;
switch (choice)
{
case 'w':
if (row == 0 || map[row - 1][col] == Boulder)
cout << "Cannot move Up.\n";
else
--row;
break;
case 's':
if (row == Rows - 1 || map[row + 1][col] == Boulder)
cout << "Cannot move Down.\n";
else
++row;
break;
case 'a':
if (col == 0 || map[row][col - 1] == Boulder)
cout << "Cannot move Left.\n";
else
--col;
break;
case 'd':
if (col == Cols - 1 || map[row][col + 1] == Boulder)
cout << "Cannot move Right.\n";
else
++col;
break;
case 'q':
endGame = true;
break;
default:
cout << "Please enter w, s, a, d, or q.\n";
}
// Redraw player.
map[row][col] = Player;
}
}