Processing代写 | COMP 1010 Assignment 5

这个作业是用Processing完成网格游戏、涂色等相关的小练习
COMP 1010
COURSE TITLE: Introductory Computer Science 1
Assignment 5

It is recommended that you view the video in the Assignment 5 folder on UM Learn to see how
the game should work.
Each question in this assignment builds on the previous question. Make sure each question is
fully working before moving on to the next.
Question 1: Preparing the Grid & Colouring the Cells [3 marks]
Download the A5Q1template.pde file in the Assignment 5 folder on 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 game board, 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.
The draw() function 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 a void drawCells(int[], int[], int, int) function that accepts two int arrays
(column numbers first, row numbers second), the number of entries in the arrays, and a colour
(an int).
• Draw a cell (a square) on the canvas for each entry in the arrays (use the column array
to get the X coordinate and the row array to the get the Y coordinate)
• Fill each square with the value of the colour
parameter.
• Hint: the colour is given as a hexadecimal value,
which is what you get when you use the “Color
Selector” tool in Processing.
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.

Question 2: Generate a Random Ship [7 marks]
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 int
array variables to store the row and column numbers of each cell 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.
For example, suppose your code randomly chose a ship
length of 4, a horizontal placement, row 3, and column 2 (as
shown at right). Then your global int array that stores the
columns of the ship cells should contain 2, 3, 4, 5, and
the global int array that stores the rows should contain 3,
3, 3, 3, meaning that the ship consists of cells: (row 3,
column 2), (row 3, column 3), (row 3, column 4), and (row 3,
column 5).
Test your randomShip() function by using your drawCells() function from question 1 to
draw the ship. An example of the output of question 2 is shown above.
Question 3: Making Guesses [6 marks]
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.
Create two 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 first
convert the mouse position in pixels into the row and column on the grid. Then, use two short
helper functions:
ASSIGNMENT 5
DEPARTMENT AND COURSE NUMBER: COMP 1010
COURSE TITLE: Introductory Computer Science 1
TERM: Summer 2020

• 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.
Question 4: Labels [2 marks]
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 1-5 seconds (you choose the length of
time), to give the user time to read it. Use a counter to
accomplish this. Do NOT change the frame rate.
Question 5: Sink the Ship [3 marks]
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.
[Programming Standards are worth 5 marks]
ASSIGNMENT 5
DEPARTMENT AND COURSE NUMBER: COMP 1010
COURSE TITLE: Introductory Computer Science 1
TERM: Summer 2020

Part 2: Written Questions
Save your answers to these questions as a PDF document named
LastnameFirstnameA5.pdf and submit this file in the Assignment 5 submission folder on UM
Learn.
Question 6: Computer Science Specializations [1 mark]
The Department of Computer Science at the University of Manitoba offers seven areas of
specialization, which are groups of courses in a particular area of Computer Science.
Name four of the areas of specialization that are currently offered.
Question 7: Undergraduate CS Student Groups [1 mark]
The University of Manitoba Computer Science website lists three Undergraduate Computer
Science Student Groups. What are the names of these three groups?