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?