making a bouncing O in C++

I have a homework assignment that I am having a hard time with. The assignment states:
In a window of character cells, which has a height of 23 and a width of 80, the program is to form a grid which is based on a two-dimensional array, and it will cause a ball to be represented by the letter capital “O”. The contents of the array are to be displayed to the user’s monitor. At the start of the game the user is to enter the two coordinates of a point on the grid to represent the initial position of the ball. Also, the user is to enter an initial diagonal direction (upper-left, upper-right, lower-left, lower-right) which the program will follow when tracing the progress of the ball through the grid. The program will terminate when the ball reaches the original array element from which it began. The ball does not need to be proceeding in the original direction. In addition, the program will leave a copy of the character capital “O” in each element of the array which it has traversed. Also, the program will count the number of unique array elements which the ball has traversed. When the program terminates, it will report the number of unique elements of the array which the ball has traversed.

This is a tough assignment and I am doing it in steps. My first step was to get the "O" to bounce around on the screen. I am having a very hard time getting this to work I fell like my code is correct, but I must be missing something because it only prints three "O"s. If anyone could point me in the right direction I would appreciate it. Below is the code I have currently:

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
#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{
	char ball[80][23];
	int coord[80][23];
	int origX;
	int origY;
	char mov1;
	char mov2;
	int x;
	int y;

	cout << "Enter the X coordinate for the ball (between 0 and 79): ";
	cin >> origX;
	cout << endl;
	cout << "Enter the Y coordinate for the ball (between 0 and 22): ";
	cin >> origY;
	cout << endl;
	cout << "Would you like the ball to move [u]p or [d]own? ";
	cin >> mov1;
	cout << endl;
	cout << "would you like the ball to move [l]eft or [r]ight? ";
	cin >> mov2;
	cout << endl;
	
	x = origX;
	y = origY;

	ball[x][y] = 'O';


	if (mov1 == 'u') 
		y--;

	if (mov1 == 'd') 
		y++;

	if (mov2 == 'r') 
		x++;

	if (mov2 == 'l') 
		x--;

	
	ball[x][y] = 'O';

	while ((x != origX) && (y != origY))
	{
		if (mov1 == 'u') 
			y--;

		if (mov1 == 'd') 
			y++;

		if (mov2 == 'r') 
			x++;

		if (mov2 == 'l') 
			x--;

		if (x == 79)
			x--;

		if (x == 0)
			x++;

		if (y == 22)
			y--;

		if (y == 0)
			y++;

		ball[x][y] = 'O';

		for(y = 0; y < 22; y++)
		{
			for(x = 0; x < 79; x++)
				cout << ball[x][y];

			cout << endl;
		}
		
		system("CLS"); 		
	}

	cin.get();
	cin.get();

	return 0;
}


update: I found part of my problem it was in my for loop. Now I am getting 4 rows of "O"s going across instead of just one my revised for loop is:
1
2
3
4
5
6
7
for(int height= 0; height< 22; height++)
{
	for(int width= 0; width< 79; width++)
		cout << ball[width][height];

	cout << endl;
}
Last edited on

run this, I have changed the code a bit. I think you will not find much problem in understanding this.

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
#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{

	char ball[23][80];
	int coord[23][80];
	int origX;
	int origY;
	char mov1;
	char mov2;
	int x;
	int y;

	int x_Direction = 1 * 1;	//initially towards right
	int y_Direction = 1 * 1;	//initially towards down

	memset(ball, ' ', 80 * 23);

	cout << "Enter the X coordinate for the ball (between 0 and 79): ";
	//cin >> origX;
	origX = 20;
	cout << endl;
	cout << "Enter the Y coordinate for the ball (between 0 and 22): ";
	//cin >> origY;
	origY = 10;
	cout << endl;
	//cout << "Would you like the ball to move [u]p or [d]own? ";
	//cin >> mov1;
	cout << endl;
	//cout << "would you like the ball to move [l]eft or [r]ight? ";
	//cin >> mov2;
	cout << endl;
	
	x = origX;
	y = origY;

	
	ball[y][x] = 'O';


	/*if (mov1 == 'u') 
		y--;

	if (mov1 == 'd') 
		y++;

	if (mov2 == 'r') 
		x++;

	if (mov2 == 'l') 
		x--;*/

	
	//ball[x][y] = 'O';

	do
	{
		y += y_Direction;
		x += x_Direction;

		if(x >= 78 || x <= 0)
			x_Direction *= 1 * -1;	//reverse direction on the edges

		if(y >= 21 || y <= 0)
			y_Direction *= 1 * -1;	//reverse direction on the edges

		/*if (mov1 == 'u') 
			y--;

		if (mov1 == 'd') 
			y++;

		if (mov2 == 'r') 
			x++;

		if (mov2 == 'l') 
			x--;

		if (x == 79)
			x--;

		if (x == 0)
			x++;

		if (y == 22)
			y--;

		if (y == 0)
			y++;*/

		memset(ball, ' ', 80 * 23);

		ball[y][x] = 'O';

		//draw screen
		for(int i = 0; i < 22; i++)
		{
			for(int j = 0; j < 79; j++)
				cout << ball[i][j];

			cout << endl;
		}
		
				
		system("CLS"); 		
	//}while ((x != origX) && (y != origY));
	}while(1);

	cin.get();
	cin.get();

	return 0;
}
Last edited on
thanks for the help I figured out the bouncing ball problem, now I am working on the next step:

