# Minesweeper Game Function Help

**URL:** <https://discourse.processing.org/t/minesweeper-game-function-help/16126>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [December 4, 2019, 9:23pm UTC](https://discourse.processing.org/t/minesweeper-game-function-help/16126 "2019-12-04T21:23:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![IsaiahZ](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@IsaiahZ](https://discourse.processing.org/u/IsaiahZ)\
**Post date:** [December 4, 2019, 9:23pm UTC](https://discourse.processing.org/t/minesweeper-game-function-help/16126/1 "2019-12-04T21:23:09Z")

</div>

Hi all, I’m attempting to make a minesweeper game. I’ve been given a template and am working off of that. Right now my problem is two things:

1. I need to write a function (I have it called drawMines), where it will accept two int array(column first, row numbers second) , and the number of entries in the array.

2. This is similar to the first but I need a function(I have it as drawNums) That accepts three characters(columns, rows and then characters to be drawn) as well as the number of entries in the arrays.

This is what I have so far, the grid is made and I just need those two functions but have no clue how to begin:

```auto
final int CELLSIZE = 50;
final int NUM_ROWS = 10;
final int NUM_COLS = 12;

void setup() {
  size(600, 500); //size MUST be (NUM_COLS*CELLSIZE) by (NUM_ROWS*CELLSIZE);
  noLoop(); //only draw once
}

void draw() {
  drawGrid();
  
  //create test arrays
  int[] mineX = {3, 1, 8, 11, 8, 4}; //mine column numbers
  int[] mineY = {4, 0, 7, 9, 2, 1}; //mine row numbers
  
  drawMines(mineX, mineY, 6); //fill the cells with mines
  
  int[] numX = {0, 10, 5, 2, 6, 7, 1, 11, 6};
  int[] numY = {2, 3, 6, 8, 0, 9, 6, 1, 4};
  char[] numValue = {'3', '1', '4', '2', '5', '6', '0', '7', '8'};
  
  drawNums(numX, numY, numValue, 9);
}

//Draw Grid Function
void drawGrid(){
 for (int i = 0; i < NUM_COLS; i++){
   for (int j = 0; j < NUM_ROWS; j++){
     int x = i*CELLSIZE;
     int y = j*CELLSIZE;
     noFill();
     noStroke();
     
     rect(x, y, CELLSIZE, CELLSIZE);
   }
 }
}
//Draw Mine Function
void drawMine(){
  
}
//Draw Numbers Function 
void drawNums(){
  
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 4, 2019, 10:49pm UTC](https://discourse.processing.org/t/minesweeper-game-function-help/16126/2 "2019-12-04T22:49:56Z")

</div>

why are your data arrays  
mineX, mineY, numX, numY, numVal  
inside draw ( redefined 60 times per sec ) ?

did you design the function call

```auto
drawMines(mineX, mineY, 6);

```

and is its purpose to draw all mines  
or might it mean only draw mine number 6

* * *

if that questionable function was given and you  
started to write that function

```auto
 //Draw Mine Function
void drawMine(){
  
}

```

it is wrong, as it not allows the calling parameters, so your program must show ERROR

---

<div class="post-metadata">

**Author:** ![TurboSlayer](https://avatars.discourse-cdn.com/v4/letter/t/4491bb/32.png) [@TurboSlayer](https://discourse.processing.org/u/TurboSlayer)\
**Post date:** [December 5, 2019, 2:27pm UTC](https://discourse.processing.org/t/minesweeper-game-function-help/16126/3 "2019-12-05T14:27:04Z")

</div>

```auto
int[] mineX = {3, 1, 8, 11, 8, 4}; //mine column numbers 
int[] mineY = {4, 0, 7, 9, 2, 1}; //mine row numbers
int[] numX = {0, 10, 5, 2, 6, 7, 1, 11, 6};
int[] numY = {2, 3, 6, 8, 0, 9, 6, 1, 4};
char[] numValue = {'3', '1', '4', '2', '5', '6', '0', '7', '8'};

void drawMine(){
  
}
//Draw Numbers Function 
void drawNums(){
  
}

```

I think these arrays need to go out of void draw(). Also I think you need to add stuff into the parentheses in void drawMine() and void drawNums().

---

<div class="post-metadata">

**Author:** ![IsaiahZ](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@IsaiahZ](https://discourse.processing.org/u/IsaiahZ)\
**Post date:** [December 5, 2019, 10:36pm UTC](https://discourse.processing.org/t/minesweeper-game-function-help/16126/4 "2019-12-05T22:36:25Z")

</div>

So you’re saying take the arrays out of draw?  
So what I want is to have drawMines(mineX, mineY);

1. I want a grid with 10 rows and 12 columns and have mines randomly appear for example;  
drawMines(3,7); where this would draw a mine in row 3, column 7, in the cell where those two meet.

2. And with drawNums I want drawNums(numX, numY, numValue);  
This one would be the numX and numY acting the same as mineX and mineY and numValue being any number between 0 and 8.

This combination of mines and numbers would spawn and fill the grid with either one number or one mine in each cell.

I guess with drawMines there could be a similar third value as numValue but it would only hold the image of the mine image I want so an array wouldn’t be necessary, would it?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 6, 2019, 1:29am UTC](https://discourse.processing.org/t/minesweeper-game-function-help/16126/5 "2019-12-06T01:29:00Z")

</div>

yes, you want make functions ( methods ) with parameter.

sadly the reference only show how to make a function without parameter  
[https://processing.org/reference/void.html](https://processing.org/reference/void.html)  
but that step is not so difficult:

```auto
void myFunction(int myparameter1, boolean myparameter2){
  if ( myparameter2 ) circle( mouseX, mouseY, myparamter1 );
}

```

well obviously that function draws something so better change the name,  
and the 2 parameter have a use too, so also find better names:

```auto
void drawCircle(int diameter, boolean showhide ){}

```

* * *

now as you say you need to use a GRID system, there are actually 2 way for the used parameter:

mineXY / numXY can mean the pixel position  
( in the processing CANVAS thinking ( 0,0, width, height ) )

or the GRID position ( 2 , 3 ) ( with a thinking of start with ( 0 , 0 )  
then you need to calculate the pix position from that for the function.

```auto
int xg = 10, yg = xg, wg = 30, hg = wg, grid = 5, many = grid*grid;

```

so this defines a GRID starting at pix ( 10,10 ) with grid ( rectangles )  
every 30 \* 30 big  
and 5 grid ( rectangles ) to the right  
and 5 grid ( rectangles ) down  
( about 5 \* 5 = 25 rects )

the start position of drawing a rectangle at GRID ( 2,3 ) goes  
int posx = xg + 2 \* wg;  
int posy = yg + 3 \* hg;  
rect( posx, posy, wg, hg );

* * *

it does not matter much what thinking you choose,  
somewhere you have to calculate these things,  
inside or outside you function.

---

<div class="post-metadata">

**Author:** ![IsaiahZ](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@IsaiahZ](https://discourse.processing.org/u/IsaiahZ)\
**Post date:** [December 6, 2019, 2:46pm UTC](https://discourse.processing.org/t/minesweeper-game-function-help/16126/6 "2019-12-06T14:46:43Z")

</div>

So would I have to do each individual cell? So let’s say I wanted a mine to appear in cell 1, meaning mineX = 1 and mineY = 1;  
How would I implement this?  
Would I use an if statement?

```auto
if (mineX ==1 && mineY == 1){
......
}

```

And if so I assume I would need to do that for mineX = 1, mineY = 2 mineX = 1, mineY = 3 etc…

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 7, 2019, 2:25am UTC](https://discourse.processing.org/t/minesweeper-game-function-help/16126/7 "2019-12-07T02:25:35Z")

</div>

as i understand

```auto
 drawMines(mineX, mineY, 6); //fill the cells with mines

```

has the grid thinking.  
so yes, inside you calculate from grid to pix as i show above  
and draw mines  
with a short FOR loop  
over your mines array  
( the ones you get in the call!! , not the globals ! )

---

<div class="post-metadata">

**Author:** ![IsaiahZ](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@IsaiahZ](https://discourse.processing.org/u/IsaiahZ)\
**Post date:** [December 7, 2019, 4:12am UTC](https://discourse.processing.org/t/minesweeper-game-function-help/16126/8 "2019-12-07T04:12:39Z")

</div>

Awesome! Thank you . This helped a lot!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 7, 2019, 5:38am UTC](https://discourse.processing.org/t/minesweeper-game-function-help/16126/9 "2019-12-07T05:38:20Z")

</div>

> [@IsaiahZ](#):
>
> and the number of entries in the array.

sorry, if that is part of the assignment you have to do / use it like that,  
BUT if it is your function design, better not do that.

```auto
//if you have a array of many a
int[] as = {1, 2, 3}; 

//and hand that to a function
void draw_it( int[] it ) {
  // the function can check how long the array is it got.
  for ( int i =0; i < it.length; i++ ) println(i, it[i] );
}

void setup() {
  // and call that function handing down the data array
  draw_it(as);
}

void draw() {
}

/* prints
0 1
1 2
2 3
*/

```
