Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with Q4 and 5. Attached below is the question. Thanks My CODE: //Q1: Set up a grid on the canvas. Draw a

I need help with Q4 and 5. Attached below is the question. Thanks

image text in transcribedimage text in transcribedimage text in transcribed

My CODE:

//Q1: Set up a grid on the canvas. Draw a "mine" cell // and a "number of mines nearby" cell. //Q2: Add functions to draw the grid, a list of mines and // a list of number cells.

final int CELLSIZE = 50; final int NUM_ROWS = 10; final int NUM_COLS = 12; final int MIN_MINES=10; final int MAX_MINES=15;

//global arrays to hold mine column and row nos int[] mineX= new int[MAX_MINES]; //column no of mine int[] mineY= new int[MAX_MINES]; //row no of mine

void setup() { size(600, 500); //size MUST be (NUM_COLS*CELLSIZE) by (NUM_ROWS*CELLSIZE); int mineCount= (int) random (MIN_MINES, MAX_MINES+1); generateMines(mineCount); }

void draw() { drawGrid(); drawMines(mineX, mineY, 6); //fill the cells with mines }

//to search the first n positions in the arrays named x and y boolean search(int[]x, int[]y, int n, int col, int row) { for (int i=0; i

//to fill arrays with randomly generated row and column numbers, making sure that each position is unique void generateMines(int noOfMines) {

int randRow, randCol;

for (int i=0; i

insert(mineX, mineY, i, randCol, randRow); } }

//to insert postion(col,row) into given arrays as long as ther is room int insert(int[]x, int[]y, int n, int col, int row) { int minLen= Math.min(x.length, y.length);

if (n==minLen) { return n; }

x[n]= col; y[n]= row;

return n+1; }

//**add your drawGrid function here**

