Create a program from scratch a one-dimensional game of


Assignment: Introduction to Java Programming Using arrays

In this assignment you will create a program from scratch; a one-dimensional game of Lights Out. Briefly, the game consists of a row of lights that can be either on or off. By choosing a particular light, you switch its state and the state of its neighbors. That is, you turn the chosen light from off to on or from on to off, and you turn each neighbor from off to on or from on to off, depending on their initial states. The game is won when all of the lights are turned off.

Procedure

Step 1: Open BlueJ and create a new project called Assignment07-userName, where username is your USC username You will have one file called LightsOut that contains all the logic for the game.

Game Overview: Your program should first prompt the user for a number of lights. This must be between 3 and 15 (inclusive). If it is not in that range, your program should enter a small loop in which it continues to prompt the user for an integer in the valid range until one is typed. You may assume the user will always input an integer.

Your program should create an array of the specified length and go through each value in the array and randomly set it to "on" or "off". This randomization is for each value separately -- we want a random pattern of lights to begin with.

You should then print out the pattern of lights so that each "on" light is a 4x4 block of *'s and each "off" light is a 4x4 block of spaces. Each light should be separated by vertical bars (|). After printing the lights, you should include numbers beneath each light, starting from 0.

Then, you should ask the user what light they would like to select (it must be an integer). You should check to be sure that the light selected is actually valid for the current game -- if not, you should enter a small loop and continue prompting the user until a correct input is typed in. See the run below as an example. (Also note: For some numbers of lights and some initial states, the Lights Out puzzle isn't even solvable. So there needs to be an option to let the user quit if they can't get to a solution!)

Once a valid light number is input, your program should make the appropriate changes to the light array, and then continue by printing out the new array and prompting the user again. When the user wins the game by turning off all of the lights, the program should stop, print a congratulatory message, and quit. There is no need to prompt the user to play again; in this case, we'll let the user rerun the program if they would like to.

LightsOut Specifics

• Variables:

o The boolean array of lights
o A boolean that represents if the game is won or not
o Optional:

- 2 constants (boolean ON = true and boolean OFF = false). I find it helpful to think of ON and OFF instead of true/false, but it is up to you.
- Scanner for user input (optional, can be a local variable or instance variable)

• Methods: each individual task of the game belongs in a method. You must have at least the following methods in your program (in no particular order):

o public void start() - this method controls the whole flow of the game. It is the only public method.

o private int getNumberOfLights() - this method is responsible for prompting the user for the number of lights he/she wants in the games. It will only return a valid number, [3-15]

o private void initializeLights() - this method will initialize each spot in the array to true or false. Note that the Random class has a nextBoolean() method that can be used.

o private void checkWin() - this method updates the boolean instance variable if needed and displays a message to the user

o private boolean isValid(int light) - this method returns true if a given light is valid for the game (that is, it's between valid array indices: 0 to number of lights)

o private int getLightNum() - this method handles the user input for which light they want to choose. It only returns a valid light number or -1.

o private void changeLights(int lightNum) - this method controls the flow of changing lights, based on the lightNum. It switches the light at spot lightNum, and then makes sure neighboring lights are valid before switching their colors.

- Note: At the end of this method or in the start method after the call to changeLights, you should check to see if the game is won after these lights are changed.

o private void printLights() - this method "pretty" prints the lights according to the printing specifications above

• Make sure to comment your code and use good style like proper indentation.

Attachment:- Array-Game.pdf

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Create a program from scratch a one-dimensional game of
Reference No:- TGS02507172

Now Priced at $120 (50% Discount)

Recommended (90%)

Rated (4.3/5)