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).
/** Special Weights **/
// Wall Weight Value.
constint Racetrack::W_WALL = 1000000;
// Finished Line Weight Value.
constint Racetrack::W_FINISH = 0;
// Uninitialized Weight Value.
constint 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 = newint*[height()];
//Allocate each row
for(int i = 0; i < height(); i++)
{
weights[i] = newint[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;
}
elseif(toupper(track[i][j]) == 'F')
{
// Set finish line weight
weights[i][j] = W_FINISH;
}
}
}
/** Assign weights to blank spaces here**/
}
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