Cannot get mouse in maze to move correctly

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

47 Replies - 1581 Views - Last Post: 28 October 2018 - 01:46 AM Rate Topic: -----

#1 CooolGuy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 06-October 17

Cannot get mouse in maze to move correctly

Posted 23 October 2018 - 09:37 PM

Hey everyone. Just to give a little background for my assignment, this is from the professor...

"In this project, we are going to create a maze using an input file. The maze size is fixed, 30 rows and 100 columns (characters). The file we read to create the maze should translate the following values on the screen:

0: blank

1: Wall character (ascii for wall is 219)

2: Landmine

3: EXIT (solution).

The objective is to use a STACK data structure that you build yourself. Do not use any built in stack structures. The start of the maze is always row 2 column 1 assuming the top location is 1,1).

Your maze must apply the following logic:

1-) If the mouse hits a mine, the game should end (in a nice explosion maybe!!!)

2-) The mouse must have a different symbol than a wall or landmine or space"

Right now i'm stuck on getting the mouse to move correctly. I also need to be able to get the mouse to back track using an "already visited" array. This is my code so far..
#include<iostream>
#include<string>
#include<stack>
#include<fstream>
#include<windows.h>
#include<conio.h>
using namespace std;

void gotoxy(int column, int line)
{
	COORD coord;
	coord.X = column;
	coord.Y = line;
	SetConsoleCursorPosition(
		GetStdHandle(STD_OUTPUT_HANDLE),
		coord
	);
}

class Stack
{
public:
	POINT arr[1000];
	int index;

	Stack() { index = 0; }
	void push(int a, int B)/>/>
	{
		POINT P;
		P.x = a;
		P.y = b;
		arr[index] = P;
		index++;
	}
	void pop()
	{
		POINT P;
		index--;
		P.x = arr[index].x;
		P.y = arr[index].y;
	}
	bool IsAtTop(int a, int B)/>/>
	{
		POINT P;
		P.x = arr[index - 1].x;
		P.y = arr[index - 1].y;

		if ((P.x == a) && (P.y == B)/>/>)
			return true;
		else
			return false;
	}
	bool isEmpty()
	{
		return (index == 0 ? true : false);
	}
	bool isFull()
	{
		return (index + 1 == 50 ? true : false);
	}
};

class Maze
{
public:
	ifstream inputFile;
	int row = 30, col = 100;
	char maze[30][100];
	int x = 1, y = 1;
	int index = 0;
	POINT arr[1000];

	void printMaze()
	{
		inputFile.open("maze.txt");

		if (inputFile.is_open())
		{
			for (int i = 0; i < row; i++)
			{
				for (int j = 0; j < col; j++)
				{
					inputFile >> maze[i][j];

					if (maze[i][j] == '0')
						cout << ' ';
					if (maze[i][j] == '1')
						cout << char(219);
					if (maze[i][j] == '2')
						cout << char(248);
					if (maze[i][j] == '3')
						cout << char(176);
				}
				cout << "\n";
			}
		}
		else if (!inputFile)
			cout << "Could not load file!\n";
	}
	void traverseMaze()
	{
		Stack S;

		while (true)
		{
			gotoxy(x, y);
			_putch('*');
			Sleep(70);

			if (maze[x + 1][y] == '1')
			{
				S.push(x + 1, y);
				maze[x][y] = '*';
				x++;
			}
			if (maze[x - 1][y] == '1')
			{
				S.push(x - 1, y);
				maze[x][y] = '*';
				x--;
			}
			if (maze[x][y + 1] == '1')
			{
				S.push(x, y + 1);
				maze[x][y] = '*';
				y++;
			}
			if (maze[x][y - 1] == '1')
			{
				S.push(x, y - 1);
				maze[x][y] = '*';
				y--;
			}
			else
			{
				S.pop();
			}
		}
	}
};

