Overview
For this assignment, your program will control a Player that is trying to move
to a target location of 40.
With the Player starting at zero, the program reads moves from the user
until the target is reached or there are no more inputs.
After each move, the number of steps that have been taken is printed along with
a graphical representation of the Player's location.
This program uses a Player class created for this course.
Using the Player class
After you have created a project for this lab,
you'll need to copy the Player class source code from my files into your file space.
To copy the Player.class file from my directory, be in your 161/wander directory and type:
cp ~hutchens161/examples/Wander/Player.java .
Be sure to type the space and then the period at the end of that command.
That's a tilde (~) before my userid.
Now you have a copy of that file in your own directory.
However, do not change it.
You will need to select File/refresh from the File menu after you copy this file into your project.
Your program must work correctly with my unmodified version of the class.
The code for the class was distributed with this assignment.
Realize that you can use the class without knowing how it works.
You are not responsible for building a class at this time.
Remember that each object (also called an instance) of a class has its own copy of the values that contain its state. Those are its instance variables. You call the methods that perform the actions of the class (and usually modify the instance variables doing it) by giving the name of the object (not the class) followed by a dot, followed by the name of the method. This is a method, so you need parentheses even if there are no parameters.
To declare a Player object named Lumpy with a target of 20, I would write:
Player Lumpy = new Player(20);
To move Lumpy four spaces forward and then two spaces backward, I would write:
Lumpy.move(4);
Lumpy.move(-2);
To print Lumpy's current location, I would write (saving the result in a variable):
int loc = Lumpy.currentLocation( ); // declare loc if necessary
System.out.println( "Lumpy is at " + loc );
What the program must do
Declare a Player object with a target of 40.
Then, while the Player hasn't reached the target and there is still good input,
move the Player.
After each move, draw a picture of where the Player is and report on how many steps have been taken.
When you run out of data or the Player reaches the target, report on what happened and how many steps
were taken before it happened.
How the program must do it
Your program must use the Player class.
It may not keep its own variables for location, target, or steps taken.
Use the methods to get that information as needed.
You're still learning how to do input loops, and this one is a little complex.
Suppose your Player object is named Pooh. You need to ask if he has reached his target
and to keep going if he has not. If he must continue, you need to prompt the user for
another move. Try:
while ( ! Pooh.targetReached( ) && hasNextIntWithPrompt(in) ) {
Inside the loop, read the amount, move the Player and print his current status.
To make this work, you will need to write a boolean valued method hasNextIntWithPrompt that takes a Scanner as its parameter, prints a prompt, and then returns the result of hasNextInt on the Scanner.
Use a method to print the Player's status - location and number of steps. Also use a method to graphically show where the Player is. Print a symbol for each location he's passed. If he is at location 5, print 5 symbols. Use a for loop for that. The status method would call the graphical location method. Remember that each method should do one action well. Don't pile lots of different actions in one method.
When you are done with the program, submit it as the wander assignment. Note that submit will also take the Player.java file.
Sample runs
Player must reach 40 to win. Player at 0 () after 0 steps. Next move: -2 Player at 0 () after 1 steps. Next move: 17 Player at 17 (PPPPPPPPPPPPPPPPP) after 2 steps. Next move: -2 Player at 15 (PPPPPPPPPPPPPPP) after 3 steps. Next move: 20 Player at 35 (PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP) after 4 steps. Next move: 4 Player at 39 (PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP) after 5 steps. Next move: -1 Player at 38 (PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP) after 6 steps. Next move: 6 Player at 40 (PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP) after 7 steps. Player reached target. Player at 40 (PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP) after 7 steps. Another run Player must reach 40 to win. Player at 0 () after 0 steps. Next move: 7 Player at 7 (PPPPPPP) after 1 steps. Next move: -3 Player at 4 (PPPP) after 2 steps. Next move: ^D Player ran out of data before reaching target. Player at 4 (PPPP) after 2 steps.