Inside the loop, you have to keep track of the current location of the Treasure Hunter as well as the location of the Treasure. Your code to increment/decrement x or y is good, as is your definition of the distance.
After computing the new location of the Treasure Hunter, call the distance calculator. If the distance > 8, print "Too far!", otherwise, if the distance is > 4, print "Far", otherwise print the distance. Hmm, if you put the call to the distance function (probably want to make that a function) at the top of the loop, before user input, the user can see how far away she is at the start, and after the move, can see whether the distance decreased or increased.
When the user takes a turn, make sure that they don't wander off of the map:
1 2 3 4
|
case "W":
case "w":
x = x - 1 < 0 ? 0, x - 1;
break;
|
(The above assumes you replace the "if /else if / else" with "switch (direction){}" .) Check for > 30 for "E" and "N" (again, assuming (30, 30) is top right).
Rather than "while (1) {}", I would use "do {} while (distance > 0);"; that's your break condition, everything else just loops around for another turn. Remember, for every iteration of the loop, you need to increase the number of moves made for the final output.
Based on your existing code, it looks like you want to exit the program when the user inputs "X". Think about what happens if some other value is input.