Computer Science 252
Problem Solving with Java
Spring 2014, The College of Saint Rose
Your primary task for this lab is to write a Java program that plays a "getting warmer" game. The basic idea is simple: we "hide" a point on the canvas and drag around a circle searching for the hidden point. To aid in this search, we give some feedback. When the circle is dragged closer to the hidden point, it becomes more red. When it is dragged further away, it becomes more blue. The goal is to get the hidden point to be inside the circle when it is "dropped".
You may work alone or with a partner on this lab. Only one submission per group is needed.
There are a number of lab questions and practice programs, and one programming assignment in this lab. Please refer to the "Submission Guidelines" on the course home page and syllabus for the requirements for each of these items.
Getting Set Up
To get your BlueJ environment set up for this week's lab assignment, start BlueJ and choose "New Project" from the "Project" menu. Navigate to your folder for this course and choose the name "Lab3" (no spaces) for the project.
Create a document where you will record your answers to the lab questions. If you use plain text, call it "lab3.txt". If it's a Word document, you can call it whatever you'd like, but when you submit, be sure you convert it to a PDF document "lab3.pdf" before you submit it.
Working solutions to all programs can be downloaded here.
Practice Questions and Programs
Start by taking a look at the ColorfulSunset example, which we did not talk about in class.
We see here that in addition to using the predefined color constants from the java.awt.Color class, using fixed custom colors (as in the Basketball2 example), and using completely random colors (as in the MoreColorfulSpirograph and the previous lab), we can compute colors.
Get the centering done first - that shouldn't be too hard.
Picking the greyscale colors might be less obvious, but is also not too bad. The greyscale colors are those whose red, green, and blue components are all equal. Black is (0,0,0) and white is (255,255,255), and other shades are the values in between. We'll use the distance from the origin. Use the standard Euclidean distance formula (square root of the difference in x, squared, plus the difference in y, squared).
Tips to help you out:
s = (int)(d + 25);
Note: this will truncate the number, not round it, but that's fine for our purposes.
Programming Assignment
On to the primary task. Your "getting warmer" game will work as follows.
When the game starts, a random location on the canvas is chosen for the "target", and the "searcher" circle is created, also at a random location. The searcher should be created completely within the bounds of the canvas, and should be yellow. Two text labels are added: one with a message that should start out with brief instructions, and one that will act as a scoreboard to indicate the number of targets that have been found.
To play the game, the user can drag the searcher around, trying to drop it onto the target. While being dragged, the searcher's color is changed to indicate how close to or far from the target its center is. When the searcher is right over the target, it should be red. When it is very far from the target, it should be blue. Otherwise, it should be a shade of purple: more red and less blue as it gets closer to the target, less red and more blue as it gets further away.
You already know how to compute a distance. You will need to think about how to use your computed distance to determine the color (i.e., the RGB value) to use for the searcher. It might help to treat any distance greater than 255 as a distance of 255. That would mean that the searcher remains completely blue when at any position further than 255 from the target.
Also, as you are dragging, update the message on the screen to one of "Getting warmer!", "Getting colder.", or "Holding steady...", indicating whether the last drag event brought the searcher, closer to, further from, or at the same distance from the target as the previous.
Determining "Wins"
When the searcher is being dragged and the mouse is released, check for a "win". The player wins if the target's location is contained in the searcher.
If the player wins (i) an appropriate congratulatory message should be displayed and the scoreboard should be updated to indicate the new number of targets found, (ii) a new random location should be chosen for the target, and (iii) the searcher should be colored yellow until the mouse is first pressed on it.
If the player does not yet win, a message should be displayed and the player is given another chance to drag the searcher to the target.
Demo
A working solution for the program will appear below. Click inside the applet to interact with it.
Written Design
Once again, you are to produce a written design for this program before you begin implementing it. The act of writing up the design will help you to organize your thoughts into a coherent plan, and having it critiqued/graded before coding is likely to head off some potential pitfalls before they get too entrenched into your thinking and your program. The written design will be worth 5 points for this program.
Development and Debugging Tips
One of the great advantages of learning to program in a graphical environment like that provided by Objectdraw is the visual feedback that can help you figure out what is going wrong when your program does not work. But in this case, one of the most important parts of your program, the target location, is invisible. To aid in the development, testing, and debugging of your program, you may consider these techniques:
If you do any of these, be sure to remove them before you submit your program for grading.
Submitting
Before 11:59 PM, Tuesday, February 11, 2014, submit your lab for grading. There are four things you need to do to complete the submission: (i) Copy your file with the answers to the lab questions into your project directory. Be sure to use the correct file name. If you prepared your answers in Word, export to a PDF file and submit that. (ii) Upload a copy of your lab (a .7z or .zip file containing your project directory) using Submission Box under assignment "Lab3". (iii) Demonstrate the execution of your programs for your instructor. (iv) Hand a printout of the Java files that make up the programming assignment (not practice programs) to your instructor. (2 business day grace period for demos and printouts).
Grading
This assignment is worth 75 points, which are distributed as follows:
> Feature | Value | Score |
Practice Questions and Programs | ||
Lab question 1 | 2 | |
GreyscaleMouseDroppings draws circles | 1 | |
GreyscaleMouseDroppings circles centered on point | 1 | |
GreyscaleMouseDroppings uses correct shading | 4 | |
Lab question 2 | 2 | |
Lab question 3 | 3 | |
ShadesOfBlue uses shades of blue | 2 | |
ShadesOfBlue correct shades from center | 2 | |
ShadesOfBlue works for any size canvas | 2 | |
GettingWarmer Design Document | ||
Major instance variables and named constants | 2 | |
Method descriptions | 2 | |
Picture of layout with coordinates/dimensions | 1 | |
Style, Design, and Efficiency | ||
Appropriate comments | 4 | |
Good variable names | 3 | |
Appropriate variable declarations | 3 | |
Good use of constants | 2 | |
Appropriate formatting | 1 | |
Does not generate new objects unnecessarily | 2 | |
Good overall design and methods | 2 | |
Correctness | ||
Initial message and scoreboard | 2 | |
Initial target position chosen randomly | 3 | |
Initial searcher position chosen randomly | 3 | |
Initial searcher position always entirely on canvas | 2 | |
Random positions based on actual canvas dimensions | 2 | |
Searcher can be dragged smoothly | 2 | |
Searcher color changes based on distance from target | 4 | |
Searcher color using accurate Euclidean distance | 2 | |
Message updates during dragging | 2 | |
Correct check for win | 3 | |
Message updates when searcher dropped not on target | 2 | |
Message updates on win | 2 | |
Scoreboard updates on win | 2 | |
New target position chosen after each "win" | 3 | |
Total | 75 | |