Project 3.4: Light Puzzle
Create a program that change color on a click in a tricky manner
REQUIREMENTS
This program is a puzzle game where the user tries to make all of the boxes the same color - but each time they click, it changes both the current box and all adjacent boxes colors.
Specifications
The window must be sized exactly 1400 x 200
Your program must start with 7 boxes that are sized 200 x 200
Each box is one of three colors tied to a code. For instance, RED --> 0, PURPLE --> 1, and BLUE --> 2.
Each time the user clicks on a box, that box and all adjacent boxes shift one step
Your final color should "wrap" back to the first color
Your program must not crash if you click on an edge block
The program identifies that all boxes are the same color and prints a win message to the user.
Since arrays can be challenging, additional support is provided below - use the outline!
PART ONE - DISPLAY
At the top...
Declare an integer called boxSize.
Declare an array of integers called boxes
This will store a number: 0, 1, or 2. Each number represents a color.
Write a setup method...
Set the size of your window
Set boxSize equal to 200.
Initialize the array of boxes to the appropriate number of boxes
Loop through the boxes and give each box a random value between 0 and 2.
Hint: You'll need to use casting! Make sure you are getting three possible values and not just 2.
Write a draw method...
Loop through the boxes...
If the box at the current position is 0, set fill equal to a color of your choice
If the box at the current position is 1, set fill equal to a second color of your choice
If the box at the current position is 2, set fill equal to a third color of your choice
Draw a rectangle to represent the current box.
Hint: This should use the loop variable, but NOT boxes. Remember the array isn't being used for position in this program, just colors.
Test:
Your program displays a line of boxes in three different colors.
PART TWO - CLICK
Write a method called nextColor(int index)
Increase the value of boxes[index] by one.
Make it cycle back if it goes past the highest code:
If boxes[index] equals 3 --> set boxes[index] to 0.
Write a mousePressed method...
Create a variable called index and assign it a value based on the mouse's x position
Hint: The index is equal to mouseX divided by boxSize
Call the nextColor method with index as a parameter.
Test:
Clicking on a cell should change a single element
PART THREE - NEIGHBORS
Modify your mousePressed method...
In addition to changing the current index, call nextColor on index - 1 and index + 1
Be careful! At first this will make your program crash if you click on the first or last element
You'll get an ArrayIndexOutOfBounds exception
To fix this....
Only call nextColor on index - 1 if index is greater than 0
Only call nextColor on index + 1 if index is less than boxes.length - 1
Test:
Clicking on a cell should change up to three boxes at once. Test the edges + make it all one color.
PART FOUR - WIN
Write a method called boolean hasWon() that checks if all boxes are the same color
This method should loop through all of the boxes
If two boxes are different values, return false.
Once the loop is complete, you can return true. It would only reach this point if all boxes matched.
Write code to display a win message if the user has won the game.
Display this in large text over the center of the screen
EXAMPLE: RUNNING PROGRAM
Click here to download a running version of the program.
Starting State
Solved Puzzle