In addition, the program will leave a copy of the character capital “O” in each element of the array which it has traversed. Also, the program will count the number of unique array elements which the ball has traversed. When the program terminates, it will report the number of unique elements of the array which the ball has traversed.

I'm not sure what this means. Can anyone give me an idea of how to get this to work. Not asking for anyone to do it for me, but a push in the right direction would be great thank you.

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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <conio.h>
#include <windows.h>
#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
	//variables
	int ball[80][23];
	void gotoxy(int, int);
	int noOfCoords = 0;
	int origX;
	int origY;
	char mov1;
	char mov2;
	int x;
	int y;

	//prompt user for ball placement and direction
	cout << "Enter the X coordinate for the ball (between 0 and 79): ";
	cin >> origX;
	cout << endl;
	cout << "Enter the Y coordinate for the ball (between 0 and 22): ";
	cin >> origY;
	cout << endl;
	cout << "Would you like the ball to move [u]p or [d]own? ";
	cin >> mov1;
	cout << endl;
	cout << "would you like the ball to move [l]eft or [r]ight? ";
	cin >> mov2;
	cout << endl;

	system("CLS");			//clear the screen
	
	x = origX;			//make x and y equal the original entries
	y = origY;

	ball[x][y];			//enter coordinate into ball array
	
	gotoxy(x, y);			//go to the coordinate
	printf("O");			//print O at the coordinate
	noOfCoords++;			//add 1 to the counter

	//move the ball once
	if (mov1 == 'u')
	{
		if (y > 0)
		{
			y--;
		}
		else
		{
			mov1 = 'd';
		}
	}

	if (mov1 == 'd') 
	{
		if (y < 22)
		{
			y++;
		}
		else
		{
			mov1 = 'u';
		}
	}

	if (mov2 == 'r') 
	{
		if (x < 79)
		{
			x++;
		}
		else
		{
			mov2 = 'l';
		}
	}

	if (mov2 == 'l') 
	{
		if (x > 0)
		{
			x--;
		}
		else
		{
			mov2 = 'r';
		}
	}

	ball[x][y];			//enter the coordinate into ball array
	gotoxy(x, y);			//go to coordinate
	printf("O");			//print O at the coordinate
	noOfCoords++;			//add 1 to the counter

	//while loop to move and bounce the ball until ball
	//returns to the original coordinates entered by
	//the user
	while ((x != origX) || (y != origY))
	{
		if (mov1 == 'u')
		{
			if (y > 0) 
			{
				y--;
			}
			else 
			{
				mov1 = 'd';
			}
		}


		if (mov1 == 'd') 
		{
			if (y < 22) 
			{
				y++;
			}
			else 
			{
				mov1 = 'u';
			}
		}

		if (mov2 == 'r') 
		{
			if (x < 79) 
			{
				x++;
			}
			else 
			{
				mov2 = 'l';
			}
		}

		if (mov2 == 'l') 
		{
			if (x > 0) 
			{
				x--;
			}
			else 
			{
				mov2 = 'r';
			}
		}

		ball[x][y];
		gotoxy(x, y);
		printf("O");
		noOfCoords++;
		
		system("CLS"); 		
	}

	//output the number of coordinates the O passed through
	cout << noOfCoords;
	cout << endl;

	system("PAUSE");

	return 0;
}

