-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBoard.java
More file actions
121 lines (107 loc) · 3.67 KB
/
Copy pathBoard.java
File metadata and controls
121 lines (107 loc) · 3.67 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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.HashMap;
/**
* 2D Array of Tiles
*
* @author Angela Wang
* @version June 4 2025
*/
public class Board extends Actor
{
private Tile[][] tiles;
private int offset = Tile.SIZE / 2; //since images are center anchored
private HashMap<String, Class> test = new HashMap<String, Class>(){{
put("w", WaterTile.class);
put("u", WallTile.class);
put("f", FloorTile.class);
put("s", SpikeTile.class);
put("l", LavaTile.class);
put("b", BarrelTile.class);
put("e", BlankTile.class);
put("g", GateTileEnter.class);
put("q", GateTileExit.class);
put("p", SpawnerTile.class);
}};
/**
* Default Board constructor - creates room of blank tiles
*/
public Board(){
tiles = new Tile[38][74];
for (int i = 0; i < tiles.length; i++){
for (int j = 0; j < tiles[i].length; j++){
tiles[i][j] = new FloorTile();
}
}
}
/**
* Board constructor with predetermined layout in the form of a String (each char
* in the String represents the Tile type)
* @param layout String representation of Tile types
*/
public Board(String layout){
tiles = new Tile[38][74];
int x = 0;
for (int i = 0; i < tiles.length; i++){
for (int j = 0; j < tiles[i].length; j++){
//System.out.println(layout.charAt(x));
//tiles[i][j] = new Tile(layout.charAt(x));
Object instance;
try {
instance = test.get(String.valueOf(layout.charAt(x))).newInstance();
tiles[i][j] = (Tile) instance;
tiles[i][j].setType(layout.charAt(x));
} catch (InstantiationException e) {
System.out.println("something went wrong with loading tiles.");
} catch (IllegalAccessException e){
System.out.println("something went wrong with loading tiles.");
}
x++;
}
}
}
/**
* Show Board (must be used in order for Board to show on screen because of Board's nature)
*/
public void display() {
int displayStartRow = 5;
int displayStartCol = 7;
for (int i = 0; i < tiles.length; i++) {
for (int j = 0; j < tiles[i].length; j++) {
int x = (j - displayStartCol) * Tile.SIZE + offset;
int y = (i - displayStartRow) * Tile.SIZE + offset;
getWorld().addObject(tiles[i][j], 0, 0); // Placeholder location
tiles[i][j].setRealLocation(x, y); // Set world coordinates
tiles[i][j].updateLocation(); // Sync visual position
}
}
}
/**
* Returns layout of the Board as a String
* @return String String wherein chars represent Tile type
*/
public String getLayout(){
String layout = "";
for (int i = 0 ; i < tiles.length; i++){
for (int j = 0; j < tiles[i].length; j++){
layout += tiles[i][j].getType();
}
}
return layout;
}
public int getTileRowCount(){
return tiles.length;
}
public Tile[][] getTiles(){
return tiles;
}
public int getTileRow(Tile tile) {
for (int i = 0; i < tiles.length; i++) {
for (int j = 0; j < tiles[i].length; j++) {
if (tiles[i][j] == tile) {
return i;
}
}
}
return -1; // not found
}
}