Computer Science 252
Problem Solving with Java

Fall 2014, The College of Saint Rose

Lab 9: Simon
Due: 11:59 PM, Sunday, November 23, 2014

For this lab, you will get some practice using ArrayLists by enhancing a class example, then you will use them to complete a (mostly-written) program that plays the children's memory game called Simon.

You may work alone or with a partner on this lab.

Getting Started

Start by downloading the folder containing two starter BlueJ projects. Download and extract this archive and you should see two folders: CourseGrades, which is a copy of the class example you will be extending for the practice program, and Simon, which is the game you will be completing for the programming assignment.

Create a document where you will record your answers to the lab questions. If you use plain text, call it "lab9.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 "lab9.pdf" before you submit it.

A working solution for the programming assignment can be downloaded here.

Practice Program: Enhancing the CourseGrades Example

In the starter, open the BlueJ project for the copy of the CourseGrades example, and read through the existing code to make sure you understand how it all works. You will be making several enhancements to this program:

Setting up for Sounds: JFugue

Your second program has the option to use sounds. It uses a library called "JFugue" from David Koelle to make this task easier. Much like objectdraw, this library is not part of a standard Java or BlueJ installation, so you will need to add it.

First, visit http://www.jfugue.org/download.html and download the first file in the list (jfugue-4.0.3.jar). Save this to your account and then add it to BlueJ's list of libraries. You can accomplish this by going into BlueJ's preferences, choosing "Libraries", then adding the path to the JFugue library to the list of User Libraries. After doing this, quit and restart BlueJ so it can load the library correctly.

Make sure you have done this correctly by downloading, compiling and running the SoundPlayerDemo example on the computer where you are doing your work for this lab.

Programming Assignment: Completing the Simon Game

Many of you are probably familiar with the electronic toy named "Simon". Simon is a simple solitaire memory game. The toy is composed of a plastic base with four colored plastic buttons on top. Each button has a different color, and a different musical note is associated with each button. The toy "prompts" the player by playing a sequence of randomly chosen notes. As each note is played, the corresponding button is illuminated. The player must then try to play the same "tune" by pressing the appropriate buttons in the correct order. If the player succeeds, the game plays a new sequence identical to the preceding sequence except that one additional note is added to the end. As long as the player can correctly reproduce the sequence played by the machine, the sequences keep getting longer. Once the player makes a mistake, the machine makes an unpleasant noise and restarts the game with a short sequence.

Your task is to implement two classes that will complete a program to play a simple game like "Simon". Like the original, our game involves four buttons which the player will have to press in an order determined by the computer. We will keep the graphics simple by placing the four buttons in a 2 by 2 grid.

As soon as the buttons are displayed, your program should generate a sequence consisting of a single note/button. It should "play" a sequence by briefly highlighting the buttons that belong to the sequence in order. After a sequence is played, your program should wait while the player tries to repeat the sequence by clicking on the buttons in the appropriate order. If the player repeats the sequence correctly, the program should randomly pick a button to add to the end of the sequence and "test" the player on this new sequence. If the user makes a mistake, the program makes a "razzing" sound and then starts over with a one note sequence.

While this is a complex program with several classes that interact with each other, your task is limited to the management of the "songs" that are played by the Simon game.

Provided Classes

Several classes are provided, most of which you will not need to modify or even look at (unless you wish to do so). The SimonController manages the game, NoisyButton and NoisyButtonListener provide the capability for our big, colorful buttons to be pressable, and the ButtonCollection keeps track of the four buttons and is able to pick one of the buttons it contains randomly.

Classes to be Completed

The Song and SongPlayer classes are where you will need to do your work. A skeleton of each class is provided.

The Song class needs to keep track of the sequence of notes/buttons that makes up the song that the player is trying to match.

The last three methods are used by the SimonController to keep track of whether the song being "entered" by the player trying to match what Simon played is correct. To do this, your Song class will need to keep track of a "current position" which indicates the next note in the song that should be played to continue to match the song.

The SongPlayer class is used to play the current song to the player of the game so he or she has a chance to repeat it back. Since we need to be able to do things like play notes and flash buttons for a given amount of time and pause between the notes, this needs to be an ActiveObject.

The provided code includes all of the instance variables you need and a complete constructor. Your task is to fill in the run method to pause for one second, then play each note in the song. This is done by calling each NoisyButton's flash method. When all of the notes have been played, the run method calls the SimonController's playComplete method to inform the game that it is OK to allow the player to start pressing buttons to try to match the sequence just played.

Fill in these methods. When your Song and SongPlayer classes work properly, you should be able to play Simon. If you have trouble with the sounds or just prefer to work quietly, you can set the PLAY_NOISE constant in the NoisyButton class to false.

You need not comment classes you did not modify, but you should augment the comments in the Song and SongPlayer classes to describe in some detail how they work. You should also make sure you name(s) are in each of those files.

Submitting Your Work

Before 11:59 PM, Sunday, November 23, 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 "Lab9". (iii) Demonstrate the execution of your programs for your instructor. (iv) Hand a printout of the Java files that make up the programming assignment to your instructor. (2 business day grace period for demos and printouts).

Grading

This assignment is worth 55 points, which are distributed as follows:

> FeatureValueScore
CourseGrades Program Enhancements
CourseGrades average command 5
CourseGrades delete command 5
CourseGrades stats command (basic) 4
CourseGrades stats command handles ties 3
CourseGrades updates to help command 2
Lab question 2
Simon
Documentation 5
Appropriate variable declarations 3
Naming conventions 2
Formatting 1
Song class ArrayList declaration 2
Song class ArrayList construction 2
Song class add method 3
Song class play method 3
Song class restart method 2
Song class atEnd method 2
Song class getNextButton method 4
SongPlayer class run method 5
Total 55