Create a program from scratch a one-dimensional game

Assignment Help JAVA Programming
Reference no: EM131711651

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

Reference no: EM131711651

Questions Cloud

Record year-end adjusting entry for the depletion expense : Prepare the December 31, 2017, entries to record both the ore deposit depletion and the mining machinery depreciation
Discuss about the undercover operations : Propose one (1) overall strategy in which you combine problem-oriented policing and investigation to tackle the identified issues.
How cole hardware used technology to manage inventory : Explain how Cole Hardware used technology to manage inventory and identify at least two (2) benefits of the use of this technology by the company
Lineups and other means of pretrial identification : Discuss two (2) situations in which you feel that Miranda warnings are not required as it relates to arrest, custody, and interrogation.
Create a program from scratch a one-dimensional game : 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.
Critical paper using the library as well as the internet : critical paper using the library as well as the internet as resources. The paper should discuss a work of art chosen from the course textbook
Define significant difference and an important difference : Write sentences that distinguish between a statistically significant difference and an important difference.
How group norms exert influence on an individuals behavior : Discuss ways employees can avoid the pitfalls of negative peer pressure and how employers can use mentors to foster positive peer pressure .
Journalize april transaction using periodic inventory system : Tennis Shop showed Cash $2,500; Inventory $1,700; and Common Stock $4,200. Journalize the April transactions using a periodic inventory system

Reviews

Write a Review

JAVA Programming Questions & Answers

  Java program for real estate agent

Write down java program for real estate agent. Program must perform the following tasks: ask users for average house price for the each of past 5 years for single family residence of 1500 square feet.

  Calculate the optimal position of the large pipeline

Your task is to calculate the optimal position (on the y-axis) of the large pipeline, so that the total length of all smaller pipelines is shortest

  Detailed explanations for polymorphism and inheritance

Prepare a 6-8-page document that provides detailed explanations for polymorphism, inheritance, and encapsulation

  Create a method that would search through an arraylist

What is the best way of the following ways (For each, Iterator, While with index) that you would use to create a method that would search through an ArrayList of files for a certain file and then delete it, and why?

  Write a friend function that can examine the elements

Write a friend function that can examine the elements directly, or pop the stacks and save the popped elements so that the stacks can be restored.

  Calculates the cutoff frequency in a simple rc circuit

Write a simple Java application which calculates the cutoff frequency in a simple RC circuit.

  Design and implement the class day

Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for Sunday

  How to write this method

How to write this method - Positive numbers rotate to the left, and negative numbers rotate to the right - the following client code would behave

  Write function to calculate gross wages for hourly employee

In a separate JS file, write a function to calculate gross wages for an hourly employee. The function should receive two parameters: the pay rate (dollars per hour, a floating point number) and the hours worked.

  Part - a1nbsp to compile and run a java source file

part - a1.nbsp to compile and run a java source file hello.java that contains a main method which of the following are

  What is an illustration of a hypothesis test please explain

what is an example of a hypothesis test? please describe the context of the problemissue that you encountered what the

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd