Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.ArrayList;
- public class MineBoard {
- private int[][] board;
- private String[][] boardh;
- private int mines;
- //constructor
- //uses int to determine size and mines
- public MineBoard (int a) {
- if (a == 2) {
- board = new int[18][18];
- mines = 40;
- } else if (a == 3) {
- board = new int[26][26];
- mines = 99;
- } else {
- board = new int[10][10];
- mines = 10;
- }
- boardh = new String[board.length-2][board.length-2];
- for (int i = 0; i < board.length; i++) {
- board[0][i] = -1;
- board[9][i] = -1;
- board[i][9] = -1;
- board[i][0] = -1;
- }
- for (int i = 0; i < boardh.length; i++) {
- for (int j = 0; j < boardh.length; j++) {
- boardh[i][j] = " - ";
- }
- }
- }
- //fills board later so that the user cannot lose on their first turn
- public void fillBoard(int a, int b) {
- Random rand = new Random();
- int c,d;
- a++;
- b++;
- for (int i = 0; i < mines; i++) {
- c = rand.nextInt(board.length-2) + 1;
- d = rand.nextInt(board.length-2) + 1;
- if (((c >= a-1 && c <= a+1) && (d >= b-1 && d <= b+1)) || ((c >= b-1 && c <= b+1) && (d >= a-1 && d <= a+1))) {
- i--;
- } else if (board[c][d] == 10) {
- i--;
- } else {
- board[c][d] = 10;
- }
- }
- for (int i = 1; i < board.length-1; i++) {
- for (int j = 1; j < board.length-1; j++) {
- board[i][j] = checkBorder(i,j);
- }
- }
- }
- // helper to remove spam in the double for loop in above method
- public int checkBorder (int i, int j) {
- int sum = 0;
- if (board[i][j] == 10) {
- return 10;
- }
- for (int f = i-1; f < i + 2; f++) {
- for (int g = j-1; g < j + 2; g++) {
- if (board[f][g] == 10) {
- sum++;
- }
- }
- }
- return sum;
- }
- public void zeroBorder (int i, int j) {
- if (i <= 0 || j <= 0) {
- return;
- }
- if (i > boardh.length || j > boardh.length) {
- return;
- }
- if (!(boardh[i-1][j-1]).equals(" - ")) {
- return;
- }
- boardh[i-1][j-1] = " " + board[i][j] + " ";
- if (board[i][j] != 0) {
- return;
- }
- zeroBorder(i+1,j);
- zeroBorder(i-1,j);
- zeroBorder(i,j+1);
- zeroBorder(i,j-1);
- }
- public boolean uncover (int i,int j) {
- if (i < 0 || j < 0) {
- return true;
- }
- if (i > boardh.length || j > boardh.length) {
- return true;
- }
- if (!boardh[i][j].equals(" - ")) {
- return true;
- }
- if ((board[i+1][j+1]) == 10) {
- boardh[i][j] = " 10 ";
- return false;
- }
- if (board[i+1][j+1] == 0) {
- zeroBorder(i+1,j+1);
- return true;
- }
- boardh[i][j] = " " + board[i+1][j+1] + " ";
- return true;
- }
- public boolean win () {
- int count = 0;
- for (int i = 0; i < boardh.length; i++) {
- for (int j = 0; j < boardh.length; j++) {
- if (!boardh[i][j].equals(" - ")) {
- count++;
- }
- }
- }
- int difference = (boardh.length * boardh.length) - count;
- if (difference == mines) {
- return true;
- } else {
- return false;
- }
- }
- public String toString() {
- String a = "";
- for (int i = 0; i < boardh.length; i++) {
- for (int j = 0; j < boardh.length; j++) {
- a += boardh[i][j];
- }
- a += "\n";
- }
- return a;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement