-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.java
128 lines (120 loc) · 3.26 KB
/
Board.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import java.util.Random;
public class Board {
private int width;
private int height;
private int mineCount;
private String difficulty;
public char flag = '⚑';
public char mine = '✸';
private char[][] board;
public Board(String difficulty) {
setDifficulty(difficulty);
reset();
}
public int getWidth() {
return this.width;
}
public int getHeight() {
return this.height;
}
public String getDifficulty() {
return this.difficulty;
}
// Will set the difficulty and appropriate game settings based on the
// difficulty set
public void setDifficulty(String difficulty) {
this.difficulty = difficulty.toUpperCase();
if (difficulty.equals("EASY")) {
this.mineCount = 10;
this.width = 9;
this.height = 9;
} else if (difficulty.equals("MEDIUM")) {
this.mineCount = 40;
this.width = 16;
this.height = 16;
} else if (difficulty.equals("HARD")) {
this.mineCount = 99;
this.width = 30;
this.height = 16;
} else {
System.out.println("Invalid difficulty selected. Set to easy.");
this.difficulty = "EASY";
this.mineCount = 10;
this.width = 9;
this.height = 9;
}
}
// Returns the number of mines surrounding the position
public int nearbyMines(int row, int col) {
int minesNearby = 0;
// TODO Would like to perform a loop of this
// Checks 8 surrounding squares and checks if they're piece
for (int i=row-1; i<=row+1; i++) {
for (int j=col-1; j<=col+1; j++) {
if (isPiece(i, j, this.mine)) {
minesNearby++;
}
}
}
return minesNearby;
}
// Will return true if the position is equal to 'piece'
public boolean isPiece(int row, int col, char piece) {
if (isValid(row, col) && this.board[row][col] == piece) {
return true;
} else {
return false;
}
}
// Will return true if the position is withing the bounds of the board
public boolean isValid(int row, int col) {
if (row >= 0 && row < this.height && col >= 0 && col < this.width) {
return true;
} else {
return false;
}
}
// Returns a string version of the piece at the position
public String getPieceAt(int row, int col) {
return Character.toString(this.board[row][col]);
}
// Sets/Resets the board, filling it with mines and hints
public void reset() {
board = new char[this.height][this.width];
// Generate the indexes of the mines without duplicates
int[] mineIndexes = new Random().ints(0, (this.width*this.height - 1)).limit(mineCount).distinct().toArray();
// Fill the board with the mines (X)
for (int i=0; i<mineIndexes.length; i++) {
this.board[Math.floorDiv(mineIndexes[i], this.width)][mineIndexes[i] % this.width] = this.mine;
}
// Add the numbers (mine hints)
for (int i=0; i<this.board.length; i++) {
for (int j=0; j<this.board[0].length; j++) {
if (this.board[i][j] != mine) {
int minesNearby = nearbyMines(i, j);
if (minesNearby != 0) {
this.board[i][j] = (char)(minesNearby + 48);
} else {
this.board[i][j] = ' ';
}
}
}
}
}
public char getMine() {
return this.mine;
}
public char getFlag() {
return this.flag;
}
public String toString() {
String newString = "";
for (int i=0; i<this.board.length; i++) {
for (int j=0; j<this.board[0].length; j++) {
newString += this.board[i][j];
}
newString += "\n";
}
return newString;
}
}