Introduction

The goal of this project is to implement a program that allows the user to play a version of the Simon game. In this version, the game has one row of colored squares. The game starts when the program flashes a random sequence of the squares. Then the user is supposed to click on the squares in the same exact order. If the user succeeds, then the game should repeat with a sequence that is one flash longer. The game ends when the user clicks the squares in the wrong order.

There should be anywhere from 2 to 6 squares in the window (determined by user input). The first random sequence should be a sequence of two flashes. The length of the sequence should increment by one each time the user is successful.

You can start your project by downloading project3.zip. This file contains DrawingPanel.java and an initial version of Simon.java. You should understand what this code is doing before making changes.

Simon.java

The initial version of Simon.java implements the following pseudocode:

Initialize the window with two colored squares.
Loop while the user correctly clicks the sequence:
Decide on the sequence of two squares to be flashed.
Flash the two squares.
Get two clicks from the user and determining which squares were clicked.
Test if the user's clicks were correct.
Write a message on the window.

You should study the code in Simon.java to gain an understanding of how the code implements the above behavior. Describe what is stored in each variable. How does the program know which squares the user clicked? How does the program know which squares are correct?

The goal is to allow anywhere from 2 to 6 colored squares. The comments in Simon.java describe what changes should be made. This includes the following:

Ask the user for the number of squares to display. The user should be prompted until an integer from 2 to 6 is entered. Below this value will be called numberOfSquares. This should not crash if the user does not enter an integer. You should use the getInt method from the book (p. 338) so that you can implement this pseudocode:

Use the getInt method to get an integer from the user.
While the integer is invalid:
Use the getInt method to get another integer from the user.

The width of the DrawingPanel should depend on numberOfSquares. Pseudocode:

Calculate width from numberOfSquares.
Create a DrawingPanel with that width.

There should be a loop drawing the outline of numberOfSquares squares. There should be an array that contains the values of the upper left corner x-coordinates (the first parameter to the drawRect method in the sample code). The length of this array should be numberOfSquares. Pseudocode:

Create an int array named xArray with length numberOfSquares.
For i from 0 to numberOfSquares - 1:
Set index i of xArray to the upper left corner x-coordinate of square i
Use the Graphics drawRect method to draw the outline of the square.

There should be a loop to fill the squares with different colors. You should have an array of 6 colors (the sample code contains an array of 2 colors). You need to use the x-coordinate array described above for the fillRect method. Pseudocode:

Create an array of 6 colors.
For i from 0 to numberOfSquares - 1:
Use the Graphics setColor method to set the color to color i.
Use the Graphics fillRect method to fill square i with color i.

In the userSucceeds loop, rand.nextInt(2) should be replaced with rand.nextInt(numberOfSquares).

In the userSucceeds loop, the first square0 == 0 if statement can be replaced with a single assignment using the x-coordinate array.

In the userSucceeds loop, the second square0 == 0 if statement can be replaced with a single statement using the colors array.

In the userSucceeds loop, the first square1 == 0 if statement can be replaced with a single assignment using the x-coordinate array.

In the userSucceeds loop, the second square1 == 0 if statement can be replaced with a single statement using the colors array.

The getSquareClicked method should have additional parameters, numberOfSquares and the x-coordinate array. That is, its header should be 11. public static int getSquareClicked(int x, int y, int numberOfSquares, int[] xArray)

The calls to getSquareClicked must be modified accordingly. This method should loop numberOfSquares times, looking for the square corresponding to x and y. This method should correspond to the following pseudocode:

For i from 0 to numberOfSquares - 1:
If x and y are in square i:
Return i.
return -1

The final result of your lab should be a program that can display anywhere from 2 to 6 squares, but each turn of the game will only flash two squares. We want to increment the number of squares being flashed each turn. Again, the comments in the code point out what changes need to be made.

At the beginning of the userSucceeds loop, create an array of length sequenceLength, and fill this array with random numbers (values from rand.nextInt(numberOfSquares)). Pseudocode:

Create an int array of length sequenceLength.
For i from 0 to sequenceLength - 1:
Set index i of the array to rand.nextInt(numberOfSquares).

For example, if numberOfSquares is 5 and sequenceLength is 7, then this array should consist of 7 random values between 0 and 4, something like:

index 0 1 2 3 4 5 6
value 1 4 4 0 2 1 4

In the userSucceeds loop, you should have a loop to flash sequenceLength squares. Your array with random numbers tells you which square to flash at each point in the sequence. Pseudocode:

For i from 0 to sequenceLength - 1:
Determine which square should be flashed at this point in the sequence.
Flash that square.

For example, for the above array, first square 0 is flashed, then square 4, square 4 again, square 0, square 2, square 1, and finally square 4. Note that is it possible that some square is never flashed (such as square 3 in the example), it is possible that some square is flashed multiple times (such as square 4 in the example).

In the userSucceeds loop, you should have a loop that gets the sequence of squares that the user clicks (the values from getSquareClicked). You should store this sequence in an array of length sequenceLength. Pseudocode:

Create an int array of length sequenceLength.
For i from 0 to sequenceLength - 1:
Determine which square was clicked by the user.
Set index i of the array to that square.

For example, if numberOfSquares is 5 and sequenceLength is 7, then this array should consist of 7 values between -1 and 4, where -1 indicates a click that was outside all of the squares. This should be something like:

index 0 1 2 3 4 5 6
value 1 4 -1 0 2 1 4

In the userSucceeds loop, use a loop to check that the user's sequence of clicks matches the correct sequence of clicks. If any click is incorrect, then userSucceeds should be set to false. Pseudocode:

For i from 0 to sequenceLength - 1:
If the user's click at this point in the sequence is not correct:
Set userSucceeds to false

For example, the above two arrays do not match because there are different values at index 2.

At the end of the userSucceeds loop, increment sequenceLength.

Academic Honesty!
It is not our intention to break the school's academic policy. Projects posted are only used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly fill out the form. Please provide a valid email address and we'll get back to you in less than 24 hours. We will be sending an invoice through PayPal upon confirmation. We are a non profit organization however we need an amount to keep this organization running, and to be able to complete our research and development.