Debugging issue

With what I have so far, I will type in north, and because it is out of bounds, it prints to screen that I can't go that direction. Then, on the next loop through, I type south ( which I KNOW is within bounds ) and it tells me it's out of bounds again. after typing south again, it makes the move, what is happening here?

1
2
3
4
5
6
	bool gameOver = false;
	
	while ( !gameOver )
	{
		hero.getCommand();	
	}


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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "player.h"
#include <iostream>

player::player(int set)
{
	xCoord = 0, yCoord = 0;
	boundLimit = set + 1;
}

void player::getCommand()
{
	std::string commandInput;
	std::cout << "What will you do?   |";
	getline(std::cin, commandInput);
	
	for ( int j = 0; j < commandInput.size(); j++ )
	{
		commandInput[j] = tolower(commandInput[j]);
	}
	
	if ( commandInput == "commands" || commandInput == "command" )
	{
		std::cout << "----Commands----" << std::endl;
		std::cout << std::endl;
		std::cout << "go north | north" << std::endl;
		std::cout << "go south | south" << std::endl;
		std::cout << "go east  | east " << std::endl;
		std::cout << "go west  | west " << std::endl;
	}
	else if ( commandInput == "go north" || commandInput == "north" )
	{
		move('n');
		
	}
	else if ( commandInput == "go south" || commandInput == "south" )
	{
		move('s');
		
	}
	else if ( commandInput == "go east" || commandInput == "east" )
	{
		move('e');
		
	}
	else if ( commandInput == "go west" || commandInput == "west" )
	{
		move('w');
		
	}
	else
	{
		std::cout << "'" << commandInput << "' is an unknown instruction. Type 'commands' for a list of valid moves." << std::endl;
	}
}

void player::move(char direction)
{
	switch ( direction )
	{
		case 'n':
					if ( (xCoord--) < boundLimit && (xCoord--) > -1 )
					{
						xCoord--;
					}
					else
					{
						std::cout << "You can't go that direction, yet." << std::endl;
					}
					break;
		case 's':
					if ( (xCoord++) < boundLimit && (xCoord++) > -1 )
					{
						xCoord++;
					}
					else
					{
						std::cout << "You can't go that direction, yet." << std::endl;
					}
					break;
		case 'e':
					if ( (yCoord++) < boundLimit && (yCoord++) > -1 )
					{
						yCoord++;
					}
					else
					{
						std::cout << "You can't go that direction, yet." << std::endl;
					}
					break;
		case 'w':
					if ( (yCoord--) < boundLimit && (yCoord--) > -1 )
					{
						yCoord--;
					}
					else
					{
						std::cout << "You can't go that direction, yet." << std::endl;
					}
					break;
		default:
					std::cout << "An error as occured, please contact the game designer." << std::endl;
	}
}
Last edited on
You are manipulating xCoord and yCoord within the if condition so it might be updated up to 3 times.
1
2
3
4
if ( (xCoord--) < boundLimit && (xCoord--) > -1 )
{
	xCoord--;
}
Oh, I didn't realize that doing that in a condition test actually manipulated it.
I see what I did wrong, thank you! I should be using xCoord - 1 since -- sets it equal to one less!

DERP!
Last edited on
Ok, that's not working either... What can I do to do fix this?

1
2
3
4
5
6
7
8
9
if ( (xCoord - 1) < boundLimit && (xCoord - 1) > -1 )
					{
						xCoord--;
					}
					else
					{
						std::cout << "You can't go that direction, yet." << std::endl;
					}
					break;
I am able to go north indefinitely, and I can't figure out why. boundLimit is = to 11. Every other directions limit is working except for north.

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "player.h"
#include <iostream>

player::player(int set)
{
	xCoord = 0, yCoord = 0;
	boundLimit = set + 1;
}

void player::getCommand()
{
	std::string commandInput;
	std::cout << "What will you do?   |";
	getline(std::cin, commandInput);
	
	for ( int j = 0; j < commandInput.size(); j++ )
	{
		commandInput[j] = tolower(commandInput[j]);
	}
	
	if ( commandInput == "commands" || commandInput == "command" )
	{
		system("CLS");
		std::cout << "----Commands----" << std::endl;
		std::cout << std::endl;
		std::cout << "go north | north" << std::endl;
		std::cout << "go south | south" << std::endl;
		std::cout << "go east  | east " << std::endl;
		std::cout << "go west  | west " << std::endl;
	}
	else if ( commandInput == "go north" || commandInput == "north" )
	{
		
		
		
	}
	else if ( commandInput == "go south" || commandInput == "south" )
	{
		move('s');
		
	}
	else if ( commandInput == "go east" || commandInput == "east" )
	{
		move('e');
		
	}
	else if ( commandInput == "go west" || commandInput == "west" )
	{
		move('w');
		
	}
	else
	{
		std::cout << "'" << commandInput << "' is an unknown instruction. Type 'commands' for a list of valid moves." << std::endl;
	}
}

void player::move(char direction)
{
	switch ( direction )
	{
		case 'n': 
					if ( (xCoord - 1) < boundLimit && (xCoord - 1) > -1 )
					{
						xCoord--;
					}
					else
					{
						std::cout << "You can't go that direction, yet." << std::endl;
					}
					break;
		case 's':
					if ( (xCoord + 1) < boundLimit && (xCoord + 1) > -1 )
					{
						xCoord++;
					}
					else
					{
						std::cout << "You can't go that direction, yet." << std::endl;
					}
					break;
		case 'e':
					if ( (yCoord + 1) < boundLimit && (yCoord + 1) > -1 )
					{
						yCoord++;
					}
					else
					{
						std::cout << "You can't go that direction, yet." << std::endl;
					}
					break;
		case 'w':
					if ( (yCoord - 1) < boundLimit && (yCoord - 1) > -1 )
					{
						yCoord--;
					}
					else
					{
						std::cout << "You can't go that direction, yet." << std::endl;
					}
					break;
		default:
					std::cout << "An error as occured, please contact the game designer." << std::endl;
	}
}
Last edited on
Shouldn't you be calling move('n') on line 34?
Last edited on
DERP. Always the simplest things i swear, thank you!
Topic archived. No new replies allowed.