Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this programing assignment. Instructions are in the given link

ID: 3535285 • Letter: I

Question

I need help with this programing assignment. Instructions are in the given link

http://cs.utsa.edu/~cs1063/projects/Spring2013/Project3/project3.html



SAMPLE CODE GIVEN.

===================================


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

package simongame;


import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.ArrayList;

import java.util.Random;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JOptionPane;



public class Simon extends JFrame {


static int Pick; //The color the player picked

public static void main(String[] args) {

JFrame frame = new JFrame("Simon");

ArrayList<Integer> Computer = new ArrayList<Integer>();

ArrayList<Integer> Player = new ArrayList<Integer>();

int CompPick; //The color the computer picks

Random gen = new Random();

// Set up the Jframe

frame.setSize(206, 232);

frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setResizable(false);

frame.setLayout(null);


// Set up the four colored buttons

final JButton BlueButton = new JButton();

BlueButton.setBounds(0, 0, 100, 100);

BlueButton.setBackground(Color.blue);


JButton RedButton = new JButton();

RedButton.setBounds(100, 0, 100, 100);

RedButton.setBackground(Color.red);


final JButton YellowButton = new JButton();

YellowButton.setBounds(100, 100, 100, 100);

YellowButton.setBackground(Color.yellow);


final JButton GreenButton = new JButton();

GreenButton.setBounds(0, 100, 100, 100);

GreenButton.setBackground(Color.green);


// Add the buttons and make the frame visable

frame.add(BlueButton);

frame.add(RedButton);

frame.add(YellowButton);

frame.add(GreenButton);

frame.setVisible(true);



do {



CompPick = gen.nextInt(4); //Pick a random number(color)


Computer.add(CompPick); //Add the color to the computer's arraylist


// Search through the arraylist and find what colors were pushed

for (int i = 0; i < Computer.size(); i++) {

if (Computer.get(i) == 0) { //Blue Button

//Make the blue button look like it was clicked

BlueButton.doClick(1000);


//Pause the execution for one second (if the arraylist has more

//than one item it needs to pause in between each color lighting up)

try {

Thread.currentThread().sleep(1000);

}

catch (InterruptedException e) {

e.printStackTrace();

}

} else if (Computer.get(i) == 1) { //Red Button

RedButton.doClick(1000);


try {

Thread.currentThread().sleep(1000);

}

catch (InterruptedException e) {

e.printStackTrace();

}

} else if (Computer.get(i) == 2) { //Yellow Button

YellowButton.doClick(1000);


try {

Thread.currentThread().sleep(1000);

}

catch (InterruptedException e) {

e.printStackTrace();

}

} else if (Computer.get(i) == 3) { //Green Button

GreenButton.doClick(1000);

try {

Thread.currentThread().sleep(1000);

}

catch (InterruptedException e) {

e.printStackTrace();

}

}

}


// Add an action listener to each button. If the button is pushed,

// set Pick to the number that corresponds to the color of the button

BlueButton.addActionListener(new ActionListener() {


public void actionPerformed(ActionEvent e)

{

Pick = 0;

}

});

RedButton.addActionListener(new ActionListener() {


public void actionPerformed(ActionEvent e)

{

Pick = 1;

}

});

YellowButton.addActionListener(new ActionListener() {


public void actionPerformed(ActionEvent e)

{

Pick = 2;

}

});

GreenButton.addActionListener(new ActionListener() {


public void actionPerformed(ActionEvent e)

{

Pick = 3;

}

});



Player.add(Pick); //Add the button that was pushed to the player's arraylist

// Search through the two arraylists to make sure the player pushed the right button

for (int j = 0; j < Computer.size(); j++) {

if (Computer.get(j) != Player.get(j)) {

JOptionPane.showMessageDialog(null, "You Lose");

}

}


} while (Pick == CompPick);




}

}