0% found this document useful (0 votes)
59 views6 pages

Od 25 Simonhandout

- The document describes an assignment to create a Simon game program in Java using object-oriented design principles. Students are tasked with writing five main classes: SimonController, NoisyButton, ButtonCollection, Song, and SongPlayer. - The NoisyButton class represents the game buttons and allows flashing and playing sounds. ButtonCollection and Song will manage collections of buttons using arrays and ArrayLists. SongPlayer will play sequences stored in Song objects. - SimonController will create the game buttons, respond to clicks, and start new sequences by adding buttons to songs. It uses the other classes to manage button collections and play sequences for the player to repeat.

Uploaded by

api-304906907
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views6 pages

Od 25 Simonhandout

- The document describes an assignment to create a Simon game program in Java using object-oriented design principles. Students are tasked with writing five main classes: SimonController, NoisyButton, ButtonCollection, Song, and SongPlayer. - The NoisyButton class represents the game buttons and allows flashing and playing sounds. ButtonCollection and Song will manage collections of buttons using arrays and ArrayLists. SongPlayer will play sequences stored in Song objects. - SimonController will create the game buttons, respond to clicks, and start new sequences by adding buttons to songs. It uses the other classes to manage button collections and play sequences for the player to repeat.

Uploaded by

api-304906907
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Programming Exercise:

ObjectDraw 24: Simon


Type Classwork
Assigned: 12-01-14
Due: 12-17-14
Objective: To gain experience working with arrays and ArrayLists.
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 depressing 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.
For this laboratory exercise, we would like you to write a Java program to allow one to play a simple
game like Simon. Like the original, our game will involve four buttons which the player will have to press
in an order determined by the computer. Given the limitations of Javas layout managers, we will keep the
graphics simple by simply placing the four buttons in a 2 by 2 grid as shown below.

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.
Your program will consist of five main classes:
SimonController will extend Controller rather than WindowController. Recall that the only difference
between a Controller and a WindowController is that the latter comes with the canvas installed.
The canvas is not needed for this program as we will not be doing any drawing.
NoisyButton will describe buttons that act like those found on a Simon game. We will provide a complete
implementation of the NoisyButton class as part of the starter folder for this lab. The details of how
to use our code are provided in the following section.
ButtonCollection will manage the set of four NoisyButtons that form the game board.
Song will manage the sequence of buttons/tones corresponding to the song played by the game which
the player needs to repeat.
SongPlayer will be a class that extends ActiveObject. It will be used to actually play the Song.
NoisyButton To complete this lab, you will use a class we have provided named NoisyButton. While
NoisyButton is not part of the standard Swing GUI library, objects of the NoisyButton class can be used
as GUI components. You can add a NoisyButton to your Controllers content pane or to a JPanel just
as you could add a JButton or a JComboBox. You can also specify how to react when someone clicks a
NoisyButton by providing a listener for such events.
The NoisyButton class provides two methods that you will use in your program. The first method is
named flash(). It makes the button flash and plays the sound associated with the button. The second
method is named addListener and is used to identify the object that should be notified when the user clicks
on a NoisyButton.
The header for the method used to add a listener to a NoisyButton is
public void addListener(NoisyButtonListener listener)
An object that listens to a NoisyButton must implement an interface, NoisyButtonListener, which is included
in our starter project and defined as:
public interface NoisyButtonListener
{
// Method invoked when a NoisyButton is clicked
public void noisyButtonClicked(NoisyButton source);
}
When someone clicks on a NoisyButton, the button will invoke the noisyButtonClicked method of the
object that has been added as a listener. We expect you to use your SimonController as the listener, so you
should define a noisyButtonClicked method in that class. When this method is invoked, the NoisyButton
that has been clicked will be passed as a parameter.
The constructor for the NoisyButton class expect two parameters: an AudioClip for the sound the button
should make when pressed and its Color. The colors you use should be somewhat dull shades of red, blue,
yellow and green. We suggest using new Color(180,0,0), new Color(0,180,0), new Color(0,0,180),
and new Color(180,180,0).
We will also provide four audio files you can use for the noises the buttons make and a fifth file for the
razzing sound made when the player goofs. The files tone.0.au, tone.1.au, tone.2.au and tone.3.au
describe the sounds the NoisyButtons should make. The file razz.au contains the unpleasant noise your
program should make when the user goofs. (We will leave it to you to guess who sat around making funny
noises to produce this file.) You can use the getAudio method associated with the Controller class
to access these files. Like getImage, this method expects a string that names a file as a parameter. The
2

