Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Random;
- class Utils {
- void linkHorizontalCells(Cell a, Cell b) {
- a.cells.set(4, b);
- b.cells.set(0, a);
- }
- void linkVerticalCells(Cell top, Cell bottom) {
- top.cells.set(6, bottom);
- bottom.cells.set(2, top);
- }
- }
- int rows = 5;
- int columns = 5;
- int mines = 5;
- int windowX = 1000;
- int windowY = 1000;
- class Cell {
- ArrayList<Cell> cells;
- int value; // 1 represents mine, 0 represents nothing
- boolean revealed;
- Cell(ArrayList<Cell> cells, int value) {
- this.cells = cells;
- this.value = value;
- this.revealed = false;
- }
- // method to count the number of surrounding mines of this cell
- int countMines() {
- int counter = 0;
- for (int i = 0; i < 8; i++) {
- if (this.cells.get(i).value == 1) {
- counter++;
- }
- }
- return counter;
- }
- void reveal() {
- this.revealed = true;
- for (int i = 0; i < 8; i ++) {
- if(this.cells.get(i).value == 0 && this.cells.get(i).countMines() == 0 && this.cells.get(i).revealed == false) {
- this.cells.get(i).reveal();
- }
- if(this.cells.get(i).value == 0 && this.cells.get(i).countMines() > 0 && this.cells.get(i).revealed == false) {
- this.cells.get(i).revealed = true;
- }
- }
- }
- }
- ArrayList<ArrayList<Cell>> initBoard(int rows, int columns) {
- ArrayList<ArrayList<Cell>> grid = new ArrayList<ArrayList<Cell>>(rows);
- for (int i = 0; i < rows; i++) {
- ArrayList<Cell> horizontals = new ArrayList<Cell>(columns);
- for (int j = 0; j < columns; j++) {
- Cell newCell = new Cell(new ArrayList<Cell>(8), 0);
- for (int k = 0; k < 8; k++) {
- newCell.cells.add(k, new Cell(new ArrayList<Cell>(0), -1));// dummy iniatializer
- }
- horizontals.add(newCell);
- }
- grid.add(horizontals);
- }
- return grid;
- }
- ArrayList<ArrayList<Cell>> initMines(ArrayList<ArrayList<Cell>> grid) {
- int minesInserted = 0;
- Random a = new Random();
- int m = a.nextInt(rows);
- int n = a.nextInt(columns);
- while (this.mines > minesInserted) {
- if (grid.get(m).get(n).value == 0) {
- grid.get(m).get(n).value = 1;
- minesInserted++;
- }
- m = a.nextInt(rows);
- n = a.nextInt(columns);
- }
- return grid;
- }
- ArrayList<ArrayList<Cell>> initLinks(ArrayList<ArrayList<Cell>> grid) {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- if (j - 1 >= 0) {
- grid.get(i).get(j).cells.set(0, grid.get(i).get(j - 1)); // left
- }
- if (i - 1 >= 0 && j - 1 >= 0) {
- grid.get(i).get(j).cells.set(1, grid.get(i - 1).get(j - 1)); // top left
- }
- if (i - 1 >= 0) {
- grid.get(i).get(j).cells.set(2, grid.get(i - 1).get(j)); // top
- }
- if (i - 1 >= 0 && j + 1 < columns) {
- grid.get(i).get(j).cells.set(3, grid.get(i - 1).get(j + 1)); // top right
- }
- if (j + 1 < columns) {
- grid.get(i).get(j).cells.set(4, grid.get(i).get(j + 1)); // right
- }
- if (i + 1 < rows && j + 1 < columns) {
- grid.get(i).get(j).cells.set(5, grid.get(i + 1).get(j + 1)); // bottom right
- }
- if (i + 1 < rows) {
- grid.get(i).get(j).cells.set(6, grid.get(i + 1).get(j)); // bottom
- }
- if (i + 1 < rows && j - 1 >= 0) {
- grid.get(i).get(j).cells.set(7, grid.get(i + 1).get(j - 1)); // bottom left
- }
- }
- }
- // Add mines
- // generate list of all unique i, j coordinate pairs
- // list of arrays of len 2
- return grid;
- }
- ArrayList<ArrayList<Cell>> board = new ArrayList<ArrayList<Cell>>();
- boolean alive ;
- void setup() {
- size(1000,1000);
- board = initBoard(rows, columns);
- board = initLinks(board);
- board = initMines(board);
- fill(255);
- }
- void draw() {
- for (int i = 0; i < rows; i ++) {
- for (int j = 0; j < columns ; j++) {
- Cell currentCell = board.get(i).get(j);
- int nearbyMines = currentCell.countMines();
- if (currentCell.revealed) {
- if (currentCell.value == 1) {
- fill (255, 0, 0);
- square(windowX/columns * i, windowY/rows * j, windowX/rows);
- noLoop();
- }
- else {
- fill(255);
- square(windowX/columns * i, windowY/rows * j, windowX/rows);
- fill(0);
- textSize(32);
- int dx = windowX/columns/2;
- int dy = windowY/rows/2;
- int posX = (windowY/columns * i) + dx;
- int posY = (windowX/rows* j) + dy;
- text(nearbyMines, posX, posY);
- }
- }
- else {
- fill(255);
- square(windowX/columns * i, windowY/rows * j, windowX/rows);
- }
- }
- }
- }
- void mousePressed() {
- float posX = mouseX;
- float posY = mouseY;
- float widthX = windowX;
- float widthY = windowY;
- int x = (int) (posX/widthX * columns);
- int y = (int) (posY/widthY * rows);
- board.get(x).get(y).reveal();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement