-
Notifications
You must be signed in to change notification settings - Fork 566
Expand file tree
/
Copy pathGameBoard.java
More file actions
158 lines (124 loc) · 4.45 KB
/
GameBoard.java
File metadata and controls
158 lines (124 loc) · 4.45 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package cleancode.minesweeper.tobe;
import cleancode.minesweeper.tobe.cell.Cell;
import cleancode.minesweeper.tobe.cell.Cells;
import cleancode.minesweeper.tobe.cell.EmptyCell;
import cleancode.minesweeper.tobe.cell.LandMineCell;
import cleancode.minesweeper.tobe.cell.NumberCell;
import cleancode.minesweeper.tobe.gamelevel.GameLevel;
import cleancode.minesweeper.tobe.positoion.CellPosition;
import cleancode.minesweeper.tobe.positoion.CellPositions;
import cleancode.minesweeper.tobe.positoion.RelativePosition;
import java.util.List;
public class GameBoard {
private final Cell[][] board;
private final int landMineCount;
public GameBoard(GameLevel gameLevel) {
int rowSize = gameLevel.getRowSize();
int colSize = gameLevel.getColSize();
board = new Cell[rowSize][colSize];
landMineCount = gameLevel.getLandMineCount();
}
public void flagAt(CellPosition cellPosition) {
Cell cell = findCell(cellPosition);
cell.flag();
}
public void openAt(CellPosition cellPosition) {
Cell cell = findCell(cellPosition);
cell.open();
}
public void openSurroundedCells(CellPosition cellPosition) {
if (isOpenedCell(cellPosition)) {
return;
}
if (isLandMineCellAt(cellPosition)) {
return;
}
openAt(cellPosition);
if (doesCellHaveLandMineCount(cellPosition)) {
return;
}
List<CellPosition> surroundedPositions = calculateSurroundedPositions(cellPosition, getRowSize(), getColSize());
surroundedPositions.forEach(this::openSurroundedCells);
}
private boolean doesCellHaveLandMineCount(CellPosition cellPosition) {
Cell cell = findCell(cellPosition);
return cell.hasLandMineCount();
}
private boolean isOpenedCell(CellPosition cellPosition) {
Cell cell = findCell(cellPosition);
return cell.isOpened();
}
public boolean isLandMineCellAt(CellPosition cellPosition) {
Cell cell = findCell(cellPosition);
return cell.isLandMine();
}
public boolean isAllCellChecked() {
Cells cells = Cells.from(board);
return cells.isAllChecked();
}
public boolean isInvalidCellPosition(CellPosition cellPosition) {
int rowSize = getRowSize();
int colSize = getColSize();
return cellPosition.isRowIndexMoreThanOrEqual(rowSize)
|| cellPosition.isColIndexMoreThanOrEqual(colSize);
}
public void initializeGame() {
CellPositions cellPositions = CellPositions.from(board);
initializeEmptyCells(cellPositions);
List<CellPosition> landMinePositions = cellPositions.extractRandomPositions(landMineCount);
initializeLandMineCells(landMinePositions);
List<CellPosition> numberPositionCandidates = cellPositions.subtract(landMinePositions);
initializeNumberCells(numberPositionCandidates);
}
private void initializeEmptyCells(CellPositions cellPositions) {
List<CellPosition> allPositions = cellPositions.getPositions();
for (CellPosition position : allPositions) {
updateCellAt(position, new EmptyCell());
}
}
private void initializeLandMineCells(List<CellPosition> landMinePositions) {
for (CellPosition position : landMinePositions) {
updateCellAt(position, new LandMineCell());
}
}
private void initializeNumberCells(List<CellPosition> numberPositionCandidates) {
for (CellPosition candidatePosition : numberPositionCandidates) {
int count = countNearbyLandMines(candidatePosition);
if (count != 0) {
updateCellAt(candidatePosition, new NumberCell(count));
}
}
}
private void updateCellAt(CellPosition position, Cell cell) {
board[position.getRowIndex()][position.getColIndex()] = cell;
}
public String getSign(CellPosition cellPosition) {
Cell cell = findCell(cellPosition);
return cell.getSign();
}
private Cell findCell(CellPosition cellPosition) {
return board[cellPosition.getRowIndex()][cellPosition.getColIndex()];
}
public int getRowSize() {
return board.length;
}
public int getColSize() {
return board[0].length;
}
private int countNearbyLandMines(CellPosition cellPosition) {
int rowSize = getRowSize();
int colSize = getColSize();
long count = calculateSurroundedPositions(cellPosition, rowSize, colSize).stream()
.filter(this::isLandMineCellAt)
.count();
return (int) count;
}
private List<CellPosition> calculateSurroundedPositions(CellPosition cellPosition, int rowSize, int colSize) {
return RelativePosition.SURROUNDED_POSITIONS.stream()
.filter(cellPosition::canCalculatePositionBy)
.map(cellPosition::calculatePositionBy)
.filter(position -> position.isRowIndexLessThan(rowSize))
.filter(position -> position.isColIndexLessThan(colSize))
.toList();
}
}