Help in finding the error in the Code

I was trying to make the Dungeon Crawl game (again) listed here in the Beginner's Exercises Article:
http://www.cplusplus.com/forum/articles/12974/

I made a couple of changes to the game, though. Firstly I tried to make the Enemies, a bit more challenging, by making them come towards the User, A variable sized Board (with a minimum and maximum range of course!).

First the basic structure of the program:
The board is defined in a class named DungeonC. The Board itself is a pointer which is allocated dynamically to variable sizes.

There is another class, Character, which has 5 protected members, of them only 3 being relevant to the problem, namely:

1
2
3
char Peg;    //Symbol of the Peg
int X;    //Height Coordinate of the 'Character' beginning at 0
int Y;    //Width Coordinate of the 'Character' beginning at 0 


There are a number of functions in the Public sections, 9 of them being to either return one of the protected members or change them.

There is one virtual Function, virtual void Move()

Further I derived 4 classes from it:
1
2
3
4
Player;
Enemy;
Trap;
Treasure;


The Enemy is the source of the Error.

The relevant snippet of code is here (I didn't think it prudent to post over 100 Lines of code here):
http://pastebin.com/zj5qhNZj

Now before I describe the error, I should tell that all the functions declared there are working (apart from Move(), which is the whole snippet itself!)

The error is somewhere in the Logic of the program, which I haven't been able to find.

A Basic Description of the Functions used in the Code:
[All functions with 'r' Prefixed in their name return a non-public value of the Class]

1) rPeg --> returns a char. Symbol of the Peg concerned.
2) rWidth --> returns an int. Width of the Board.
3) Offset --> returns an int. I use it to get the value of the location of a point on the Board on the basis of Coordinates.
4) ShowCell --> return a char. The Peg located on the cell passed as the Parameter
5) GetX & GetY --> The Name give it away, they return the Height and Width Coordinate of a cell respectively.
6) AddPeg & RemovePeg --> This Adds a Peg and removes on from the parameters of the Functions.

Now with all the Functions explained, the Error:

The code was supposed to work like this:

H.......
........
....X...
........
.......T

----------------
........
H...X...
........
........
.......T

----------------
........
...X....
H.......
........
.......T

----------------
........
..X.....
........
H.......
.......T

----------------
........
........
..X.....
.H......
.......T

----------------
........
........
........
..X.....
.H.....T



And so on...

But what happens is that The Enemy Doesn't move if my position Changes horizontally, but does if it does Vertically.

Any help regarding this problem?
Some one?
Maybe it could be because you're never looking at A->GetY()?

if ((A->GetX() < X) && Diff > 0)

else if ((A->GetX() < Y) && Diff < 0)

else if ((A->GetX() > X) && Diff > 0)

else if ((A->GetX() > Y) && Diff < 0)
Did the Change. That did correct the first error.

Now I noticed another one, more confusing that the previous.

Again, this is a form of output:

H.......
........
.......X
........
.......T

----------

........
H......X
........
........
.......T

----------

........
.H.....X
........
........
.......T



That is, the Enemy won't move if I move in the same row or column as the Enemy (Unless I am just within one cell away from it).

Any help?
Some one?
When Diff is set to 0, X=A->GetX(), or Y=A->GetY()

Even if you set Diff to -1 or 1 in that case, the X or Y are still equal, and you have no test in the following code that handles equal positions, so nothing will be done.
Maybe you must use <= and >= instead of < and >, or set X and Y according to your new value of Diff.
1
2
3
Xdif = A->GetX() - X;
Ydif = A->GetY()-Y;
int Diff = Xdif-Ydif;


Wouldn't Diff being 0 mean that X=A->GetX() AND Y=A->GetY()?

The they are in the same position, wouldn't it?

EDIT:
I guess I have found the problem.
It was that my method assumed that Xdif and Ydif are both Positive.
I made a function to make it so. Solving the probel.
Thanks for all the Help though.
Last edited on
Topic archived. No new replies allowed.