LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-10-2005, 08:36 PM   #1
mshinska
LQ Newbie
 
Registered: Oct 2005
Posts: 14

Rep: Reputation: 0
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!!
 
Old 11-11-2005, 02:21 AM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
I have a suggestion: if you want people to actually read your code, post it in code tags. And don't repost the code, edit your original post instead (you'd be surprised how many people seemingly don't know that you can do that).

Other than that, this smells like homework, so I'm not gonna' say anything more about it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
arrays of elements with [gcc4]array type has incomplete element type lmmix Linux - Software 0 02-26-2005 08:07 AM
Arrays of Functions and Classes problem. (C++) -=zAe=- Programming 2 02-24-2005 10:21 AM
array functions in c djgerbavore Programming 6 01-07-2005 03:28 PM
php: array with functions ldp Programming 7 09-22-2004 04:55 PM
Problem in C: arrays, functions and strings OrganicX Programming 15 03-18-2003 09:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration