“Final Game Minesweeperer” by Shreya
https://openprocessing.org/sketch/996351
License CreativeCommons Attribution ShareAlike
https://creativecommons.org/licenses/by-sa/3.0
{{filePath}}
{{width}} x {{height}}
Report Sketch
Oh, that naughty sketch! Please let us know what the issue is below.
Apply Template
Applying this template will reset your sketch and remove all your changes. Are you sure you would like to continue?
Report Sketch
Report Comment
Please confirm that you would like to report the comment below.
We will review your submission and take any actions necessary per our Community Guidelines. In addition to reporting this comment, you can also block the user to prevent any future interactions.
Please report comments only when necessary. Unnecessary or abusive use of this tool may result in your own account being suspended.
Are you sure you want to delete your sketch?
Any files uploaded will be deleted as well.
Delete Comment?
This will also delete all the replies to this comment.
Delete this tab? Any code in it will be deleted as well.
Select a collection to submit your sketch
We Need Your Support
Since 2008, OpenProcessing has provided tools for creative coders to learn, create, and share over a million open source projects in a friendly environment.
Niche websites like ours need your continued support for future development and maintenance, while keeping it an ad-free platform that respects your data and privacy!
Please consider subscribing below to show your support with a "Plus" badge on your profile and get access to many other features!
Click on a square and the field is revealed, the number on each box represents the number of bombs that surround that block Right click to flag a box
CC Attribution ShareAlike
Final Game Minesweeperer
xxxxxxxxxx
//FINAL GAME MINESWEEPER
int N_DEFAULT = 12;
int B_DEFAULT = 15;
color BACK = color(223, 234, 228);
color GRID = color(205, 150, 145);
color BOMB = color(186, 74, 110);
color FLAG = color( 12, 171, 171);
color TEXTS = color(112, 59, 105);
// cell type
char H = 'h';
char O = 'o';
char F = 'f';
int txtSize;
Grid grid = new Grid(N_DEFAULT, B_DEFAULT);
NewGame newGame;
void setup() {
size(360, 420);
frameRate(60);
txtSize = floor(grid.size*0.6);
textFont(createFont("Arial", txtSize));
textAlign(CENTER, CENTER);
stroke(BOMB);
newGame = new NewGame();
}
void draw() {
background(BACK);
grid.display();
newGame.display();
}
void mousePressed() {
grid.mousePress();
newGame.clicked();
}
class Grid {
int ww, hh;
int n;
int n_total;
int bombs;
int size = 20;
int margin = size*3;
boolean lost;
boolean won;
int firstCellPressed = -1;
ArrayList<Cell> cells = new ArrayList<Cell>();
Grid(int n, int bombs) {
init(n, bombs);
}
void init(int n, int bombs) {
firstCellPressed = -1;
lost = false;
won = false;
this.n = n;
this.bombs = bombs;
n_total = n*n;
ww = n*size + margin*2;
hh = n*size + margin*3;
// reset and create cells
cells.clear();
for (int i = 0; i < n_total; i++) {
cells.add(new Cell(i%n, floor(i/n), i));
}
}
void firstClick(int first) {
firstCellPressed = first;
// exclude cell's index clicked and neighbours
ArrayList<Integer> ix = new ArrayList<Integer>();
ix.add(first);
for (int z = 0; z < 8; z++) {
if (cells.get(first).vicini[z] > -1 ) {
ix.add(cells.get(first).vicini[z]);
}
}
Integer[] ixs = ix.toArray(new Integer[ix.size()]);
ixs = sort(ixs);
// randomly choose the positions of the bombs
boolean[] bb = new boolean[n_total - ix.size()];
for (int i = 0; i < bb.length; i++) {
bb[i] = i < bombs ? true : false;
}
bb = arrayShuffle(bb);
ArrayList<Boolean> bbs = new ArrayList<Boolean>();
for (int i = 0; i < bb.length; i++) {
bbs.add(bb[i]);
}
for (int i = 0; i < ixs.length; i++) {
bbs.add(ixs[i], false);
}
// set bombs
for (int i = 0; i < n_total; i++) {
cells.get(i).bomb = bbs.get(i);
}
// determine cell values
for (int i = 0; i < n_total; i++) {
if (cells.get(i).bomb) {
cells.get(i).value = -1;
continue;
}
for (int z = 0; z < 8; z++) {
if (cells.get(i).vicini[z] > -1 && cells.get(cells.get(i).vicini[z]).bomb ) {
cells.get(i).value++;
}
}
}
}
void display() {
int n_revealed = 0;
int n_flag = 0;
for (int i = 0; i < n_total; i++) {
cells.get(i).display();
if (cells.get(i).type== O && !cells.get(i).bomb) {
n_revealed++;
} else if (cells.get(i).type == F) {
n_flag++;
}
}
if (n_revealed == n_total - bombs) {
won = true;
}
fill(TEXTS);
if (lost) {
text("YOU LOST! GAME OVER", ww/2, margin/2);
} else if (won) {
text("YOU WON!", ww/2, margin/2);
} else {
text("FLAGS: " + n_flag + "/" + bombs, ww/2, margin/2);
}
}
void mousePress() {
if (!won && !lost) {
for (int i = 0; i < n_total; i++) {
if (cells.get(i).isUnderMouse()) {
if (firstCellPressed < 0) firstClick(i);
if (cells.get(i).type == H) {
if (mouseButton == LEFT) {
cells.get(i).type = O;
if (cells.get(i).value == 0) {
// check all adjacent empty cells
checkEmpties(i);
} else if (cells.get(i).bomb) {
lost = true;
}
} else if (mouseButton == RIGHT) {
cells.get(i).type = F;
}
} else if (cells.get(i).type == F && mouseButton == LEFT) {
cells.get(i).type = H;
}
break;
}
}
}
}
void checkEmpties(int ii) {
for (int i = 0; i < 8; i++) {
if ( cells.get(ii).vicini[i] > -1 &&
cells.get(cells.get(ii).vicini[i]).type == H ) {
cells.get(cells.get(ii).vicini[i]).type = O;
if (cells.get(cells.get(ii).vicini[i]).value == 0) {
checkEmpties(cells.get(ii).vicini[i]);
}
}
}
}
boolean[] arrayShuffle(boolean[] array) {
int index;
boolean temp;
for (int i = array.length - 1; i > 0; i--) {
index = floor(random(i+1));
temp = array[index];
array[index] = array[i];
array[i] = temp;
}
return array;
}
class Cell {
int x;
int y;
int[] id = new int[3];
int value = 0;
char type = H;
boolean bomb = false;
int[] vicini = new int[8];
Cell(int i, int j, int t) {
id[0] = i;
id[1] = j;
id[2] = t;
x = margin + id[0]*size;
y = margin + id[1]*size;
vicini[0] = id[1] - 1 >= 0 ? t - n : -1;
vicini[1] = id[1] + 1 < n ? t + n : -1;
vicini[2] = id[0] - 1 >= 0 ? t - 1 : -1;
vicini[3] = id[0] + 1 < n ? t + 1 : -1;
vicini[4] = id[1] - 1 >= 0 && id[0] - 1 >= 0 ? t - n - 1 : -1;
vicini[5] = id[1] - 1 >= 0 && id[0] + 1 < n ? t - n + 1 : -1;
vicini[6] = id[1] + 1 < n && id[0] - 1 >= 0 ? t + n - 1 : -1;
vicini[7] = id[1] + 1 < n && id[0] + 1 < n ? t + n + 1 : -1;
}
void display() {
pushStyle();
if (type == H) fill(GRID);
else if (type == F) fill(GRID, 100);
else fill(BACK);
rect(x, y, size, size);
if (type == O || lost) {
if (value > 0) {
fill(0);
text(value, size/2 + x, size/2 + y);
} else if (bomb) {
fill(BOMB);
text('B', size/2 + x, size/2 + y);
}
}
else if (type == F || won && bomb) {
if (type == H) type = F;
fill(FLAG);
text('F', size/2 + x, size/2 + y);
}
popStyle();
}
boolean isUnderMouse() {
if (mouseX > x && mouseX < x + size && mouseY > y && mouseY < y + size) {
return true;
}
return false;
}
}
}
//New Game Button
class NewGame {
float x, y;
float w, h;
NewGame() {
x = grid.ww/2;
y = grid.hh-grid.margin/2;
w = grid.margin*1.5;
h = grid.margin*0.4;
}
void display() {
pushStyle();
noStroke();
rectMode(CENTER);
fill(255, 150);
rect(x, y, w, h);
fill(TEXTS);
text("New Game", x, y);
popStyle();
}
void clicked() {
if (mousePressed && isUnderMouse()) {
grid.init(N_DEFAULT, B_DEFAULT);
}
}
boolean isUnderMouse() {
if ( abs(mouseX - x) < w/2 &&
abs(mouseY - y) < h/2 ) {
return true;
}
return false;
}
}
See More Shortcuts
Please verify your email to comment
Verify Email