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;
}
|