### Assigning weights to blank spaces and displaying them

I am supposed to create a racing game where the player will start at the highest weighted number and the finish line will be the lowest weighted number(0). I am supposed to determine the highest weighted value by having the numbers on the track increase the farther away they are from the finish line(0). I also have to make sure that it works with other tracks. Some of the tracks have corners where the cars would have to turn so not all of them are straight paths. Here is a demonstration of how it should look in the console if it was a straight path. Really confused about how I would do this and I am pretty new to C++ so if there was a simplified way of doing it that would be great, not looking for efficiency just something I can use to get the code working. Thank you!

X X X X
X 6 6 X
X 5 5 X
X 4 4 X
X 3 3 X
X 2 2 X
X 1 1 X
X 0 0 X
X X X X

Here is the code I have right now to create the racetrack and the walls(X).

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127`` ``````/** Special Weights **/ // Wall Weight Value. const int Racetrack::W_WALL = 1000000; // Finished Line Weight Value. const int Racetrack::W_FINISH = 0; // Uninitialized Weight Value. const int Racetrack::W_UNINIT = -1; Racetrack::Racetrack() : track() { // Let the initializer list construct the track vector. // Weights can not be created until the size of the track is known. weights = NULL; } /** Destroy objects and weights pointer **/ Racetrack::~Racetrack() { // An array cannot be deleted if it has not yet been created. if(weights != NULL) { // Delete each row. for(int i = 0; i < this->height(); i++) { delete[] weights[i]; } } // Delete the array of pointers. delete[]weights; } /** Read the track and add elements to vector and weights to weight array **/ void Racetrack::read(std::istream &ins) { std::string element; while(ins >> element) { for(int i = 0; i < element.size(); i++) { element = replaceStrChar(element, "T", ' '); } track.push_back(element); } this->initWeights(); } /** Convert string into character **/ std::string Racetrack::replaceStrChar(std::string str, const std::string& replace, char ch) { // Set locator equal to the first appearance of any character in replace. size_t found = str.find_first_of(replace); // While our position in the string is in range while (found != std::string::npos) { str[found] = ch; // Change the character at position. found = str.find_first_of(replace, found+1); } return str; // Return our new string. } /** Displays Track in Game Mode **/ void Racetrack::displayTrack() { for(int i = 0; i < height(); i++) { for(int j = 0; j < width(); j++) { std::cout << std::setw(3) << track[i][j]; } std::cout << std::endl; } } /** Displays the weights of the current track. Used for testing purposes **/ void Racetrack::displayWeights() { for(int i = 0; i < height(); i++) { for(int j = 0; j < width(); j++) { if(weights[i][j] > 100) { std::cout << std::setw(3) << "X"; } else { std::cout << std::setw(3) << weights[i][j]; } } std::cout << std::endl; } } /** Initialize weight values **/ void Racetrack::initWeights() { // Allocate the number of required rows (pointers) weights = new int*[height()]; //Allocate each row for(int i = 0; i < height(); i++) { weights[i] = new int[width()]; //Set each cell as uninitialized for(int j = 0; j < width(); j++) { weights[i][j] = W_UNINIT; // Set wall weights if(toupper(track[i][j]) == 'X') { weights[i][j] = W_WALL; } else if(toupper(track[i][j]) == 'F') { // Set finish line weight weights[i][j] = W_FINISH; } } } /** Assign weights to blank spaces here**/ }``````
Last edited on
It's deja vu all over again, man!
https://cplusplus.com/forum/general/284365/

You might want to poke around that thread, since a lot of the technical work has already been done.

Plus,
 Please learn to use code tags, they make reading and commenting on source code MUCH easier. How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/ There are other tags available. How to use tags: http://www.cplusplus.com/articles/z13hAqkS/ HINT: you can edit your post and add code tags. Some formatting & indentation would not hurt either
I came across that post and I saw people talk about using pythagorean theorem to do it, but I was really confused on how I would go about implement that. I’m completely new to algorithms and was quite lost with the post that was later made on that thread. Also thank you for the formatting resources.
When @Duthomhas says "use pythagorean theorem", he means use straight-line distance from point A to point B. The problem is that straight-line distance will be too optimistic if you have to drive around an obstacle on the path from A to B.

Anyway if you have specific questions, feel free to ask
Last edited on
Topic archived. No new replies allowed.