[Maze game by uisng c++] Hello! First of all, thank you for helping my stuff. Ac
ID: 3748871 • Letter: #
Question
[Maze game by uisng c++]
Hello! First of all, thank you for helping my stuff.
Actually, I try to make a maze game in using c++.
I have some problems in my code.
["I have to keep my format that function conditions void create_maze(int &row_robot, int &column_robot );
void show_maze(int row_robot, int column_robot);
bool robot_move(int &row_robot, int &column_robot); ]
1) In bool robot_move(int &row_robot, int &column_robot) function, the random number is not woring as well. Do you know how to solve this problem?
1) In bool robot_move(int &row_robot, int &column_robot) function, I made a random number but it is not working as well. I will attached my coding result below. As you can see, the Random number is always same.
2) When I run it, I only want to keep only one map, but it showed lots of maps.
3) The robot("X") delete the wall("+") even I made a if condition in bool robot_move() function.
Could you help me how to deal with those issues? I will paste my code below and the result image.
/Users/jeonj iwan/CLionProjects/CIS25/cmake-build-debug/CIS25 RANDOM NUMBER: 3 How many moves: 0 RANDOM NUMBER: 3 How many moves: 1 RANDOM NUMBER: 3 How many moves: 2 RANDOM NUMBER: 3 How many moves: 3 RANDOM NUMBER: 3Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <time.h>
#include <random>
#include <stdlib.h> // system("cls");
using namespace std;
void create_maze(int &row_robot, int &column_robot );
void show_maze(int row_robot, int column_robot);
bool robot_move(int &row_robot, int &column_robot);
const int SIZE = 10;
enum Values {space, wall, e, robot};
Values maze[SIZE][SIZE];
int main()
{
srand(time(0));
int row_robot, column_robot, moving;
bool temp = true;
row_robot = 4;
column_robot = 4;
moving = -1;
create_maze(row_robot, column_robot);
show_maze(row_robot, column_robot);
while (temp == true)
{
++moving;
if (maze[row_robot][column_robot] != wall)
{
robot_move(row_robot, column_robot);
usleep(1000);
show_maze(row_robot, column_robot);
cout << "How many moves: " << moving << endl;
}
else if (maze[row_robot][column_robot] == e)
{
cout << "Your Robot is survived";
cout << "How many moves: " << moving << endl;
temp = false;
break;
}
}
// maze[row_robot][column_robot] = robot;//
}
void create_maze(int &row_robot, int &column_robot)
{
int i, j;
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
maze[i][j] = wall;
}
}
for (i = 1; i < SIZE; i += 3)
{
for (j = 0; j < SIZE; j++)
{
maze[i][j] = space;
}
}
maze[1][0] = e;
maze[7][9] = e;
maze[1][9] = wall;
maze[4][0] = wall;
maze[4][8] = wall;
maze[4][9] = wall;
maze[7][0] = wall;
maze[2][2] = space;
maze[2][6] = space;
maze[2][8] = space;
maze[3][2] = space;
maze[5][7] = space;
maze[6][3] = space;
maze[6][7] = space;
maze[row_robot][column_robot] = robot; // Robot describe as 'X'//
}
void show_maze(int row_robot, int column_robot)
{
int i, j;
for(i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
if (maze[i][j] == 0)
cout << " ";
else if (maze[i][j] == 1)
cout << "+";
else if (maze[i][j] == 2)
cout << "e";
else
cout << "X";
}
cout <<endl;
}
//maze[row_robot][column_robot] = robot;//What is the need of this
}
bool robot_move(int &row_robot, int &column_robot)
{
unsigned int num;
//srand((unsigned int)time(0));//you already have set srand in main
num = rand() % 4 + 1;
cout << "RANDOM NUMBER: " << num << endl;
// while (1)//what is the need of an infinite loop here ..what if roboat move towards wall or e.... break statement will never be executed and it goes to an infinite loop
// {
if (num == 1) //Move to North
{
if(row_robot-1>0)//change in check that aray index is valid or not
{
if (maze[row_robot-1][column_robot] != wall)//change in row count
{
if (maze[row_robot-1][column_robot] != e)//You need to check the new posigion not current one
{
maze[row_robot][column_robot] = space;
row_robot--;
maze[row_robot][column_robot] = robot;
//break;
}
}
}
}
else if (num == 2) //Move to South//
{
if(row_robot+1<SIZE)//change in check that aray index is valid or not
{
if (maze[row_robot+1][column_robot] != wall)//You need to check the new posigion not current one
{
if (maze[row_robot+1][column_robot] != e)
{
maze[row_robot][column_robot] = space;
row_robot++;
maze[row_robot][column_robot] = robot;
//break;
}
}
}
}
else if (num == 3) //Move to East//
{
if(column_robot+1<SIZE)//change in check that aray index is valid or not
{
if (maze[row_robot][column_robot+1] != wall) //You need to check the new posigion not current one
{
if (maze[row_robot][column_robot+1] != e)
{
maze[row_robot][column_robot] = space;
column_robot++;
maze[row_robot][column_robot] = robot;
//break;
}
}
}
}
else //Move to west//
{
if(column_robot-1>0)//change in check that aray index is valid or not
{
if (maze[row_robot][column_robot-1] != wall)//You need to check the new posigion not current one
{
if (maze[row_robot][column_robot-1] != e)
{
maze[row_robot][column_robot] = space;
column_robot--;
maze[row_robot][column_robot] = robot;
//break;
}
}
}
}
// }
cout<<" ......................................... ";
return 0;
}
//Tell me(in comments) when your roboat survived then will change it for exit
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.