Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cs2410.assn8.controller;
- import java.util.ArrayList;
- import java.util.Collections;
- import static java.lang.Boolean.FALSE;
- import static java.lang.Boolean.TRUE;
- import cs2410.assn8.view.ScoreBoard;
- import cs2410.assn8.view.Cell;
- public class MineLogic {
- private ScoreBoard scoreBoard;
- private int mineCount = 300;
- private int gameState = 0;
- // 1 - New game in progress
- // 2 -
- // 3 -
- private Cell[][] cellRefArray;
- private int[][] mineField;
- private ArrayList<Integer> randMines;
- public MineLogic(ScoreBoard board) {
- scoreBoard = board;
- cellRefArray = new Cell[20][20];
- mineField = new int[20][20];
- }
- public void updateRef(Cell[][] ref) {
- cellRefArray = ref;
- }
- public void startGame() {
- shuffleBoard();
- scoreBoard.disableStartButton(true);
- mineCount = 300;
- gameState = 1;
- cheater();
- }
- public void reveal(int x, int y) {
- //System.out.println("FROM BUTTON --- x: " + x + "y: " + y);
- revealNext(x, y + 1);
- revealNext(x + 1, y + 1);
- revealNext(x + 1, y);
- revealNext(x + 1, y - 1);
- revealNext(x, y - 1);
- revealNext(x - 1, y - 1);
- revealNext(x - 1, y);
- revealNext(x - 1, y + 1);
- }
- private void revealNext(int x, int y) {
- // Return if coordinates sent go outside the grid
- if (x < 1 || x > 20 || y < 1 || y > 20) {
- return;
- }
- // Return if button has already been clicked
- if (cellRefArray[x][y].isDisabled()) {
- return;
- }
- // Use recursion if the next cell should be blank
- // Reveal adjacent mines if the next cell should not be blank
- }
- private void gameOver() {
- }
- private void gameReset() {
- }
- private void shuffleBoard() {
- randMines = new ArrayList<>();
- for (int i = 0; i < 100; i++) {
- randMines.add(-1);
- }
- for (int i = 0; i < 300; i++) {
- randMines.add(0);
- }
- Collections.shuffle(randMines);
- int index = -1;
- for (int r = 0; r < 20; r++) {
- for (int c = 0; c < 20; c++) {
- index++;
- mineField[r][c] = randMines.get(index);
- }
- }
- /*
- for (int r = 0; r < 20; r++) {
- System.out.println();
- for (int c = 0; c < 20; c++) {
- System.out.print(mineField[r][c] + "\t");
- }
- }
- */
- for (int r = 0; r < 20; r++) {
- for (int c = 0; c < 20; c++) {
- if (mineField[r][c] != -1) {
- // NORTH
- if (!(c - 1 < 0) && !(c + 1 > 19) && !(r - 1 < 0) && !(r + 1 > 19)) {
- if (mineField[r + 1][c] == -1) {
- mineField[r][c]++;
- }
- }
- // NORTHEAST
- if (!(c - 1 < 0) && !(c + 1 > 19) && !(r - 1 < 0) && !(r + 1 > 19)) {
- if (mineField[r + 1][c + 1] == -1) {
- mineField[r][c]++;
- }
- }
- // EAST
- if (!(c - 1 < 0) && !(c + 1 > 19) && !(r - 1 < 0) && !(r + 1 > 19)) {
- if (mineField[r][c + 1] == -1) {
- mineField[r][c]++;
- }
- }
- // SOUTHEAST
- if (!(c - 1 < 0) && !(c + 1 > 19) && !(r - 1 < 0) && !(r + 1 > 19)) {
- if (mineField[r - 1][c - 1] == -1) {
- mineField[r][c]++;
- }
- }
- // SOUTH
- if (!(c - 1 < 0) && !(c + 1 > 19) && !(r - 1 < 0) && !(r + 1 > 19)) {
- if (mineField[r - 1][c] == -1) {
- mineField[r][c]++;
- }
- }
- // SOUTHWEST
- if (!(c - 1 < 0) && !(c + 1 > 19) && !(r - 1 < 0) && !(r + 1 > 19)) {
- if (mineField[r - 1][c - 1] == -1) {
- mineField[r][c]++;
- }
- }
- // WEST
- if (!(c - 1 < 0) && !(c + 1 > 19) && !(r - 1 < 0) && !(r + 1 > 19)) {
- if (mineField[r + 1][c] == -1) {
- mineField[r][c - 1]++;
- }
- }
- //NORTHWEST
- if (!(c - 1 < 0) && !(c + 1 > 19) && !(r - 1 < 0) && !(r + 1 > 19)) {
- if (mineField[r + 1][c - 1] == -1) {
- mineField[r][c]++;
- }
- }
- }
- }
- }
- }
- public void decreaseBombs () {
- scoreBoard.setTextBombs(--mineCount);
- }
- public void increaseBombs () {
- scoreBoard.setTextBombs(++mineCount);
- }
- public void cheater() {
- for (int r = 0; r < 20; r++) {
- for (int c = 0; c < 20; c++) {
- if (mineField[r][c] == -1) {
- cellRefArray[r][c].getStyleClass().clear();
- cellRefArray[r][c].getStyleClass().add("button-tile-red");
- }
- else {
- cellRefArray[r][c].setText(Integer.toString(mineField[r][c]));
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement