comparing a string letter with another letter?

Im making a small game call field runner but i have a proble with the if statment on line 43.

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
#include<iostream>
#include<string>

using namespace std;

void playermove ();
void print();

string player = "P";
string board[8][8] = 
{
{"ooooPooo"},
{"oooooooo"},
{"oooooooo"},
{"oooooooo"},
{"oooooooo"},
{"oooooooo"},
{"oooooooo"},
{"oooooooo"}};

main(){

 	print();	
	playermove();
	
	return 0;
}


void print(){
	for(int i = 0; i < 8 ; i++){
		cout << "\n" << "\t";
		for (int u = 0; u < 8 ; u++){
			cout << board[i][u];
		}}
}

void playermove(){

	for(int i = 0; i < 8 ; i++){

		for (int u = 0; u < 8 ; u++){
			if(board[i][u] == player){
				int y = i;
				int x = u;
				char move[5];
				cin >> move;
				
				if (move == "up"){
					y = y - 1;}
				if (move == "down"){
					y = y + 1;}
				if (move == "right"){
					x = x + 1;}
				if (move == "left"){
					x = x - 1;}
				board[i][u] = "o";
				board[y][x] = "P";
			};
		}}
}
Line #43

if(board[i][u] == player.front() ){
There is an array of 64 strings, but only only 8 are needed.

9
10
11
12
13
14
15
16
17
18
19
char player = 'P';
string board[8] =
{
{"ooooPooo"},
{"oooooooo"},
{"oooooooo"},
{"oooooooo"},
{"oooooooo"},
{"oooooooo"},
{"oooooooo"},
{"oooooooo"}};


Also change lines 57 and 58
57
58
    board[i][u] = 'o';
    board[y][x] = 'P';
Last edited on
okay so i found the bug and fix it and now i can move up and and left but for some reason i cant move down or right.
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

#include<iostream>
#include<string>

using namespace std;

void playermove ();
void print();

string player = "P";
string board[8][8] = 
{
{{"o"},{"o"},{"o"},{"o"},{"o"},{"o"},{"o"},{"o"}},
{{"o"},{"o"},{"o"},{"o"},{"o"},{"o"},{"o"},{"o"}},
{{"o"},{"o"},{"o"},{"o"},{"o"},{"o"},{"o"},{"o"}},
{{"o"},{"o"},{"o"},{"o"},{"o"},{"o"},{"o"},{"o"}},
{{"o"},{"o"},{"o"},{"o"},{"o"},{"o"},{"o"},{"o"}},
{{"o"},{"o"},{"o"},{"o"},{"o"},{"o"},{"o"},{"o"}},
{{"o"},{"o"},{"o"},{"o"},{"o"},{"o"},{"o"},{"o"}},
{{"o"},{"o"},{"o"},{"o"},{"P"},{"o"},{"o"},{"o"}}};

main(){
while(true){
 	print();	
	playermove();}
	return 0;
}


void print(){
	for(int i = 0; i < 8 ; i++){
		cout << "\n" << "\t";
		for (int u = 0; u < 8 ; u++){
			cout << board[i][u];
		}}
}

void playermove(){

	for(int i = 0; i < 8 ; i++){

		for (int u = 0; u < 8 ; u++){
			if(board[i][u] == player){
				int y = i;
				int x = u;
				string move = "lllll";
				cout << "\n";
				cin >> move;
				
				
				if (move == "up"){
					y = y - 1;}
				else if (move == "down"){
					y = y + 1;}
				else if (move == "right"){
					x = x + 1;}
				else if (move == "left"){
					x = x - 1;}
				else {cout << "wait what are you trying to do?";}
				board[i][u] = "o";
				board[y][x] = "P";
			};
		}}
}
You want the outer loop to break in order to return back to your main and run print() again. I'll focus on i to explain. Let's say P is at [6][4]. In order for that loop to enter that position,

i = 6

When you minus y (up), the new position of P is [5][4]. The loop runs.

i = 7

So the loop runs until it breaks and print() can run because the if condition will never be true.

Same example, P is at [6][4].

i = 6

When you add to y (down), the new position of P is [7][4]. The loop runs and becomes,

i = 7

Because the latest position of P is [7][4], if (board[i][u] == player) returns true, then you enter the block and cin >> move stops the program. You are actually moving, try entering right, then enter up.
Last edited on
Topic archived. No new replies allowed.