Minesweeper Game Function Help

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:

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(){
  
}

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

did you design the function call

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

 //Draw Mine Function
void drawMine(){
  
}

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

1 Like
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().

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?

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
but that step is not so difficult:

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:

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.

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.

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?

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…

as i understand

 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 ! )

Awesome! Thank you . This helped a lot!

1 Like

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.

//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
*/