-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
218 lines (183 loc) · 4.23 KB
/
Board.java
File metadata and controls
218 lines (183 loc) · 4.23 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import java.util.ArrayList;
/**
* Board for a grid based game
* @author Stephen
*
*/
public abstract class Board {
private Tile gameBoard[][]; //2-D grid board
private Object score; //score object; can be specialized based on game
public Board(Tile[][] board)
{
gameBoard = board;
}
public Board(int rows, int columns)
{
gameBoard = new Tile[rows][columns];
}
public Tile[][] getBoard()
{
return gameBoard;
}
public void setBoard(Tile[][] board)
{
gameBoard = board;
}
public Object getScore()
{
return score;
}
public void setScore(Object s)
{
score = s;
}
//clears the log for the board
public void clearLog()
{
SP_Lib.clearFile("log.txt");
}
/**
* Returns the location directly adjacent to the specified location in a given direction
* @param loc Source location
* @param dir Direction to look in
* @return Adjacent location in the specified direction
*/
public Location getLocationInDirection(Location loc, Direction dir)
{
int row = loc.getRow();
int column = loc.getColumn();
switch(dir)
{
case UP:
row -= 1;
break;
case DOWN:
row += 1;
break;
case RIGHT:
column += 1;
break;
case LEFT:
column -= 1;
break;
default:
System.out.println("Default case!?");
}
Location returnL = new Location(row, column);
if(isValid(returnL))
return returnL;
else
return null;
}
/**
* Checks if a location is valid in the current board
* @param l Location to check validity of
* @return Whether location is valid or not
*/
public boolean isValid(Location l)
{
if(l.getRow() >= 0 && l.getRow() < gameBoard.length)
if(l.getColumn() >= 0 && l.getColumn() < gameBoard[0].length)
return true;
return false;
}
/**
* Gets a tile from a given location
* @param loc Location to retrieve from
* @return Tile at the location
*/
public Tile get(Location loc)
{
if(isValid(loc))
return gameBoard[loc.getRow()][loc.getColumn()];
else
return null;
}
public Tile get(int row, int col)
{
return get(new Location(row, col));
}
/**
* Moves a tile from one location to another
* @param t Tile to move
* @param l Location to move the tile to
*/
public void moveTile(Tile t, Location l)
{
gameBoard[t.getLocation().getRow()][t.getLocation().getColumn()] = new Twenty48Tile(t.getLocation(), Types.EMPTY);
t.setLocation(l);
gameBoard[l.getRow()][l.getColumn()] = t;
}
/**
* Moves the tile at a location to another location
* @param from Location to move from
* @param to Location to move to
*/
public void moveTile(Location from, Location to)
{
moveTile(get(from), to);
}
/**
* Sets a tile at a given location, and updates its location
* @param t Tile to set
* @param l Location to set at
*/
public void set(Tile t, Location l)
{
if(isValid(l))
{
gameBoard[l.getRow()][l.getColumn()] = t;
t.setLocation(l);
}
}
/**
* Gets all empty locations in a grid
* @return All the empty locations in the grid
*/
public ArrayList<Location> getEmptyLocations()
{
ArrayList<Location> l = new ArrayList<Location>();
for(int r = 0; r < gameBoard.length; r++)
for(int c = 0; c < gameBoard[0].length; c++)
if(gameBoard[r][c].getValue() == 0)
l.add(new Location(r,c));
return l;
}
/**
* Gets the locations surrounding a given grid point
* @param loc Location to get adjacent locations around
* @return All valid adjacent locations
*/
public ArrayList<Location> getAdjacentLocations(Location loc)
{
ArrayList<Location> returnA = new ArrayList<Location>();
Location temp;
for(int x = 0; x < 4; x++)
{
temp = getLocationInDirection(loc, Direction.values()[x]);
if(temp != null)
if(isValid(temp))
returnA.add(temp);
}
return returnA;
}
/**
* Prints out the board and the score to the console; mainly for debug purposes, but can be used in a text-based game
*/
public String toString()
{
String output = new String();
String temp;
output += "Score: " + getScore().toString() + "\n";
for(int row = 0; row < gameBoard.length; row++)
{
for(int col = 0; col < gameBoard[0].length; col++)
{
temp = (gameBoard[row][col]) + " ";
output += "|" + temp.substring(0, 4);
}
output += "|\n";
}
return output;
}
}