CSC 120A Homework Assignment 7 - Fall 2004

[Course home page]

Due: Monday, November 1, 2004

The goals of this assignment are to have you practice writing a variety of patterns of loops, to give you practice with dividing your program into manageable subtasks (i.e., methods), and to continue to develop your problem solving skills. This assignment will take more thought about design and implementation than previous ones so start working on it early.

Assignment:

Your assignment is to write a program that involves creating a game. Most reasonable games are complicated and difficult to program, especially if you need to design a strategy so that the computer can play. Thus, this game will be a bit simplistic and not very interesting in the long run, but hopefully it will be amusing for a while.

Suppose the computer has a square of 100 x 100 pixels and within that 100 x 100 square will be a single needle, randomly situated. The goal of the game is for the human player to find the location of needle in as few guesses as possible. At each turn, the player makes a guess of two integers (x, y) representing the point where the player thinks the needle is situated. We'll use a standard Cartesian coordinate system from math class. The x-value represents the horizontal coordinate and the y-value the vertical. The point (0,0) is in the bottom left of the square and x and y values increase to the right and upwards.

Once the player has made a choice, the computer could just say "Yes, you found it" or "No, it's not there" but that would be a very boring game because the human player would just have to make lots of random guesses. So, instead, once the player makes a guess, the computer will either say (print out on the screen), "Yes, you found it" (and the game is won) or it will give one of the following eight directional clues:

"No, the needle is Northeast"
"No, the needle is Northwest"
"No, the needle is Southeast"
"No, the needle is Southwest"
"No, the needle is North"
"No, the needle is South"
"No, the needle is East", or
"No, the needle is West"

From these directional clues, the human player will be able to make better guesses. To better explain, here's a sample image (this image is just for demonstration purposes, your program does not have to produce any graphics):

In this sample image, the needle is at (25,17) and the player has guessed (72,33). So the computer should tell the player that the needle is Southwest of the guess.

Besides playing a single game as described above, your program will also need to be able to play multiple games and keep track of:

Sample Output

Here is a run of my solution to this assignment. You can use it to get an idea of how your program should run. Notice, the user should only be able to input coordinates between 0 and 99, inclusive. Also, if the user inputs a negative number for either the x or y coordinate, the game should immediately end after printing statistics for the total games played. If the user gives up, that game should not be counted in the statistics. Also, the average score and best score statistics should only be printed if one or more games have been played.
> java NeedleInAHaystack
Welcome to NEEDLE IN A HAYSTACK!!!

Starting a new game...

What is your guess for the x coordinate? (-1 to give up) 565
What is your guess for the y coordinate? (-1 to give up) 3
Please enter only coordinates between 0 and 99, inclusive
What is your guess for the x coordinate? (-1 to give up) 50
What is your guess for the y coordinate? (-1 to give up) 50
No, the needle is Northwest
What is your guess for the x coordinate? (-1 to give up) 25
What is your guess for the y coordinate? (-1 to give up) 75
No, the needle is Southwest
What is your guess for the x coordinate? (-1 to give up) 15
What is your guess for the y coordinate? (-1 to give up) 65
No, the needle is Northeast
What is your guess for the x coordinate? (-1 to give up) 20
What is your guess for the y coordinate? (-1 to give up) 70
No, the needle is Northwest
What is your guess for the x coordinate? (-1 to give up) 17
What is your guess for the y coordinate? (-1 to give up) 72
No, the needle is East
What is your guess for the x coordinate? (-1 to give up) 18
What is your guess for the y coordinate? (-1 to give up) 72
Yes, you found it!

That took you 6 guesses.

Total games played: 1
Average score per game: 6.0 (less is better)
Best score: 6

Do you want to play another game? (y/n) y

Starting a new game...

