Allow the user (marked by G in the example) to move either up, down, left, or right each turn. If the player steps on a trap then they lose. If the make it to the treasure 'X' then they win.
★★ Add enemies that move randomly in any direction once per turn. (enemies just like traps cause the player to lose if touched)
HINT: Don't let the player move off the gameboard! You program will crash if they move off the top or bottom of the board!
(the same holds true for enemies)
#include<iostream>
usingnamespace std;
void grid(int);
int main()
{
int c;
cout<<"press 0 to start"<<endl;
cin>>c;
grid(c);
return 0;
}
void grid(int c)
{
int a , b; //for movement initially 0
if(c == 0)
a = b = 0;
if(c == 5)
{a = 1; b =0;}
if(c == 1)
{a = -1; b =0;}
if(c == 2)
{a = 0; b =1;}
if(c == 3)
{a = 0; b = 1;}
for(int i = 0; i < 5; i++) //displaying grid
{
for(int j = 0; j < 5; j++)
{
if( (i==3 && j == 2) || (i == 0 && j==4))
cout<<"E ";
elseif( i==(0 + a) && j == (2 + b)) //adding a and b to move
cout<<"Y ";
elseif( (i==4 && j == 4))
cout<<"X ";
else
cout<<"_ ";
}
cout<<endl;
}
int p;
cout<<" press 5 (up), 1(left), 2(down) or 3(right) to move"<<endl;
cin>>p;
grid(p);
}
I saw this question on the site recently in beginner exercises. tried to solve it but it seems i don't know what i'm doing. there's answers to it but they looked more complex than it should be and the topic is closed. so can you at least give a hint for a simple solution?