int main()
{
	Maze M;

	M.printMaze();
	M.traverseMaze();

	M.inputFile.close();
	system("pause");
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Cannot get mouse in maze to move correctly

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14579
  • View blog
  • Posts: 58,444
  • Joined: 12-June 08

Re: Cannot get mouse in maze to move correctly

Posted 24 October 2018 - 06:44 AM

Quote

Right now i'm stuck on getting the mouse to move correctly.

What does this mean?
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6565
  • View blog
  • Posts: 22,528
  • Joined: 05-May 12

Re: Cannot get mouse in maze to move correctly

Posted 24 October 2018 - 06:59 AM

View PostCooolGuy, on 24 October 2018 - 12:37 AM, said:

Right now i'm stuck on getting the mouse to move correctly.

It may help if you actually updated your x and y variables to move through the maze.
It may also help to remember that arrays in C++ are zero based.
Was This Post Helpful? 0
  • +
  • -

#4 CooolGuy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 06-October 17

Re: Cannot get mouse in maze to move correctly

Posted 24 October 2018 - 07:32 AM

View Postmodi123_1, on 24 October 2018 - 01:44 PM, said:

Quote

Right now i'm stuck on getting the mouse to move correctly.

What does this mean?


I am loading the maze form this "maze.txt" file. I am using the traverse maze function to get a mouse to check right, left, up, down, and to move the mouse accordingly.

Attached File(s)

  • Attached File  maze.txt (2.99K)
    Number of downloads: 21

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6565
  • View blog
  • Posts: 22,528
  • Joined: 05-May 12

Re: Cannot get mouse in maze to move correctly

Posted 24 October 2018 - 07:39 AM

And? Explain more about what "I'm stuck on getting the mouse to move correctly" means.
Tell us what behavior are you seeing? What behavior were you expecting to see?
Tell us what are the things you have tried to do to get yourself unstuck? What were the results of those attempts?
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is online

  • member icon

Reputation: 5615
  • View blog
  • Posts: 17,312
  • Joined: 25-December 09

Re: Cannot get mouse in maze to move correctly

Posted 24 October 2018 - 08:24 AM

By the way this statement is incorrect: 1: Wall character (ascii for wall is 219). ASCII characters are all within the range of 0 to 127 inclusive. And be careful in C/C++ a char is usually limited to values between -128 to 127 anything larger or smaller will overflow the type and invoke undefined behavior.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1554
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: Cannot get mouse in maze to move correctly

Posted 24 October 2018 - 09:41 AM

As you have noted to get something to move mouse or not,

you need an x y position of its current position and future position or using reverse logic you can achieve animation with an x y position of its past position and its current position.

You only have an x y position of its current position.

using the past position idea you print a mouse at x y at the current location then print a space " " erasing the past position,

this will make the mouse look like its moving.
Was This Post Helpful? 0
  • +
  • -

#8 CooolGuy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 06-October 17

Re: Cannot get mouse in maze to move correctly

Posted 24 October 2018 - 07:07 PM

So I've adjusted the code a little bit. I've gotten the mouse to move to the right but I cannot seem to get him to move in any other direction when he hits a wall. Once it hits a wall on the right, it just stops moving. I also need to implement a backtracking feature. Here is the adjusted code...
#include<iostream>
#include<string>
#include<stack>
#include<fstream>
#include<windows.h>
#include<conio.h>
using namespace std;

void gotoxy(int column, int line)
{
	COORD coord;
	coord.X = column;
	coord.Y = line;
	SetConsoleCursorPosition(
		GetStdHandle(STD_OUTPUT_HANDLE),
		coord
	);
}

class Stack
{
public:
	POINT arr[1000];
	int index;

	Stack() { index = 0; }
	void push(int a, int B)/>/>
	{
		POINT P;
		P.x = a;
		P.y = b;
		arr[index] = P;
		index++;
	}
	void pop()
	{
		POINT P;
		index--;
		P.x = arr[index].x;
		P.y = arr[index].y;
	}
	bool IsAtTop(int a, int B)/>/>
	{
		POINT P;
		P.x = arr[index - 1].x;
		P.y = arr[index - 1].y;

		if ((P.x == a) && (P.y == B)/>/>)
			return true;
		else
			return false;
	}
	bool isEmpty()
	{
		return (index == 0 ? true : false);
	}
	bool isFull()
	{
		return (index + 1 == 50 ? true : false);
	}
	bool notVisited()
	{

	}
};

class Maze
{
public:
	ifstream inputFile;
	int row = 30, col = 100;
	char maze[30][100];
	int x = 1, y = 1;
	int index = 0;
	POINT arr[1000];
	int visitedArr[1000];

	void printMaze()
	{
		inputFile.open("maze.txt");

		if (inputFile.is_open())
		{
			for (int i = 0; i < row; i++)
			{
				for (int j = 0; j < col; j++)
				{
					inputFile >> maze[i][j];

					if (maze[i][j] == '0')
						cout << ' ';
					if (maze[i][j] == '1')
						cout << char(219);
					if (maze[i][j] == '2')
						cout << char(248);
					if (maze[i][j] == '3')
						cout << char(176);
				}
				cout << "\n";
			}
		}
		else if (!inputFile)
			cout << "Could not load file!\n";
	}
	void traverseMaze()
	{
		Stack S;
		bool done = false;

		while (maze[y][x] != '3')
		{
			gotoxy(x, y);
			_putch('*');
			Sleep(30);

			if (maze[y][x + 1] == '0' && S.IsAtTop(x, y) == false) //Checking up //Other conditions //Not at top of stack //Also visited array
			{
				S.push(x + 1, y);
				x++;
			}
			else if (maze[y][x - 1] == '0' && S.IsAtTop(x, y) == false) //Checking down
			{
				S.push(x - 1, y);
				x--;
			}
			else if (maze[y + 1][x] == '0' && S.IsAtTop(x, y) == false) //Checking right
			{
				S.push(x, y);
				y++;
			}
			else if (maze[y - 1][x] == '0' && S.IsAtTop(x, y) == false) //Checking left
			{
				S.push(x, y - 1);
				y--;
			}
			else
			{
				S.pop();
			}
		}
	}
};

int main()
{
	Maze M;

	M.printMaze();
	M.traverseMaze();

	M.inputFile.close();
	system("pause");
	return 0;
}


This post has been edited by CooolGuy: 24 October 2018 - 07:08 PM

Was This Post Helpful? 0
  • +
  • -

#9 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1554
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: Cannot get mouse in maze to move correctly

Posted 24 October 2018 - 11:50 PM

As far as I can see,

You have done very little,

apply some logic... two arrays one for current position one for past position.

Also not surprising that it just stops moving when it hits a wall as you have provided no instructions to the computer to tell it what to do when it hits a wall...! The computer cannot read your intentions only your actions, so given the lack of information on walls, the computer does nothing.

apply some logic..!

do some real work..!
Was This Post Helpful? 1
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6565
  • View blog
  • Posts: 22,528
  • Joined: 05-May 12

Re: Cannot get mouse in maze to move correctly

Posted 25 October 2018 - 10:14 AM

I think part of the problem here is that our OP didn't do any analysis regarding "why" a stack needed to be used. He simply started using it, and therefore is not using it properly in terms of what he pushes unto the stack, and what he pops back off the stack.

Hint: When you are navigating a maze (and not simply following the left-hand rule -- left-hand because you want your right hand ready to draw your sword when you come around that corner, find a monster, and fail your initiative roll), you'll want to keep track of not only what position you were at, but also what directions you have already tried.
Was This Post Helpful? 1
  • +
  • -

#11 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1554
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: Cannot get mouse in maze to move correctly

Posted 25 October 2018 - 10:22 AM

Yes, research is part of coding though... you can't possibly be an expert in everything so researching mazes and how to solve them programmatically would have been top of my list of things to do, which would have taken me very quickly to Stacks... and how they apply to mazes.

Not sure about the Dungeons and Dragons reference though, some people might think we are nerds. :)
Was This Post Helpful? 1
  • +
  • -