void drawGrid() { final int HEIGHT=700;

//draw the grid for (int i=0; i

//**add your drawMines function here**

void drawMines(int[] cols, int[] rows, int entries) { for (int i=0; i

//**add your drawNums function here** void drawNums(int[] cols, int[] rows, int []vals, int entries) { for (int i=0; i

//**paste your drawMine and drawNum functions from Q1 here** //**add your drawMine function here** void drawMine(int row, int col) { int stroke=3;

//draws mine in specified cell //draw mine design

fill(255); stroke(0); strokeWeight(1); rect(CELLSIZE*row, CELLSIZE*col, CELLSIZE, CELLSIZE); fill(0); ellipse(CELLSIZE*row + (CELLSIZE/2), CELLSIZE*col + (CELLSIZE/2), 3*NUM_ROWS, 3*NUM_ROWS);

stroke(255, 0, 255); strokeWeight(stroke); line(CELLSIZE*row + stroke, CELLSIZE * col + (CELLSIZE/2), CELLSIZE * row + CELLSIZE- stroke, CELLSIZE * col + (CELLSIZE/2)); line(CELLSIZE*row + (CELLSIZE/2), CELLSIZE * col + stroke, CELLSIZE*row + (CELLSIZE/2), CELLSIZE*col + CELLSIZE- stroke);

stroke(255); strokeWeight(0); fill(128, 128, 128); ellipse(CELLSIZE*row + (CELLSIZE/2), CELLSIZE * col + (CELLSIZE/2), 2*NUM_ROWS, 2*NUM_ROWS);

fill(0, 255, 0); ellipse(CELLSIZE*row + (CELLSIZE/2), CELLSIZE*col + (CELLSIZE/2), NUM_ROWS, NUM_ROWS); }

//**add your drawNum function here** void drawNum(int row, int col, int val) { final int OFFSETX= 15; final int OFFSETY= 39;

//draw and center text and place in box fill(255); stroke(0); strokeWeight(1); rect(row*CELLSIZE, col*CELLSIZE, CELLSIZE, CELLSIZE); fill(0, 0, 255); textSize(35); text(val, row * CELLSIZE+OFFSETX, col * CELLSIZE + OFFSETY); }

A function boolean search(int[] x, int[] y, int n, int col, int row) should search the first n positions in the arrays named x and y. if there is a position where col matches the entry in x and row matches the entry in y, the function should return true. Otherwise, return false. A function int insert(int[] x, int[] y, int n, int col, int row) should insert the position (col, row) into the given arrays, as long as there is room in the arrays. insert() should return the number of items in the arrays after the function runs (i.e., return n if nothing was inserted, and n+1 if the insertion was successful). In draw(), use the drawMines () function from Q2 to display the randomly generated mines on the game board. Question 4: Making Guesses (8 marks) In this question, you will allow the user to make guesses. For testing purposes, leave the mines visible on the canvas. Add global arrays to keep track of the guesses. You will need the column numbers, the row numbers, and the values to be printed. (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 mine arrays (use the search function you wrote in Q3.) If the guess is found in the mine arrays, the game should end (the game has been lost). Everything should freeze, all of the mines should be displayed, a game over message should be printed, and the game should not respond to mouse clicks. If the guess is not found in the mine arrays, then it should be added to the guess arrays. Count the number of neighbouring cells that contain mines, and add this to the value array. Write a second insert function to add a guess to the set of three arrays, and make sure that the function will not add duplicate entries in the event that the user repeats a guess. "Neighbouring cells" refers to all cells that are adjacent to the current cell vertically, horizontally, or diagonally. See the images below for examples. One cell can have up to eight neighbouring cells. If the user has guessed every cell that does not contain a mine, the game should end (the game has been won). The game over message should reflect a win. 4 A504 A504 X 21 20 2 1 1 2 GAME OVER! YOU LOST! 9 1 0 0 20 21 0 1 2 0 0 1 0 0 1 2 0 2 1 0 0 2 1 0 0 1 0 02 3 1 1 0 0 1 2 1 02 O 3 0 1 0 0 1 0 2 1 0 1 1 2 1 1 0 0 1 1 1 0 2 2 1 0 0 0 0 0 0 0 0 0 AME OVER!YOU WON 3 0 3 0 1 0 1 1 1 2 0 2 3 4 4 3 1 1 0 1 2 0 3 O2000 1 1 1 1 2 2 2 2 3 2 3 2 1 0 1 1 2 0 1 1 0 0 0 0 0 1 2 1 1 In draw(), use your drawNums () function from Q2 to display the user's guesses. Leaving the mines visible will allow you to test that your counts are correct when you click on a cell without a mine, and that the game ends when you click on a cell with a mine. Question 5: Make it fun! (3 marks) A5Q5 x Add some more features to the game: Keep score. Keep track of the number of guesses the user makes. Count every click for this, including duplicates. If the user wins, display the score as part of the game over message. 3 2 2 0 2 1 0 1 1 1 1 0 0 0 GAME OVER YOU LOSTI 1 0 0 To play again pedes Add the ability to play again. With each new game, increase the number of mines, up to some maximum value. If the game is over and the user presses Non the keyboard, a new game should be generated. Add instructions to your game over message to tell the player how to start a new game. If the user presses N, clear the game board, generate a new set of mines, and let the player play again. Do not display the mines while playing. Only display the mines when the game is over. A5Q5 0 01 02 0 2 0 1 0 0 0 0 0 1 1 3 2 3 1 2 2 3 2 00001011 2000 0 0 0 0 1 0 1 1 2 0 0 0 0 1 1 1 1 0 4 5 3 QAMOQER:QoOw@wrha s200311 1 1 1 1o play agaio press 1 2 0 2 1 1 1 0 2 1 2 1 2 1 1 1 1 1 1 1 2 0 3 1 0 0 1 1 0 1 1 3 3 0 3 2 1 0 0 1 0 1 0 2 0 2 2 0 1 0

<>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 3 Lnai 6323

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

3642159389, 978-3642159381

More Books

Students also viewed these Databases questions

Question

Demonstrate three aspects of assessing group performance?

Answered: 1 week ago