What is your guess for the x coordinate? (-1 to give up) 505
What is your guess for the y coordinate? (-1 to give up) 50
Please enter only coordinates between 0 and 99, inclusive
What is your guess for the x coordinate? (-1 to give up) 50
What is your guess for the y coordinate? (-1 to give up) 50
No, the needle is Southwest
What is your guess for the x coordinate? (-1 to give up) 25
What is your guess for the y coordinate? (-1 to give up) 25
No, the needle is Southeast
What is your guess for the x coordinate? (-1 to give up) 30
What is your guess for the y coordinate? (-1 to give up) 15
No, the needle is South
What is your guess for the x coordinate? (-1 to give up) 30
What is your guess for the y coordinate? (-1 to give up) 10
No, the needle is South
What is your guess for the x coordinate? (-1 to give up) 30
What is your guess for the y coordinate? (-1 to give up) 5
No, the needle is South
What is your guess for the x coordinate? (-1 to give up) 30
What is your guess for the y coordinate? (-1 to give up) 1
Yes, you found it!

That took you 6 guesses.

Total games played: 2
Average score per game: 6.0 (less is better)
Best score: 6

Do you want to play another game? (y/n) y

Starting a new game...

What is your guess for the x coordinate? (-1 to give up) 50
What is your guess for the y coordinate? (-1 to give up) 50
No, the needle is Southeast
What is your guess for the x coordinate? (-1 to give up) -1

Total games played: 2
Average score per game: 6.0 (less is better)
Best score: 6


Thanks for playing!
    

Design

For this assignment, you had better put more effort into designing the solution first before you actually start writing Java code. While thinking through the design, try to focus only on what needs to be done, rather than how to achieve that using Java syntax. Your design should be an outline of how you are going to go about writing the program code. It is like making an outline for an English paper -- you'll rarely see a well-written paper that was not outlined first.

For your design, you may use a flowchart or just an outline of steps using English phrases. When putting together the outline, you're just interested in questions like "What should I do next?" Try not to go off on tangents into the details, like how exactly a loop will have to work. It is not easy to design solutions for programming problems -- it's a skill that has to be learned -- so just do your best.

If you have questions about this assignment, I will want to see your design outline first. That lets me know how you've organized your solution so that I can more easily determine if you have a design problem or a coding problem. If you have a coding problem, it is easier to correct the program if I've seen your design and understand what you're trying to accomplish.

Coding

Here are some general suggestions when you start implementing your program:
Method decomposition:

As the programming assignments become more involved, it will be useful to develop your code so that portions of your design can be implemented and tested one at a time. You should be able to subdivide your code into a series of subtasks -- i.e. interacting methods. Each method may accomplish one task that is listed on your design solution. For example, you may have one method that gets input from the user, one that generates a random location for the needle, etc. As another example, if you have thought out your design well, you should be able to write a method that plays a single game first and then easily expand it to handle multiple games. The overall method for playing a game will call other methods to place the needle, get input, etc.
 

Debugging printouts:

Once you've gotten your program to compile, your output may not produce what you expect. You will then have to debug your code. As we've discussed several times in class, one of primary techniques for doing so is inserting extra println statements in your code to try and isolate where logical errors are occurring. After you've gotten your program working, you can go through and comment out these extra print statements. To help distinguish debugging printouts from the normal output of your code, you may want to use statements of the form:

System.out.println("DEBUGGING: " + method name + " >>> " + local details)
 

Randomization:

To generate random values for the needle position, the Math.random() function will probably be useful (see Table 3.1 on page 123 of the textbook). However, while it will only be fun to play the game if you don't know the answer ahead of time, it will be hard to debug your code if you're not sure what the correct answer is supposed to be. Therefore, in the initial stages of debugging, you may want to just set the position of the needle using some constant values so that you can reliably test the behavior of your program (such as whether the hints it gives are correct).
 

Hand in

Hand in a printout of all the commented Java program files that are necessary to make your program run correctly, and also submit them online through VikingWeb.

 

Acknowledgments: This assignment was prepared based on one developed by Dr. Jeffrey Carroll (CMU).

 



Last modified: Wed Nov 24 12:54:47 EST 2004