Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

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

Java, Programming

  • Category:- Java
  • Reference No.:- M92520343
  • Price:- $120

Priced at Now at $120, Verified Solution

Have any Question?


Related Questions in Java

In ruby the hash class inherits from enumerable suggesting

In Ruby, the Hash class inherits from Enumerable, suggesting to a programmer that Hashes are collections. In Java, however, the Map classes are not part of the JCF (Java Collections Framework). For each language, provide ...

Assessment socket programmingtaskwrite a java gui program

Assessment: Socket Programming Task Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If yo ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Assessment instructionsin this assessment you will complete

Assessment Instructions In this assessment, you will complete the programming of two Java class methods in a console application that registers students for courses in a term of study. The application is written using th ...

Question slideshows or carousels are very popular in

Question : Slideshows (or carousels) are very popular in websites. They allow web developers to display news or images on the website in limited space. In this code challenge, you are required to complete the JavaScript ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Can someone kindly help me to consider whether java

Can someone kindly help me to consider whether Java provides the facility of operator overloading? If it does, may you kindly describe how overloading operators can be accomplished? If not, may you kindly describe why yo ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As