#12 andrewsw   User is offline

  • head thrashing
  • member icon

Reputation: 6645
  • View blog
  • Posts: 27,201
  • Joined: 12-December 12

Re: Cannot get mouse in maze to move correctly

Posted 25 October 2018 - 10:25 AM

As a leftie, should I still follow the left-hand rule... but walk backwards?
Was This Post Helpful? 0
  • +
  • -

#13 CooolGuy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 06-October 17

Re: Cannot get mouse in maze to move correctly

Posted 25 October 2018 - 10:48 AM

View PostSkydiver, on 25 October 2018 - 05:14 PM, said:

I think part of the problem here is that our OP didn't do any analysis regarding "why" a stack needed to be used. He simply started using it, and therefore is not using it properly in terms of what he pushes unto the stack, and what he pops back off the stack.

Hint: When you are navigating a maze (and not simply following the left-hand rule -- left-hand because you want your right hand ready to draw your sword when you come around that corner, find a monster, and fail your initiative roll), you'll want to keep track of not only what position you were at, but also what directions you have already tried.

I understand why I am using a stack. I just do not know how to write what I need to get the mouse to move correctly. We only went only stacks one time in class and am now expected to write this big program. I just don't know what to write next or even how to write it.
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6565
  • View blog
  • Posts: 22,528
  • Joined: 05-May 12

Re: Cannot get mouse in maze to move correctly

Posted 25 October 2018 - 10:57 AM

View Postandrewsw, on 25 October 2018 - 01:25 PM, said:

As a leftie, should I still follow the left-hand rule... but walk backwards?

Are you really left-handed?

Was This Post Helpful? 0
  • +
  • -

#15 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6565
  • View blog
  • Posts: 22,528
  • Joined: 05-May 12

Re: Cannot get mouse in maze to move correctly

Posted 25 October 2018 - 11:02 AM

View PostCooolGuy, on 25 October 2018 - 01:48 PM, said:

I understand why I am using a stack. I just do not know how to write what I need to get the mouse to move correctly. We only went only stacks one time in class and am now expected to write this big program. I just don't know what to write next or even how to write it.

Can you explain in plain words why you are using the stack in the context of this problem? Why are you pushing your future position? How will that help you when you pop it back off the stack? Why are you not doing anything with the stuff that you are popping off the stack currently?

Perhaps if you pushed your current position and current direction before taking a branch it would be more helpful. Later when you pop that off the stack, you'll know that by popping it off the stack that direction didn't work out, so you'll need to try the next direction, and if you've exhausted all directions, that you'll need to pop some more.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »