Array problem

I was trying to make the Dungeon Crawl program given in the article Beginner Exercises.

Now I thought of using a bi-dimensional array, but was told to use single dimensional arrays instead.

I am having a problem in that part. The code first:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
while (true)
{
board[ (playh-1)  * (Width) + (playw-1)] = Hero.Peg;  //Putting the Player Peg on the board
//Various I/O operations and printing of the board.
switch (Move)
{
case "Up":
	if (playh != 1)
	{
		board[ (playh-1) * Width + (playw-1)] = '.';
		playh -= 1;
	}
	break;
case "Down":
	if (playh != Height)
	{
		board[ (playh-1) * Width + (playw-1)] = '.';
		playh += 1;
	}
case "Left":
	if (playw != 1)
	{
		board[ (playh-1) * Width + (playw-1)] = '.';
		playw -= 1;
	}
	break;
case "Right":
	if (playw != Width)
	{
		board[ (playh-1) * Width + (playw-1)] = '.';
		playw += 1;
	}
	break;
}
//Breaking the loop if WIN or LOSE
}


Here playw & playh are the variables for the coordinates of the player's peg (Initially equal to 1 & 1)
I have set the Width and Height of the board to be input by the user (Min: 5 Max: 8)

The code works fine for Up, Left and Right. The problem is in Down.

The output is somewhat like this [5 X 5 board]:

H....
.....
.....
.....
....T

Move: Right

.H...
.....
.....
.....
....T

Move: Right

..H..
.....
.....
.....
....T

Move: Down

.....
.H...
.....
.....
....T


As you can see the H comes from column 3 to column 2 when it should stay in column 4.

The problem is possible for all the possible board arrangements (5x5, 5x6 ,5x7 etc)

Any help?
closed account (S6k9GNh0)
I'm confused by: (playh-1) * Width
Why can't you just store the coordinates (x,y)?
Can you provide the full code so my head doesn't explode with assumptions...
Last edited on
Can you provide the full code so my head doesn't explode with assumptions...

Full code is too long for posting here (204 lines).
Why can't you just store the coordinates (x,y)?

I am storing the coordinates as (playh, playw) They are short for player height and player width.
I'm confused by: (playh-1) * Width

I got that formula in this article that was given in a previous topic suggesting me not to use Multi Dimensional Arrays.
This was the article:
http://cplusplus.com/articles/G8hv0pDG/
Try putting it up on pastebin or something.
The code works fine for Up, Left and Right. The problem is in Down.
You forgot a break; before line 20.

(Initially equal to 1 & 1)
Why? When 0,0 the calculation would be easier
I had put up the furmula:
board[ (Height-1) * Width + (Width-1) ] = CHARACTER

as a comment. I decided to do it keeping this formula as standard.

And thanks a lot for finding that error.
By the way, that's the kind of thing you'd want to put in a function:

1
2
3
unsigned int find_board_offset(unsigned int x, unsigned int y) {
    //...
}


Of course, the best solution (IMO) is to put the entire 2d array in a class like the article suggested (it did, right?).
I will do that! it is a good idea.

As for making the class, the example it posted (it was using a template, or was it something . else?) was a bit too complex for me. So I decided to stick to the formula!
Topic archived. No new replies allowed.