using arrays and functions and trying to initialize a point in the array
I am working on a computer program that is a game. this is the instructions:
The game starts with a print out of a 2-Dgrid representing the track made up of a twodimensional
array of characters (char’s) with 52 rows and 72 columns. The picture on the
following page represents the starting condition for a one-person game. The grid can be printed
to the terminal screen by writing the entire char grid to cout. If you have trouble seeing
the whole grid in a single window, try using <Control>-<right mouse button> in an XTerm
window and resizing the window to the whole screen.
The outer boundary of the track and the two impenetrable barriers are represented by ’X’’s,
the open track by spaces ’ ’, the finish line by ’F’’s (lower right hand corner) and the car,
pictured at its starting location, grid[1][1] (upper left hand corner), by an ’O’. In the following
picture, the rows go down the page top to bottom and the columns go left to right.
Here’s how the game is played. The user is continually prompted (until the game ends) for
a component of horizontal (positive is left to right) and vertical acceleration (positive is top to
bottom). (You imagine that this prompt is given once per second, that is, the user has a chance
to change the acceleration once/second.) The user responds with either -1, 0, or 1 for each of
these, effectively steering, accelerating and braking the car. If the user responds with a number
outside of this range, the car crashes and the game is over, because either the engine explodes
or the brakes and tires burn up—real concerns for real race cars.
The velocity in both the vertical and horizontal directions starts at 0 and increases or decreases
after each prompt, for example, xVelocity = xVelocity + xAcceleration, and the
same for the y direction. Then, both components of the position are changed, for example,
xPosition = xPosition + xVelocity, and the same for the y direction. A car crashes when
its position takes it outside the racetrack (except when it crosses the finish line) or into a wall or
barrier, or an improper acceleration was input. Crossing the finish line “wins” the race.
I got the board to print out but I am having trouble getting the car to print. Right now it doesn't even print in the grid it prints in front of my cout statement. Here is my code:
#include <iostream>
using namespace std;
const int ROWS = 52;
const int COLS = 72;
int xPosition = 1;
int yPosition = 1;
int yAcceleration = 0;
int xAcceleration = 0;
int xVelocity = 0;
int yVelocity = 0;
void displayRace(const char a[] [COLS])
{
for (int i = 0; i < ROWS; i = i + 1)
{ for (int j = 0; j < COLS; j = j + 1)
{ cout << a[i][j];
}
cout << endl;
}
return;
}
void initRace(char b[] [COLS])
{
for (int i = 0; i < ROWS; i = i + 1)
{ for (int j = 0; j < COLS; j = j + 1)
{
b[i][j] = ' ';
if (i == 0 || i == ROWS-1 || j == 0 || j == COLS-1)
{
b[i][j] = 'X';
}
if (i==ROWS-1 && ( j==COLS-7 || j==COLS-6 || j==COLS-5 ||j==COLS-4 || j==COLS-3 || j==COLS-2))
{
b[i][j] = 'F';
}
if ((j>=10 && j<=29) && (i>=1 && i<=35))
{
b[i][j] = 'X';
}
if ((i>=16 && i<=50) && (j>=41 && j<=64))
{
b[i][j] = 'X';
}
}
}
return;
}
int main(void)
{
char raceTrack[ROWS][COLS];
initRace(raceTrack);
displayRace(raceTrack);
char car[yPosition][xPosition];
car[yPosition][xPosition] = 'O';
cout<< car[yPosition][xPosition];
cout << "Horizontal and vertical acceleration (-1, 0, 1): ";
cin >> yAcceleration;
cin >> xAcceleration;
return 0;
}
Any help or suggestions would be greatly appreciated!!
|