Question
Behavior: This program implements a primitive game in which the player tries to find a pot of gold. Behavior: the game is played on an
Behavior:
This program implements a primitive game in which the player tries to find a pot of gold.
Behavior: the game is played on an implicit grid of 2D coordinates. The grid boundaries are:
Left: 0
Right: 10
Bottom: 10
Top: 0
Initially, the player is placed at coordinate (5,5).
The boundaries are inclusive: zero is a legal row and column coordinate; 10 is a legal row and column coordinate.
The program places the pot of gold at a random location on the grid that is distinct from (5,5). (The player has to do at least some work to get rich).
Behavior:
The program prompts the user to enter a direction to move as a single char
n (north)
s (south)
e (east)
w (west)
If any other letter is entered (except 'q'), an error message is printed and the user is re-prompted.
If a valid direction is entered, the following happens:
If the move would result in the player leaving the board, the program prints OUCH! and the player remains at his/her current location.
If the move places the player on top of the gold, the program prints HOORAY!, reports the number of moves the player made and terminates. ("OUCH" moves count as moves!)
Otherwise, the player is moved to the new location and the program prints:
GETTING WARMER! if the player has gotten closer to the gold.
GETTING COLDER! if the player is farther from the gold.
If an invalid direction is entered, the program reports invalid input and re-prompts the user.
Example run:
% ./search Direction: n GETTING WARMER! Direction: w GETTING COLDER! Direction: e GETTING WARMER! Direction: e GETTING WARMER! Direction: n GETTING COLDER! Direction: r Invalid input. Try again. Direction: e GETTING WARMER! Direction: s HOORAY! Total Moves: 7 Goodbye!
|
Library Functions To Use:
Standard Library: rand(), srand()
Starter code: you have been given (in the src file search.c) some starter code which does the following:
-
Code to handle command line arguments which allow the program to be run with pre-specified location of the pot-o-gold (vs. default randomized)
-
A function which reads and parses the user command
This code contains concepts that you DO need to become familiar with, eventually, so it is worth studying. You just won't need to add this functionality from scratch THIS time.
COMMENTS:
You do not need arrays to solve this problem! In fact, using arrays will likely make it more complicated than necessary!
However, you will not be penalized if you use arrays in an otherwise correct program since they were not explicitly forbidden ahead of time.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started