I'm confused on how to do this program C++

Hi, my professor told me to write this program in my Computer Science Programming Class but I have no idea on how to do this. Can someone give me tips or help me out on how to this?

Thank you

http://www.cs.csi.cuny.edu/~zelikovi/csc126/TreasureHunt.htm
Last edited on
You have some code to start with. Under The Procedures it is described how you should do it step by step. Start with (1). When have finished (1) do (2), etc.
I'm not sure how to do a loop though and not sure how to make it when you press n, s, w, e it would make plus 1 on the x or y.
Edit: Yes I see that now, use a switch. It's more efficient and will get the job done for you.
Last edited on
closed account (zb0S216C)
MMhawk607: That's a good exemplification of a poor control structure. A switch[1] is more preferable. For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
switch( Choice )
{
    case 'N': case 'n':
        ++Y;
        break;

    case 'E': case 'e':
        ++X;
        break;

    default:
        // Assume quit...
        break;
}

References:
[1] http://www.cplusplus.com/doc/tutorial/control/


Wazzak
The first thing to do is to writeout the requirements
and the 2nd thing is to put those requirements into a logical order.

The teacher has you well started already by providing a MAIN() function.

I would place the " infinite " LOOP in there - inside MAIN().
It'll run forever until the game is finished or the computer rusts.

Then place everything else into separate functions


Loop
disp msg n.e.w.s.-x to exit
get pos
update pos
calc Dist
disp dist
# 7. Decisions……………
"Your location is: ( x,y);
END OF LOOP
print num of steps
stop


Each of these steps or processes can be,
and should be separate function calls.

Loop {
DispDirMenu();
GetDirection();
UpdatePosition();
CalcDistance();
DisplayDistance();
CalcDecisions_and_PrintMsg(); // see item #7 for details
DispLocation(); // "Your location is: ( x,y);
} // end of loop
print num of steps();
} // end of main()


Remember to declare your global vars outside main
declare your local vars in main or in each func()

comment each step.

ps, using SWITCH statement is required (inside the "CalcDecisions_and_PrintMsg" functioin.

pss, keep your comments lined-up, cuz teachers hate it when they're not.


Procedures
1. You must use a LOOP
2. At the start of the loop, display the message "Please enter direction (n,s,e,w), or x to exit: ".
3. Now, get the position from the keyboard.
4. Update the Explorer’s coordinates
5. Calculate the distance from the explorer to the treasure:
Distance = sqrt(static_cast<double>((x-x1)*(x-x1)+(y-y1)*(y-y1)))

6. Display the distance from the Treasure (this information will clue the Explorer to either keep going in the same direction or switch directions).

7. Decisions……………
If the distance is greater than 8 then you display the message "you are too far from the treasure".
If the distance is greater than 4 and less or equal to 8 you display the message "you are far from the treasure".
If the distance is less or equal to 4 you display the message "you are getting closer to the treasure".
Also, check if you have reached the treasure (i.e., x=x1, and y=y1); and if so, Get out of the loop and display the message "congratulations, you have reached the treasure".

8. At the end of each loop display the message "Your location is: ( x,y); where x and y are the
Explorer’s coordinates

9. Make sure that you print out how many steps it took to reach the treasure
Last edited on
Thanks for the replies but how do you print out how many steps it took to reach the treasure part?

@Framework
Thank you, that's really helpful. :D
Last edited on
That is where analysis comes-in, and why it's critical to have your design in front of you ( not just the code).

You have to determine what constitutes a "Step"
Personally, I would say it belongs to either of the
GetDirection();
UpdatePosition();

functions.

each time you enter one of these two functions ( but only one)
then increment a counter.
int StepCntr =0;
StepCntr++;


in the function:
print num of steps();

PRINTF(\n\n My dear Professor, I have reached the goal, now give me an "A" for the class - for I have taken %d number of steps \n" , StepCntr )

RETURN


Last edited on
Hmm there's so many functions that you guys listed that I have no idea what it does cause my professor haven't taught us yet.

Like how to print the steps or update the position.

Damn this is hard >.<

Ah and thanks for the help guys for helping me on this :D
Topic archived. No new replies allowed.