12.07.2015 Views

Homework (.pdf) - thecubscientist.com

Homework (.pdf) - thecubscientist.com

Homework (.pdf) - thecubscientist.com

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

P5G. 2D Guessing Game (optional)Write a simple 2D Guessing Game program that will allow the user to guess the rowand column that the number “0” is in. If the row and column guess is correct, the<strong>com</strong>puter will display a “You Won!” message. If the row and column guess isincorrect, the <strong>com</strong>puter will tell the user how many rows and columns the guess is offby. For example if the number to be guessed is in (0,0) and the user guesses (3, 4), the<strong>com</strong>puter will tell the user he/she is off by 3 rows and 4 columns. If you wish, youmay limit the user to a certain number of guesses to increase the difficulty level.Complete the following tasks for your guessing game (feel free to add your ownfeatures that maintain the spirit of the game)a. Create a 2D array called gameBoard of type int. Use dialog boxes to askthe user how many rows and columns he/she wants for the game.b. Use a for loop to fill the 2D array with random numbers between 1 and100.c. Use Math.random() to pick a random row and random column to placethe number “0” in (this will be the number that the user must attempt tofind). Hint: call Math.random() twice to generate two randomnumbers…one for the row and one for the column. Be sure not to pick rowsand/or columns that are out of bounds!d. Print the 2D array in rectangular format using an “X” to represent eachnumber in the 2D array, effectively “hiding” the numbers in the 2D array.e. Use a dialog box to ask the user for a row and column to use as their guessto find the number “0”.f. If the guess is correct, reply with a message confirming the win! (e.g.“BAM! You found the number 0!)g. If the guess is incorrect, replay with a message telling the user how manyrows and how many columns he/she is off by (see the example above).h. Reprint the array, placing a “N” for “No Bam!” in place of the “X” to showthat this location has already been picked.i. Repeat steps (e) through (h) until the number “0” is found or until alllocations have been checked (unless you choose the limit the number ofguesses).Below are some features that might be useful for this programpublic static void main( String[] args ) throws InterruptedException{Thread.sleep( 1000 );System.out.println( “\f” );} // end method main// delays program// need “throws InterruptedException// after public static void main// clears the screenPage 6AP Computer Science – HW Set 5 Programshttp://<strong>thecubscientist</strong>.<strong>com</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!