Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //global constants and variables
- final int NUM_MINES = 10;
- final int CELLSIZE = 50;
- final int NUM_ROWS = 10;
- final int NUM_COLS = 12;
- int [] mineX = new int[NUM_ROWS];
- int [] mineY = new int[NUM_COLS];
- boolean gameOn = true;
- int xCoordinate;
- int yCoordinate;
- int[] numX = new int[NUM_ROWS*NUM_COLS];
- int[] numY = new int[NUM_ROWS*NUM_COLS];
- char[] numValue = new char[NUM_ROWS*NUM_COLS];
- int numCount = 0;
- String endGameMessage = "";
- void setup() {
- size(600, 500);
- generateMines(NUM_MINES); // generates the mines
- }
- void draw() {
- drawGrid(); // draws the grid
- drawMines(mineX, mineY, NUM_MINES); //fill the cells with mines
- drawNums(numX, numY, numValue, (NUM_ROWS*NUM_COLS)); // draws the numbers
- //checks if the player has won or not and displays a message
- if (!gameOn) {
- checkIfPlayerWon();
- gameLost();
- }
- }
- //draws a grid
- void drawGrid() {
- for (int i=0; i<NUM_COLS; i++) {
- for (int j=0; j<NUM_ROWS; j++) {
- int rectX = i*CELLSIZE;
- int rectY = j*CELLSIZE;
- fill(167);
- rect(rectX, rectY, CELLSIZE, CELLSIZE);
- }
- }
- }
- //takes in the mine arrays and calls on my drawMines function
- void drawMines(int[] x, int[] y, int num) {
- for (int i=0; i<num; i++) {
- int posX = y[i];
- int posY = x[i];
- drawMine(posX, posY);
- }
- }
- //takes in the num arrays and calls on my drawNum function
- void drawNums(int[] x, int[] y, char[] val, int num) {
- for (int i=0; i<num; i++) {
- int posX = y[i];
- int posY = x[i];
- char number = val[i];
- drawNum(posX, posY, number);
- }
- }
- //draws the mine cells
- void drawMine(int x, int y) {
- int numX = (y%NUM_COLS) * CELLSIZE;
- int numY = (x%NUM_COLS) * CELLSIZE;
- int mineSize = 40;
- strokeWeight(1);
- fill(255, 0, 0);
- rect(numX, numY, CELLSIZE, CELLSIZE);
- fill(255, 255, 0);
- ellipse(numX+CELLSIZE/2, numY+CELLSIZE/2, mineSize, mineSize);
- stroke(0);
- strokeWeight(2);
- line(numX+CELLSIZE/4, numY+CELLSIZE/4, numX+3*CELLSIZE/4, numY+3*CELLSIZE/4);
- line(numX+3*CELLSIZE/4, numY+CELLSIZE/4, numX+CELLSIZE/4, numY+3*CELLSIZE/4);
- //println(numX, numY);
- }
- //draws the numbered Cells
- void drawNum(int x, int y, char num) {
- int numX = (y%NUM_COLS) * CELLSIZE;
- int numY = (x%NUM_COLS) * CELLSIZE;
- strokeWeight(1);
- fill(255);
- rect(numX, numY, CELLSIZE, CELLSIZE);
- textSize(40);
- textAlign(CENTER, CENTER);
- fill(0, 0, 255);
- text(num, numX + CELLSIZE/2, numY + CELLSIZE/2);
- }
- //generates a certain amount of random mines
- void generateMines(int numberOfMines) {
- boolean continu = true;
- for ( int i=0; i<numberOfMines; i++) {
- continu = true;
- do {
- int newX = int(random(NUM_COLS));
- int newY = int(random(NUM_ROWS));
- if (search(mineX, mineY, i, newX, newY) == false) {
- insert(mineX, mineY, i, newX, newY);
- continu = false;
- println(newX, newY);
- }
- } while (continu);
- }
- }
- //searches and tests for an empty spot to place a mine
- boolean search(int[] x, int[] y, int n, int col, int row) {
- boolean mines = false;
- for (int i=0; i<n; i++) {
- if (x[i] == col && y[i] == row) {
- mines = true;
- } else {
- mines = false;
- }
- }
- return mines;
- }
- //inserts the mine into an empty location
- int insert(int[] x, int[]y, int n, int col, int row) {
- if (x.length <= n || y.length <= n) {
- return 0;
- } else {
- x[n] = col;
- y[n] = row;
- return n+1;
- }
- }
- //checks on the mouse location and returns the row cell its in
- int getRowIndexFromMouseClick() {
- xCoordinate = mouseX/CELLSIZE;
- return xCoordinate;
- }
- //checks on the mouse location and returns the column cell its in
- int getColumnIndexFromMouseClick() {
- yCoordinate = mouseY/CELLSIZE;
- return yCoordinate;
- }
- //inserts a number into a guessed cell
- void insertNumber(int xCoordinate, int yCoordinate) {
- numX[numCount] = xCoordinate;
- numY[numCount] = yCoordinate;
- numValue[numCount] = countNeighbourMines(xCoordinate, yCoordinate);
- numCount += 1;
- }
- //checks the neighbouring cells for mines
- char countNeighbourMines(int xCoordinate, int yCoordinate) {
- int mineCount = 0;
- char mineCountDigit;
- for (int i=0; i <= NUM_ROWS; i++) {
- if (mineX[i] == xCoordinate && mineY[i] == yCoordinate) {
- mineCount +=1;
- }
- }
- mineCountDigit = (char)mineCount;
- return mineCountDigit;
- }
- //checks if the player won the game
- void checkIfPlayerWon() {
- if (numCount >= (NUM_ROWS*NUM_COLS) - NUM_MINES) {
- endGameMessage = "WINNER!";
- gameOn = false;
- }
- }
- //checks in the player lost the game
- void gameLost() {
- endGameMessage = "LOSER!";
- gameOn = false;
- }
- //mouse clicked function for making guesses by the player
- void mouseClicked() {
- if (gameOn) {
- xCoordinate = getRowIndexFromMouseClick();
- yCoordinate = getColumnIndexFromMouseClick();
- if (search(mineX, mineY, NUM_MINES, xCoordinate, yCoordinate)) {
- gameLost();
- } else if (!search(mineX, mineY, NUM_MINES, xCoordinate, yCoordinate)) {
- insertNumber(xCoordinate, yCoordinate);
- checkIfPlayerWon();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement