Get Instant Help From 5000+ Experts For
question

Writing: Get your essay and assignment written from scratch by PhD expert

Rewriting: Paraphrase or rewrite your friend's essay with similar meaning at reduced cost

Editing:Proofread your work by experts and improve grade at Lowest cost

And Improve Your Grades
myassignmenthelp.com
loader
Phone no. Missing!

Enter phone no. to receive critical updates and urgent messages !

Attach file

Error goes here

Files Missing!

Please upload all relevant files for quick & complete assistance.

Guaranteed Higher Grade!
Free Quote
wave
Creating a Battleship Game in Processing

Q1: Preparing the Grid & Colouring Cells

Download the A5Q1template.pde file in the Assignment 5 folder in UM Learn.The grid parameters have been set for you. A global constant CELLSIZE stores the width and height of each square cell on the gameboard, in pixels. The global constants NUM_ROWS and NUM_COLS store the number of rows and number of columns on the game board. The size of the canvas is set in setup(), so that it is the correct size for the parameters above.In this assignment, you will store the row and column numbers for cells in parallel arrays (one array to store the row numbers, and one array to store the column numbers, where the row and column for a cell are stored at the same position in both arrays). Valid column numbers are 0(zero) to NUM_COLS-1. Valid row numbers are 0 to NUM_ROWS-1. draw() contains code that will fill arrays with row and column numbers, and it calls a drawCells() function to colour cells on the game board. Do not change any of the provided code while working on question 1.

Write the drawCells() function such that it accepts two int arrays (column numbers first, row numbers second), the number of entries in the arrays, and a colour (an int). This function should fill each cell listed in the arrays with the specified colour.The conversion from column number and row number to location on the canvas MUST make use of the global constants. Do NOT use any magic numbers.Test your drawCells() function, and make sure that your program output appears as in the image at right.

Q2: Generate a Random Ship

Remove the contents of draw() that were given in the question 1 template, and remove the noLoop() line from setup.

When the game begins, it should first choose a random position for a ship. Set up global intarray variables to store the x and y coordinates (cell numbers) of the ship. Write a randomShip() function that will accept a length for the ship, and fill the global ship arrays. When the game begins, choose a random ship length between 3 and 8, and then choose a position for the ship.

To choose a position for the ship:

• Randomly decide whether to use a horizontal or vertical placement.
• Randomly choose a row or column (depending on whether the ship is horizontal or vertical).
• Randomly choose a location within that row or column. Make sure that the entire ship is within the visible canvas.

Q2: Generate a Random Ship

Q3: Making Guesses

In this question, you will add the ability of the user to make guesses, and identify those guesses as either hit or miss.

For testing purposes, leave the ship visible on the canvas. Add global arrays to keep track of the hits and misses. (What is the maximum number of  guesses? Set the array size accordingly.)

Guesses will be made by clicking the mouse. Write a mouseClicked() function that will:

• Convert the mouse position in pixels into the row and column on the grid. (Use two short helper functions.)

• Test if that guessed cell is found in the ship arrays (write a search function – you will use it again below).

• If the guess is found in the ship arrays, add the guess to the hit arrays. If the guess is not found, add it to the miss arrays. (Use a function to add a guess to a pair of arrays, and make sure that that function will not add duplicate entries in the event that the user repeats a guess.)Use drawCells() to draw the hits in one colour and the misses in another colour. An example of the output of question 3 after 5 guesses is shown at right.

Q4: Labels

When a guess is made, in addition to colouring the cell, print either “Hit!” or “Miss!” on the cell. The message should persist for at least one second, to give the user time to read it. Use a counter to accomplish this. Do NOT change the frame rate.

Q5: Sink the Ship

To determine when the game is over, we need to know when all of the ship cells have been hit. Each time the ship is hit, delete that cell from the ship arrays. When the ship arrays are empty, the ship is sunk.Write a delete() function that will accept two int arrays (x and y), the number of items in those arrays, and the cell that should be deleted. Make sure that your delete function properly updates the x and y arrays, and the number of items.

Be careful! If you delete from the ship arrays and the user guesses a hit cell again later, your code will think that is a miss. Fix this error so that once a cell is hit, it remains hit even if the user clicks on it again.Delete the code that displays the ship. It’s not much of a game if you know where to aim Add a game over test, and when the game ends, print a “Game Over!” message on the canvas. The game should freeze and there should be no response if the user clicks on the canvas.

support
close