class Box {nuke
	constructor(i, j, w) {
		this.i = i; // Column index of the box
		this.j = j; // Row index of the box
		this.x = i * w; // x of boxes position
		this.y = j * w; // y of boxes position
		this.w = w; // Width of the box
		this.neighbourCount = 0; // count for boxes around that contain a nuke
		this.nuke = false; // does the box contain a nuke? 
		this.revealed = false; // is the box revealed? 
	}

	show() {
		stroke(0); // Set the stroke to black
		noFill(); // dont fill
		rect(this.x, this.y, this.w, this.w); // draw the box
		if (this.revealed) { // Check if the box is revealed
			if (this.nuke) { // if the box contains a nuke...
				fill(127); // Set the fill color to gray
				image(nukeImage, this.x + 1, this.y + 1);
			} else { //if no nuke... 
				fill(0, 100, 0, 100); // Set the fill colour
				rect(this.x, this.y, this.w, this.w); // Draw a filled rect
				if (this.neighbourCount > 0) { 	// if neighbour boxes contain nukes... 
					textAlign(CENTER); // Set the text alignment to center
					fill(0); // Set the fill color to black
					textSize(12); // Set the text size to 12 pixels
					text(this.neighbourCount, this.x + this.w * 0.5, this.y + this.w - 6); // draw number of neighbouring nukes as text
				}
			}
		}
	}

	// lets count some nukes 
	countnukes() {
		if (this.nuke) {
			this.neighbourCount = -1; // if a box has a nuke set neighbourCount to -1
			return;
		}

		let totalNeighbours = 0; //creating local variable for totalNeighbours

		// Loop through the surrounding boxes
		for (let xoff = -1; xoff <= 1; xoff++) { //xoff from -1 to 1
			let i = this.i + xoff; // set i as the column index of neighbour box by adding xoff value
			if (i < 0 || i >= columns) continue; // check if i is out of the border of the grid, if less or greater than the total columns then move to next 

			for (let yoff = -1; yoff <= 1; yoff++) { // yoff range -1 to 1 
				let j = this.j + yoff; // let j as the row index of neighbour box by adding yoff (y offset) value 
				if (j < 0 || j >= rows) continue; // check if j is out of the border of the grid

				let neighbour = grid[i][j]; // set nieghbour to value of box from grid
				if (neighbour.nuke) { // if neighbour is a nuke
					totalNeighbours++; // add +1 to totalNeighbours 
				}
			}
		}

		this.neighbourCount = totalNeighbours; // Set the neighbour count for current box
	}

	contains(x, y) { // has the mouse clicked within the box (my hitbox)
		return x > this.x && x < this.x + this.w && y > this.y && y < this.y + this.w; // if x and y is between the boundaries of the border
	}

	reveal() { // creating reveal
		this.revealed = true; // Set the reveal to true
		if (this.neighbourCount == 0) { // if box has no neighbour nukes
			this.floodFill(); // call my floodfill
		}
	}

	floodFill() { //floodfill blank space for no nukes
		// Loop through the neighbouring boxes again
		for (let xoff = -1; xoff <= 1; xoff++) { // setting x offset again
			let i = this.i + xoff; // set i as the column index of neighbour box by adding xoff value
			if (i < 0 || i >= columns) continue; // check if i is out of the border of the grid, if less or greater than the total columns then move to next 

			for (let yoff = -1; yoff <= 1; yoff++) { // y offset range
				let j = this.j + yoff; // let j as the row index of neighbour box by adding yoff (y offset) value 
				if (j < 0 || j >= rows) continue; // check if i is out of the border of the grid, if less or greater than the total columns then move to next 

				let neighbour = grid[i][j]; // set nieghbour to value of box from grid
				if (!neighbour.revealed) { // if neighbour isn't revealed
					neighbour.reveal(); // Reveal the neighbour
				}
			}
		}
	}
}