Hola! usé min y max para mantener al Hero dentro del mapa y por ahi vi que alguien usó cls de stdlib que me pareció muy útil. Espero les sirva!
#include <iostream>
#include <stdlib.h>
using namespace std;
void DrawMap(int positionX, int positionY, char gameMap[3][5])
{
system("cls");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
if (i == positionY && j == positionX)
{
cout << 'H';
}
else
{
cout << gameMap[i][j];
}
}
cout << endl;
}
}
int main()
{
int heroPosX = 0;
int heroPosY = 2;
char map[3][5] =
{
{'1', '1', '1', '1', '1'},
{'1', '1', '1', '1', '1'},
{'1', '1', '1', '1', '1'}
};
char input = '\0';
cout << "Usa 'a' o 'd' para mover, y 'q' para salir." << endl;
do
{
cout << "Presiona una tecla: ";
cin >> input;
switch (input)
{
case 'd':
heroPosX = min(heroPosX + 1, 4);
break;
case 'a':
heroPosX = max(heroPosX - 1, 0);
break;
case 's':
heroPosY = min(heroPosY + 1, 2);
break;
case 'w':
heroPosY = max(heroPosY - 1, 0);
break;
default:
break;
}
DrawMap(heroPosX, heroPosY, map);
cout << endl;
} while (input != 'q');
return 0;
}
#include <iostream>
#include <stdlib.h>
using namespace std;
void DrawMap(int positionX, int positionY, char gameMap[3][5])
{
system("cls");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
if (i == positionY && j == positionX)
{
cout << 'H';
}
else
{
cout << gameMap[i][j];
}
}
cout << endl;
}
}
int main()
{
int heroPosX = 0;
int heroPosY = 2;
char map[3][5] =
{
{'1', '1', '1', '1', '1'},
{'1', '1', '1', '1', '1'},
{'1', '1', '1', '1', '1'}
};
char input = '\0';
cout << "Usa 'a' o 'd' para mover, y 'q' para salir." << endl;
do
{
cout << "Presiona una tecla: ";
cin >> input;
switch (input)
{
case 'd':
heroPosX = min(heroPosX + 1, 4);
break;
case 'a':
heroPosX = max(heroPosX - 1, 0);
break;
case 's':
heroPosY = min(heroPosY + 1, 2);
break;
case 'w':
heroPosY = max(heroPosY - 1, 0);
break;
default:
break;
}
DrawMap(heroPosX, heroPosY, map);
cout << endl;
} while (input != 'q');
return 0;
}