//gotoxy definition
void gotoxy(int eex, int eey)
{
	COORD coord;
	coord.X = eex;
	coord.Y = eey;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
Last edited on
Thanks for helping me get this code figured out. It was a tough assignment for me because I am a bit rusty with C++. The idea was to have the "ball" bounce around the screen then output the unique coordinates the ball visited. The user is to input the starting point and direction the ball starts. I realize I could have used enumerators and many different types of algorithms, but because I am rusty I chose to use if statements for the most part. Below I have included my full code to help anyone else with this type of problem.

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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <conio.h>
#include <windows.h>
#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
/*--------------------------- local variables ------------------------------*/

	int ball[80][23];
	void gotoxy(int, int);
	int noOfCoords = 0;
	int origX;
	int origY;
	char mov1;
	char mov2;
	int x;
	int y;

/*----------------------------- user prompt --------------------------------*/

	cout << "Enter the X coordinate for the ball (between 0 and 79): ";
	cin >> origX;
	cout << endl;
	cout << "Enter the Y coordinate for the ball (between 0 and 22): ";
	cin >> origY;
	cout << endl;
	cout << "Would you like the ball to move [u]p or [d]own? ";
	cin >> mov1;
	cout << endl;
	cout << "would you like the ball to move [l]eft or [r]ight? ";
	cin >> mov2;
	cout << endl;

	system("CLS");					//clear the screen

	x = origX;					//make x and y equal the original entries	
	y = origY;

	//initialize the ball array's elements to 0
	for(int row = 0; row < 79; row++)
	{
		for(int col = 0; col < 22; col++)
			ball[row][col] = 0;
	}
	
	gotoxy(x, y);					//go to the coordinate	
	printf("O");					//print O at the coordinate	

	//change the ball element to 1 if not 0 and increase the counter
	if (ball[x][y] == 0)
	{
		ball[x][y] = 1;
		noOfCoords++;
	}//endif
	

/*------------------------- move the ball once -----------------------------*/

	if (mov1 == 'u')
	{
		if (y > 0)
		{
			y--;
		}//endif
		else
		{
			mov1 = 'd';
		}//endif
	}//endif

	if (mov1 == 'd') 
	{
		if (y < 22)
		{
			y++;
		}//endif
		else
		{
			mov1 = 'u';
		}//endif
	}//endif

	if (mov2 == 'r') 
	{
		if (x < 79)
		{
			x++;
		}//endif
		else
		{
			mov2 = 'l';
		}//endif
	}//endif

	if (mov2 == 'l') 
	{
		if (x > 0)
		{
			x--;
		}//endif
		else
		{
			mov2 = 'r';
		}//endif
	}//endif

	gotoxy(x, y);					//go to coordinate
	printf("O");					//print O at the coordinate

	//change the ball element to 1 if not 0 and increase the counter
	if (ball[x][y] == 0)
	{
		ball[x][y] = 1;
		noOfCoords++;
	}//endif
	
/*------------------------ ALGORITHM to move ball ---------------------------*/

	//while the coordinates are not the same as entered
	while ((x != origX) || (y != origY))
	{
		//move the ball in the correct direction
		if (mov1 == 'u')
		{
			if (y > 0) 
			{
				y--;
			}//endif
			else 
			{
				mov1 = 'd';
			}//endif
		}//endif


		if (mov1 == 'd') 
		{
			if (y < 22) 
			{
				y++;
			}//endif
			else 
			{
				mov1 = 'u';
			}//endif
		}//endif

		if (mov2 == 'r') 
		{
			if (x < 79) 
			{
				x++;
			}//endif
			else 
			{
				mov2 = 'l';
			}//endif
		}//endif

		if (mov2 == 'l') 
		{
			if (x > 0) 
			{
				x--;
			}//endif
			else 
			{
				mov2 = 'r';
			}//endif
		}//endif

		gotoxy(x, y);
		printf("O");

		if (ball[x][y] == 0)
		{
			ball[x][y] = 1;
			noOfCoords++;
		}//endif
		
		
		system("CLS"); 		
	}//endwhile

	//output the number of coordinates the O passed through
	cout << endl;
	cout << "The ball crossed " << noOfCoords << " unique coordinates.";
	cout << endl;

	system("PAUSE");

	return 0;
}//endmain

//gotoxy definition
void gotoxy(int eex, int eey)
{
	COORD coord;
	coord.X = eex;
	coord.Y = eey;
	
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}//endgotoxy 
Topic archived. No new replies allowed.