button sounds will be used by the code we have provided within the NoisyButton class. You will use the
built-in play method to produce the razzing sound. If you declare a variable as:
AudioClip nastyNoise;
and in your Controller you assign it a value using:
nastyNoise = getAudio("razz.au");
then you can say:
nastyNoise.play();
when you want to make a razzing sound.
Collection classes The goal of this lab is to exercise your knowledge of arrays and ArrayLists. From this
point of view, the most interesting aspect of this assignment will be the implementation of two classes that
will maintain collections of NoisyButtons. One of these classes will use an array, while the other will use an
ArrayList.
The first of these two classes will be aptly named ButtonCollection. It will simply hold the four
buttons that appear on the screen in an array. Its most important feature will be a method that will return
a randomly chosen button from the collection.
The other collection class you implement will be used to hold the sequence of buttons the user is currently
being asked to repeat. This class will be named Song.
One method you will need to include in your ButtonCollection is a method that will return a NoisyButton
randomly chosen from the collection. Each time you start a new song or the user correctly repeats the existing song, you will use this method to select a random button and then add this button to the current
Song.
Both the ButtonCollection and Song class will need to provide methods that can be used to add
NoisyButtons to a collection. As you create the NoisyButtons in your begin method and add them to the
display you will also add them to your ButtonCollection. The standard Simon game only has four buttons,
so an array of four elements will be sufficient to implement the ButtonCollection class.
On the other hand, you may add more than four buttons to the current Song. You will add a button to
the Song whenever the player correctly repeats the current sequence. It is hard to predict how often a good
player might do this, so use an ArrayList so that you may add as many buttons to the Song as you need.
The Song class will need to provide several other methods that can be used to step through the sequence
of buttons the user is supposed to repeat. These methods are most easily understood in the context of the
classes that will use them, the SongPlayer and SimonController classes, so we will discuss them in the
following sections.
The SongPlayer class Your Song class will manage the sequence of tones corresponding to the song
played by the game, but you will need a separate SongPlayer class to play the song. The reason you need
to create a separate class to play all the notes in a Song is that you need to pause between the individual
notes (and it will be best if you also pause for a second or so before beginning to play the song). The
pause method can only be used within an ActiveObject andthe Song will not extend ActiveObject. The
SongPlayer class will be a class that extends ActiveObject. Therefore, whenever you want to play a Song,
you will create a SongPlayer to actually do the work. The SongPlayer constructor will take a Song as a
parameter. The SongPlayers run method will contain a loop that alternately flashes a NoisyButton and
then pauses for a short period.
To write this loop, you will need a way to access the NoisyButtons stored in the Song from the SongPlayer
class. The ArrayList in which the NoisyButtons are actually stored will be a private instance variable within
the Song class. It cannot be accessed directly from the SongPlayer. Instead, you will need to include a
method within the Song class that returns an Iterator for the ArrayList.

When your SongPlayer needs to iterate through the entire Song, you can get an Iterator for the Song)s
ArrayList, and use the Iterators hasNext() and next() methods to iterate through the entire
song Song.
The SimonController class To complete this program, you will need to construct a class that
extends Controller that will act as your main program. The begin method for this class
will create the four NoisyButtons. It will also establish the controller class as a listener
for the buttons and add them to the display and a ButtonCollection. In addition, it should
create and play a Song containing just one note.
The other important method in the SimonController will be the noisyButtonClicked method.
Each time a button is clicked you will need to determine whether or not the right button was
clicked. To do this, you will have to know which button is expected next. If the user clicks
the wrong button, you will make a nasty noise and start over by creating a new one-note song.
If the user clicks the right button, you will have to determine whether the user has repeated
the entire Song so that you can decide whether to wait for additional clicks or to add a note
to the Song.
Basically, to write noisyButtonClicked, you need a way to step through the notes of the
Song one by one, much as you do this to write the loop that plays the song in SongPlayer. You
will find that you can use the same method described in our discussion of the SongPlayer class
to get an Iterator for the ArrayList. However, instead of iterating through the entire ArrayList,
youll just want to move forward by one element each time the user clicks a button. If the
user gets to the end of the Song without making a mistake, your code should add a new, random
NoisyButton to the end of the Song and play the entire song again from the beginning.
Implementation. As usual, we suggest a staged approach to the implementation of this program.
This allows you to identify and deal with logical errors quickly.
Constructing the appropriate screen display is a good place to start. The size of your
programs window should be 400 pixels wide by 400 pixels tall. Write and test the portion
of the begin method needed to create the four NoisyButtons and add them to the display.
You will need to get the audio clips to do this. Dont try to add listeners or put the
NoisyButton in a ButtonCollection at this point.
Once the buttons are displayed, you should make sure they can flash. Test this by adding
a noisyButtonClicked method to your SimonController that simply flashes the button passed
to it as a parameter. Add the controller as the listener for the NoisyButtons. Then,
click and see if it works.
Next implement the ButtonCollection class and see how good it is at picking random buttons.
Modify your begin method to place your four NoisyButtons in a ButtonCollection. Then
modify your noisyButtonClicked method so that each time you click it flashes a button
randomly chosen by the ButtonCollection rather than the one you clicked on.
Now, define the Song class. It is a bit hard to test this class one piece at a time because
of the close relationship between the Song class and the SongPlayer class, it is probably
best to define and test the two classes at the same time. Once you think both classes
have been written, you can change your begin method so that it creates an empty Song and
change the noisyButtonClicked method so that each time you click a button, you add that
button to the Song and then play the entire Song. It the Song and SongPlayer classes
are correct, each time you click a button, the program should repeat the sequence of all
the buttons you have clicked.
Of course, this is backwards. The player is supposed to repeat what the computer did,
not the other way around! So...
4

