The goal of this project is to implement a program that allows the user to play
ID: 3534593 • Letter: T
Question
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.
Lab 9 will focus on allowing a variable number of squared in the window. Project 3 will focus on allowing the sequence length to change.
The initial version of Simon.java implements the following pseudocode:
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 of Lab 9 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:
The final result of your Lab 9 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.
import java.awt.*;
import java.util.*;
/*
* This program implements a version of the Simon memory game.
* In this version, the game has one row of colored squares.
* The game starts when the program flashes a random sequence of
* 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 square longer.
* The game ends when the user clicks the squares in the wrong
* order.
*/
public class Simon {
public static void main(String[] args) {
/* Lab 9 Need To Do
* Ask the user for the number of squares to display.
* The user should be prompted until an integer
* from 2 to 6 is entered. The comments below call
* this value numberOfSquares.
*
* This should not crash if the user does not enter an integer.
*/
/* Lab 9 Need To Do
* The width of the panel should depend on numberOfSquares.
*/
DrawingPanel panel = new DrawingPanel(450, 300);
Graphics g = panel.getGraphics();
g.setFont(new Font("Serif", Font.BOLD, 24));
/* Lab 9 Need To Do
* Instead of drawing 2 squares, there should be a loop
* to draw from 2 to 6 squares as specified by the user.
*
* There should be an array that contains the values
* of the upper left corner x-coordinates (100 and 250
* in this sample code). The length of this array should
* be numberOfSquares.
*/
// Draw outline of square 0
g.drawRect(100, 100, 100, 100);
// Draw outline of square 1
g.drawRect(250, 100, 100, 100);
/* Lab 9 Need To Do
* Instead of filling 2 squares with colors, write a loop
* to fill from 2 to 6 squares with colors.
* You should have an array of 6 colors (this sample code
* contains an array of 2 colors).
* You need to use the x-coordinate array described above
* for g.fillRect.
*/
Color[] colors = {Color.BLUE, Color.RED};
// Fill square 0 with its color
g.setColor(colors[0]);
g.fillRect(101, 101, 99, 99);
// Fill square 1 with its color
g.setColor(colors[1]);
g.fillRect(251, 101, 99, 99);
int sequenceLength = 2;
Random rand = new Random();
// prime the loop
boolean userSucceeds = true;
while (userSucceeds) {
// decide on a random sequence of squares
/* Project 3 Need To Do
* Create an array of length sequenceLength, and
* write a method that fills this array with
* values from rand.nextInt(numberOfSquares).
*/
/* Lab 9 Need To Do
* rand.nextInt(2) should be replaced with
* rand.nextInt(numberOfSquares)
*/
// Which square should be flashed first?
int square0 = rand.nextInt(2);
// Which square should be flashed second?
int square1 = rand.nextInt(2);
// flash the squares
/* Project 3 Need To Do
* Instead of two pieces of code to flash two squares,
* 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.
*/
int upperLeftX;
Color squareColor;
// Flash the square to be flashed first.
/* Lab 9 Need To Do
* This if statement can be replaced with a single assignment
* using the x-coordinate array with square0 as the index.
*/
if (square0 == 0) {
upperLeftX = 100;
} else {
upperLeftX = 250;
}
/* Lab 9 Need To Do
* This if statement can be replaced with a single statement
* using the colors array with square0 as the index.
*/
if (square0 == 0) {
squareColor = Color.BLUE;
} else {
squareColor = Color.RED;
}
flashSquare(panel, g, 1, upperLeftX, squareColor);
panel.sleep(1000);
// Flash the square that should be flashed second.
/* Lab 9 Need To Do
* This if statement can be replaced with a single assignment
* using the x-coordinate array with square1 as the index.
*/
if (square1 == 0) {
upperLeftX = 100;
} else {
upperLeftX = 250;
}
/* Lab 9 Need To Do
* This if statement can be replaced with a single statement
* using the colors array with square1 as the index.
*/
if (square1 == 0) {
squareColor = Color.BLUE;
} else {
squareColor = Color.RED;
}
flashSquare(panel, g, 2, upperLeftX, squareColor);
// get the sequence of user clicks
/* Project 3 Need To Do
* Instead of separate pieces of code for each click,
* you should have a loop that gets the sequence of
* squares that the user clicks.
* You should store this sequence in an array of
* length sequenceLength.
*/
// Get the first click.
int[] click = busyWait(panel);
// What square did the user click?
int user0 = getSquareClicked(click[0], click[1]);
// Get the second click.
click = busyWait(panel);
// What square did the user click?
int user1 = getSquareClicked(click[0], click[1]);
// Check if the user's clicks are correct.
/* Project 3 Need To Do
* 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.
*/
// Was the first click wrong?
if (square0 != user0) {
userSucceeds = false;
}
// Was the second click wrong?
if (square1 != user1) {
userSucceeds = false;
}
// message about success or failure
if (userSucceeds) {
g.setColor(Color.BLACK);
g.drawString("You won! Try Again!", 10, 30);
} else {
// erase old message
g.setColor(Color.WHITE);
g.drawString("You won! Try Again!", 10, 30);
g.setColor(Color.RED);
g.drawString("You lost!", 10, 30);
}
panel.sleep(1000);
/* Project 3 Need To Do
* Increment sequenceLength.
* Do this when you have finished all the other
* Project 3 Need To Do's
*/
}
}
// Flash the square as indicated by the parameters.
public static void flashSquare(DrawingPanel panel, Graphics g, int sequenceNumber,
int upperLeftX, Color squareColor) {
g.setColor(Color.WHITE);
g.fillRect(upperLeftX + 1, 101, 99, 99);
g.setColor(Color.BLACK);
g.drawString("" + sequenceNumber, upperLeftX + 50, 150);
panel.sleep(500);
g.setColor(squareColor);
g.fillRect(upperLeftX + 1, 101, 99, 99);
}
// Return 0 if the user clicked on square 0.
// Return 1 if the user clicked on square 1.
// Otherwise, return -1.
/* Lab 9 Need To Do
* This must be generalized for any numberOfSquares.
* You should modify the method so that it has two
* additional parameters, the number of squares and
* the x-coordinate array.
*/
public static int getSquareClicked(int x, int y) {
if (x > 100 && x < 200 && y > 100 && y < 200) {
return 0;
} else if (x > 250 && x < 350 && y > 100 && y < 200) {
return 1;
} else {
return -1;
}
}
// Loop until the user has pressed and released the mouse.
// Return the coordinates of the click as an int array
// of length 2.
// Neither Lab 9 nor Project 3 need to change this code.
// Actually, this is badly-designed code. We should be
// using event-driven programming for this (Chapter 14).
public static int[] busyWait(DrawingPanel panel) {
// loop until mouse is pressed
boolean pressed = panel.mousePressed();
while (pressed == false) {
pressed = panel.mousePressed();
}
// loop until mouse is released
while (pressed == true) {
pressed = panel.mousePressed();
}
int[] result = new int[2];
result[0] = panel.getClickX();
result[1] = panel.getClickY();
while (result[0] == -1 || result[1] == -1) {
result[0] = panel.getClickX();
result[1] = panel.getClickY();
}
return result;
}
}
Explanation / Answer
Please rate with 5 stars :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.