How Can You Track the Path of a Destruction Droid in C++?

  • Context: Comp Sci 
  • Thread starter Thread starter Wanna-be Eng.
  • Start date Start date
  • Tags Tags
    C++ Program
Click For Summary

Discussion Overview

The discussion revolves around a programming problem involving a Destruction Droid that follows a series of commands to determine its final position on a flat moon. Participants explore how to implement a solution in C++, including handling input, calculating distances, and file operations.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant outlines the problem statement and the commands that the Destruction Droid can execute, emphasizing the need to track its final position based on these commands.
  • Another participant suggests that whether to use functions or classes in the implementation is a matter of personal preference and provides a basic algorithm for finding the nearest colony based on the droid's coordinates.
  • A participant mentions successfully writing the program but seeks guidance on accessing and modifying data from a .txt file using C++ functions.
  • Another participant responds with specific C standard library functions for file operations, recommending the use of fopen(), fclose(), and fscanf() or fread() for reading data from files.

Areas of Agreement / Disagreement

There is no explicit consensus on the best approach to implement the solution, as participants express different preferences regarding the use of functions versus classes and provide varying levels of detail in their suggestions.

Contextual Notes

Participants do not fully resolve the specifics of file handling or the implementation details of the distance calculation, leaving some assumptions about the data structure and command execution unaddressed.

Who May Find This Useful

Readers interested in C++ programming, particularly in the context of game development or simulations, as well as those looking for guidance on file handling and coordinate calculations in programming tasks.

Wanna-be Eng.
Messages
2
Reaction score
0

Homework Statement


Problem Destruction Droid Introduction Life was easy and happy on the third moon of Sirius 8: people lived and worked in the many peaceful
colonies. However, the Klingon Empire decided to extend its control to this part of the galaxy: they
landed a Destruction DroidTM
on the surface of the moon. The Destruction DroidTM
will go to one
colony and destroy it completely. If we knew which colony is targeted by the Destruction DroidTM, then
we could concentrate our forces to defend that colony. Fortunately, we have intercepted the commands
transmitted to the Destruction DroidTM, thus it is possible to determine its destination. Your task is to
write a program that determines the unfortunate colony where the Destruction DroidTM
goes.

One of the nice things about this moon is that it is almost completely flat, hence everything can be
described easily with two coordinates. East is x
direction, North is y
direction.

Input Each input file begins with a line containing four integers:

n<=500, the number of commands,
m<=100, the number of colonies,
x and y, the starting coordinates of the Destruction DroidTM

The first line is followed by n
lines containing one command each. There are 5 different commands:

LEFT p : turn left p degrees
RIGHT p : turn right p degrees
FASTER p : increase the speed with p units
SLOWER p : decrease the speed with p units
WAIT p : the Destruction DroidTM goes for p time units in the current direction with the current speed

Speed is given in coordinate units per time units. It can be assumed that the commands do not increase
the speed above 500 or below 0. After landing, the Destruction DroidTM faces North(y direction), and
its speed is 0. The commands LEFT, RIGHT, FASTER, and SLOWER are executed in zero time: the
Destruction DroidTM does not move during these commands.

The last m lines of the input describe the colonies. Each lin ebegin swith a name of length at most 20,
which does not contain any space characters. The name is followed by the two coordinates of the colony.

Output You have to output the name of the colony where the Destruction DroidTM will be after executing the
commands.
Write a newline character afterthe name ofthe colony. It canbe assumedthattheDestruction DroidTM will be at a distance of at most 10 from some colony, and there is a unique such colony. It is possible that the Destruction DroidTM goes through some other colonies while executing the commands, but we do not care about that. Sample Input 5 4 0 0

FASTER 10
WAIT 10
RIGHT 135
SLOWER 5
WAIT 28
Aaaa 0 0
Bbbb 100 100
Cccc 0 100
Dddd 100 0

Sample Output

Dddd

Homework Equations



all c++ you can get :biggrin:

The Attempt at a Solution



I was thinking about using functions. Is it necessary to use clases? I'm not sure how to make the program calculate the last position of the droid. I'll come up with some code, probably tomorrow.
 
Last edited:
Physics news on Phys.org
Whether to use functions or classes is up to you, it depends on your preference.

In the end, you will have coordinates for the position of the droid, which you need to compare to the coordinates of the colonies. I would use the following algorithm:
Code:
  int nearestColonyIndex = -1, theDistance = 0, smallestDistance = -1;
  for(i = 0; i <= count(colonies); i++) {
    theDistance = distance(droidCoordinates, colonies[i].coordinates);
    if(nearestColonyIndex == -1 || theDistance < smallestDistance) {
      smallestDistance = theDistance;
      nearestColonyIndex = i;
    }
  }
  cout << "The nearest colony is at index " << i << " and is named " << colonies[i].name << endl;

  int distance(int x1, y1, x2, y2) {
    // Calculate distance (squared) by Pythagorean theorem
    return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
  }
 
I managed to do the program, now I need to know how to access a .txt file, so i can take data from it, and also modify data. Using functions and stdio library
 
Use the functions whose prototypes are in stdio.h - fopen() for opening a file, fclose() for closing it when you are done, and either fscanf() or fread() for reading from the file. Presumably you have a textbook or at least some documentation that describes these functions and how to use them.
 

Similar threads

Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 8 ·
Replies
8
Views
12K