Once you think the ButtonCollection, Song and SongPlayer classes are functioning correctly,
it is time to construct versions of the begin and noisyButtonPressed methods that will
play Simon as expected. In begin, you will need to construct a Song consisting of just
one note and play it. In noisyButtonPressed, you need to check whether the expected button
was pressed and whether it was the last button in the Song and then either wait for the
next click, make a nasty noise and start over or add a note to make the song longer and
play it.
As an extra touch:
One odd bit of behavior your program will exhibit is that it will get very confused if
you click a button before the computer has finished playing the sequence it wants you
to complete. Try it and think about what is going wrong.
To fix this behavior, you have to ignore clicks on the buttons while a SongPlayer is actively
playing a song. You can do this by adding a boolean instance variable to your SimonController
that keeps track of when a SongPlayer is active. It is easy to set this boolean to true
when you create a SongPlayer. It is a bit harder to have it become false when the SongPlayer
is done.
Submitting Your Work Before turning in your work, be sure to double check both its logical
organization and your style of presentation. Make your code as clear as possible and include
appropriate comments describing major sections of code and declarations.
Skeleton of the NoisyButton class provided in the starter
public class NoisyButton ...
{
// construct a new NoisyButton
// noise determines the tone played when the button is clicked
// shade determines the unhighlighted color of the button
public NoisyButton(AudioClip noise, Color shade) { ... }
// Assign an object to listen for when someone clicks on the button
public void addListener(NoisyButtonListener listener) { ... }
// Make the button flash by creating an ActiveObject that does the work
public void flash() { ... }
}
Startup file for SimonController We will provide you with the following start-up file to help
you get going with the SimonController class:
import
import
import
import

objectdraw.*;
java.awt.*;
javax.swing.*;
java.applet.AudioClip;

// name, lab, etc


public class SimonController extends Controller implements NoisyButtonListener {
private static final int BUTTONCOUNT = 4; // the number of distinct sounds
// corresponding to the game buttons

private static final int COLORINTENSITY = 180; // how bright to make buttons
private AudioClip nastyNoise; // a razzing noise

// create the display of four buttons on the screen


public void begin() {
// buttons should appear in a grid
getContentPane().setLayout(new GridLayout(2,2));
// load the nasty noise
nastyNoise = getAudio("razz.au");
// create an array of colors for the buttons
Color shades[] = new Color[BUTTONCOUNT];
shades[0] = new Color(COLORINTENSITY, 0, 0);
shades[1] = new Color(0, COLORINTENSITY, 0);
shades[2] = new Color(0, 0, COLORINTENSITY);
shades[3] = new Color(COLORINTENSITY, COLORINTENSITY, 0);

for (int buttonNum = 0; buttonNum < BUTTONCOUNT; buttonNum++) {


AudioClip curSound = getAudio("tone." + buttonNum + ".au");
// create and add buttons
}
// add the panel of buttons to the window
validate();
}
// Check to see if the player clicked the expected button
public void noisyButtonClicked(NoisyButton theButton) {
/*
if (theButton == "the expected button") {
// player got it right
} else {
// player goofed
}
*/
}
}

You might also like