tMove() Function

I just started learning C++ about two days ago. It is my first programming language, but I feel that I have progressed far.

However, upon practicing coding, I stumbled across a dilemma in which I've spent over half-an-hour trying to work out.

I am creating a game called Dungeon Crawl. There is a [7][10] array in which I have produced. It serves as the "map" of the game, laying out the positions of the characters. It looks like this:

. . . . . . . . . .
. P . . . . . . . .
. . . . . . E . . .
. . . T . . . . . .
. . . . E . . . . .
. . T . . . E . . .
. . . . . . . . . X

The 'E's stay in place, but if the player ('P') collides into it, the player dies. The objective is to reach the X, as it is the location of the "treasure".

I created a function called tMove(), which is intended to locate 'T' by iterating through the array and using conditionals (if board[i][j] == 'T'). The function is used after the player is prompted to enter which direction they would like to go (Up/Down/Left/Right).

Anyway, once 'T' is located, it will cause 'T' to move in a random direction after testing if there is not an 'E' in the way and makes sure it does not pass outside of the board.

The problem is as follows: Sometimes the 'T's will disappear because the for loop finds the T that has moved already and replaces it with a '.'. How can I go about fixing this? Should I introduce different variables, and if so, how?

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
37
38
39
40
41
42
43
44
45
void tMove()		//for the AI 'T' and how it moves (SUPER LOOP NEST INC)
{
	for(int i = 0; i < 7; i++)
	{
		for(int j = 0; j < 10; j++)
		{
			if(board[i][j] == 'T')
			{
					srand(time(0));
					int tRand = rand() % 4;
					switch(tRand)		//if the remainder is 0,1,2,3
					{
					case 0:
						if(board[i - 1][j] != 'E' && i > 0)
						{
							board[i][j] = '.'; //replace T with a '.'
							board[i - 1][j] = 'T'; //T moves up
						} 
						break;
					case 1:
						if(board[i + 1][j] != 'E' && i < 7)
						{
							board[i][j] = '.';
							board[i + 1][j] = 'T';	//T moves down
						} 
						break;
					case 2:
						if(board[i][j - 1] != 'E' && j > 0)
						{
							board[i][j] = '.';
							board[i][j - 1] = 'T'; //T moves left
						}
						break;
					case 3:
						if(board[i][j + 1] != 'E' && j < 7)
						{
							board[i][j] = '.';
							board[i][j + 1] = 'T'; //T moves right
						}
						break;
					}
			}
		}
	}
}
Last edited on
Well what I would guess is happening is that T is being moved to a location that already contains another T. So if you have an array like this:

. . . . . . . . . T
. P . . . . . . . T
. . . . . . E . . .
. . . T . . . . . .
. . . . E . . . . .
. . T . . . E . . .
. . . . . . . . . X

Than board[0][9] will trigger your if statement. Now if rand()%4 returns 1, board[0][9] is set to '.', and board[1][9] stays 'T'.

To make it all less obvious from your perspective, the 'T' at board [1][9] is later moved.


I don't know about a solution though (I am a beginner too). You could stop 'T' from moving to locations with 'T's already in them, but that may not be what you want.

BTW case 3's if statement should check if j is less than 9, not 7.
Okay, nice catch on the conditional statements. I failed horribly there. I also edited it so that the 'T's don't overlap.

But, I'm still running into the same problem with the Ts disappearing.

The 'T's were moving properly, then all of a sudden, they just... vanished! Three moves into the game, it looks like this:

From

. . . . . . . . . .
. . T . . . . . . .
. . . . . . E . . .
. T . . . . . . . .
. P . . E . . . . .
. . . . . . E . . .
. . . . . . . . . X

To
. . . . . . . . . .
. . . . . . . . . .
. . . . . . E . . .
. . . . . . . . . .
. P . . E . . . . .
. . . . . . E . . .
. . . . . . . . . X

My theory is that when the array is scanning through (since it's scanning through columns then rows), the T is place right back where it is supposed to be placed by random decision. Then, it notices the T there again.

Let's say the loop is at board[2][3], where some 'T' is located. The whole process of conditional statements is executed, so 'T' moves to the right.

Now, 'T' is at [2][4], which is the next part of the ray to be analyzed because the for loop increases (board(i)(j)) j by 1. This causes it to move 'T' again, to, let's say [3][4]. The array will be iterated to [3][4] in the for loop, causing it to move T again!

This is the main problem. It causes 'T's to disappear, and I don't know how I would introduce another variable in there as confirmation.

Edit: Browser failed, making a mess of the board.
Last edited on
I got it to work. All I had to do was make an if statement in a switch statement in an if statement (yeah, annoyingly complicated), because when it would iterate the array, they couldn't move right nor down. My fix made it so they can move every direction.

Thank you for helping out, Don Gato.

Edit: Almost forgot the code!

Here's the fixed code, but it is VERY messy, so prepare your eyes:
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
void tMove()		//for the 'T' and how it moves (SUPER LOOP NEST INC)
{
	srand(time(0));
	int tRand = rand() % 4;
	if(tRand == 1 || tRand == 0)
	{
		for(int i = 0; i < 7; i++)
		{
			for(int j = 0; j < 10; j++)
			{
				if(board[i][j] == 'T')
				{	
					switch(tRand)		//if the remainder is 0,1
					{
					case 0:
						if(board[i - 1][j] != 'E' 
							&& board[i - 1][j] != 'T' 
							&& board[i - 1][j] != 'X' && i > 0)
						{
							board[i][j] = '.';  //replace T w/ '.'
							board[i - 1][j] = 'T'; //T moves up
						} 
						break;
					case 1:
						if(board[i][j - 1] != 'E'
							&& board[i][j - 1] != 'T' 
							&& board[i][j - 1] != 'X' && j > 0)
						{
							board[i][j] = '.';
							board[i][j - 1] = 'T';	//T moves left
						} 
						break;	
					}
				}
			}
		}
	}
	if(tRand == 2 || tRand == 3)
	{
		for(int i = 6; i >= 0; i--)
		{
			for(int j = 9; j >= 0; j--)
			{
				if(board[i][j] == 'T')
				{
					switch(tRand)		//if the remainder is 0,1
					{
					case 2:
						if(board[i + 1][j] != 'E' 
							&& board[i + 1][j] != 'T' 
							&& board[i + 1][j] != 'X' && i < 6)
						{
							board[i][j] = '.';		
							board[i + 1][j] = 'T'; //T moves down
						} 
						break;
					case 3:
						if(board[i][j + 1] != 'E' 
							&& board[i][j + 1] != 'T' 
							&& board[i][j + 1] != 'X' && j < 9)
						{
							board[i][j] = '.';
							board[i][j + 1] = 'T';	//T moves right
						} 
						break;
					}
				}
			}
		}
	}
}
Last edited on
Oh man, just keep track of the locations of P, T, and Es.

Topic archived. No new replies allowed.