moving a character around a 'board' on screen

So basically, I have a 2d Array that is 3x3. I have made it a class called "Board", and the constructor fills it with 'X's. I would like the user (depicted as an 'O') to be able to move up, down, left, or right and then print the array back to them with their positioning updated. I think my code looks good, I am getting a runtime error though. please help!

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  #include <iostream>
#include <vector>
#include <string>
#include <ctime>
using namespace std;

class Board
{
public:
	Board();
	void setBoard();
	void getBoard();
	void changeBoard();
	void searchBoard();
	
private:
	char board[3][3];
	int row;
	int col;
	int target1, target2;
};

int main()
{
	Board B1;

	cout<<"initialized board: "<<endl;
	B1.getBoard();

	B1.setBoard();

	cout<<endl<<"New board: "<<endl;
	B1.getBoard();
	B1.changeBoard();
	

	
	return 0;
}

Board::Board()
{
	for (row=0;row<3;row++)
	{
		for(col=0;col<3;col++)
		{
			board[row][col] = ' ';
		}
	}
}

void Board::setBoard()
{
	board [0][0] = 'O';

	for (row=0;row<3;row++)
	{
		for (col=0;col<3;col++)
		{
			if (board[row][col] != 'O')
			{
			board[row][col] = 'X';
			}
		}

	}
}

void Board::getBoard()
{
	for (row=0;row<3;row++)
	{
		for (col=0;col<3;col++)
		{
			cout<<board[row][col];
		}
		cout<<endl;
	}
}


void Board::searchBoard()
{
	bool found = false;

	while (found != true)
	{
		for (row=0;row<3;row++)
		{
			for (col=0;col<3;col++)
			{
				if (board[row][col]=='O')
				{
					bool found = true;
					target1 = row;
					target2 = col;
				}
			}
		}
	}
}

void Board::changeBoard()
{

	searchBoard();

	char choice;
	cout<<"use 'w', 'a', 's', and 'd' to navigate"<<endl;
	cout<<"'up', 'down', 'left', and 'right', respectively."<<endl;
	cin>>choice;

	switch (choice)
	{
	case 'w':
		board[target1][target2] = 'X';
		board[(target1)-1][target2] = 'O';
		break;
	case 'a':
		board[target1][target2] = 'X';
		board[target1][(target2)-1] = 'O';
		break;
	case 's':
		board[target1][target2] = 'X';
		board[(target1)+1][target2] = 'O';
		break;
	case 'd':
		board[target1][target2] = 'X';
		board[target1][(target2)+2] = 'O';
		break;
	default:
		break;
	};
}
1
2
3
cout<<"use 'w', 'a', 's', and 'd' to navigate"<<endl;
	cout<<"'up', 'down', 'left', and 'right', respectively."<<endl;
	cin>>choice;

I think here you meant "up, left, down, right".

I'm not sure about the runtime error though works fine for me.
Yes, I saw that after I posted. But when you run, it's working fine and producing ideal output?
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
using namespace std;

class Board
{
public:
	Board();
	void setBoard();
	void getBoard();
	void changeBoard();
	void searchBoard();
	
private:
	char board[3][3];
	int row;
	int col;
	int target1, target2;
};

int main()
{
	Board B1;

	cout<<"initialized board: "<<endl;
	B1.getBoard();

	B1.setBoard();

	cout<<endl<<"New board: "<<endl;
	B1.getBoard();
	B1.changeBoard();
	//also show the new board :
	B1.getBoard();
	return 0;
}

Board::Board()
{
	for (row=0;row<3;row++)
	{
		for(col=0;col<3;col++)
		{
			board[row][col] = ' ';
		}
	}
}

void Board::setBoard()
{
	board [0][0] = 'O';

	for (row=0;row<3;row++)
	{
		for (col=0;col<3;col++)
		{
			if (board[row][col] != 'O')
			{
			board[row][col] = 'X';
			}
		}

	}
}

void Board::getBoard()
{
	for (row=0;row<3;row++)
	{
		for (col=0;col<3;col++)
		{
			cout<<board[row][col];
		}
		cout<<endl;
	}
}


void Board::searchBoard()
{
	//bool found = false;
	for (row=0;row<3 /* && not found*/;row++)
	{
		for (col=0;col<3 /* && not found*/;col++)
		{
			if (board[row][col]=='O')
			{
				//bool found = true;
				//you meant : found = true;
				//which still won't work with the outer loop, you have to specify
				//the condition in the each loop's condition.
				target1 = row;
				target2 = col;
				return;/or alternatively , simply return to the caller
			}
		}
	}
}

void Board::changeBoard()
{

	searchBoard();

	char choice;
	cout<<"use 'w', 'a', 's', and 'd' to navigate"<<endl;
	cout<<"'up', 'left', 'down', and 'right', respectively."<<endl;
	cin>>choice;

	switch (choice)
	{
	case 'w':
		board[target1][target2] = 'X';
		board[(target1)-1][target2] = 'O';
		break;
	case 'a':
		board[target1][target2] = 'X';
		board[target1][(target2)-1] = 'O';
		break;
	case 's':
		board[target1][target2] = 'X';
		board[(target1)+1][target2] = 'O';
		break;
	case 'd':
		board[target1][target2] = 'X';
		board[target1][(target2)+2] = 'O';
		break;
	default:
		break;
	};
}

This is your original code corrected with the comments.
Last edited on
@a k n,
thank you for your help, all's working well now!
Topic archived. No new replies allowed.