-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryBoard.java
183 lines (154 loc) · 5.48 KB
/
MemoryBoard.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MemoryBoard extends Board {
// ATTRIBUTES
private final int rows = 5;
private final int columns = 4;
private final List<Integer> numOptions;
// CONSTRUCTOR
public MemoryBoard(List<Integer> numOptions) {
super();
this.numOptions = numOptions;
this.addMatchTypes();
this.addDisappearTypes();
this.createBoardGame(this.createBoardTiles());
}
// GETTERS
public int getRows() {
return this.rows;
}
public int getColumns() {
return this.columns;
}
public List<Integer> getNumOptions() {
return numOptions;
}
// ABSTRACT METHOD IMPLEMENTATIONS
@Override
public void addMatchTypes() {
super.getMatchTypes().add(new SameTileMatch());
}
@Override
public void addDisappearTypes() {
super.getDisappearTypes().add(new SimpleDisappear());
}
@Override
public void displayBoard() {
System.out.println("Rows are displayed on the LEFT side of the board (displayed vertically).");
System.out.println("Columns are displayed on the TOP of the board (displayed horizontally).\n");
System.out.print(" ");
for (int colNum=1; colNum<=columns; colNum++) {
if (colNum == columns) {
System.out.println(" " + colNum + " ");
} else {
System.out.print(" " + colNum + " ");
}
}
for (int rowNum=1; rowNum<=rows; rowNum++) {
System.out.print(rowNum + " ");
for (int colNum=1; colNum<=columns; colNum++) {
String display = super.getGameBoard().get(rowNum-1).get(colNum-1).printDisplay();
if (colNum == columns) {
System.out.println("[" + display + "]");
} else {
System.out.print("[" + display + "]");
}
}
}
System.out.println();
}
@Override
public List<Tile> createBoardTiles() {
// main list to get create Memory Tiles
List<Tile> memoryTiles = new ArrayList<>();
// double the amount of each number (for matching)
List<Integer> allMemoryValues = new ArrayList<>(this.getNumOptions());
allMemoryValues.addAll(this.getNumOptions());
// shuffle all values
Collections.shuffle(allMemoryValues);
// create and initialize all Memory Tiles with its value
int valIndex = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
Integer value = allMemoryValues.get(valIndex % allMemoryValues.size());
memoryTiles.add(new ValueTile(i, j, value));
valIndex++;
}
}
return memoryTiles;
}
@Override
public void createBoardGame(List<Tile> tiles) {
// create list of tiles per row
List<Tile> rowTiles = new ArrayList<>();
for (Tile tile: tiles) {
// add a tile
rowTiles.add(tile);
// if the row is maxed out to column size
if (rowTiles.size() == columns) {
// add row of tiles to gameBoard
super.getGameBoard().add(rowTiles);
// empty list for new row
rowTiles = new ArrayList<>();
}
}
}
@Override
public boolean isValidMove(String move) {
// move = <ROW> <COLUMN>
String[] parts = move.split(" ");
int row = Integer.parseInt(parts[0])-1;
int col = Integer.parseInt(parts[1])-1;
// return false if tile has "disappeared" or if tile has been "matched" already
// return true if match has not been found yet
return super.getGameBoard().get(row).get(col).getDisplay() == null;
}
@Override
public void execute(List<Tile> tiles, Player player) {
for (Tile tile: tiles) {
if (tile instanceof ValueTile memTile) {
memTile.showValue();
}
}
}
@Override
public List<Tile> checkMatches() {
// find any and all matches (same match)
List<Tile> matchedTiles = new ArrayList<>();
for (Matchable IMatch: super.getMatchTypes()) {
List<Tile> foundMatches = IMatch.match(super.getGameBoard());
if (foundMatches != null) {
matchedTiles.addAll(foundMatches);
}
}
// return all matchedTiles
return (!matchedTiles.isEmpty()) ? matchedTiles : null;
}
@Override
public void removeMatchedTiles(List<Tile> matchedTiles) {
List<DisappearingTile> disappearingTiles = new ArrayList<>();
for (Tile tile : matchedTiles) {
if (tile instanceof DisappearingTile dTile) {
disappearingTiles.add(dTile);
}
}
// only one type of disappearing
for (Disappearable IDisappear : super.getDisappearTypes()) {
IDisappear.disappear(disappearingTiles, super.getGameBoard());
}
}
@Override
public boolean isFull() {
// not really isFull, more like isAllMatched or isAllX
List<List<Tile>> gameBoard = super.getGameBoard();
for (List<Tile> row : gameBoard) {
for (Tile tile : row) {
if (tile.getDisplay() == null || !(tile.getDisplay().equals("X"))) {
return false;
}
}
}
return true;
}
}