-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMap.java
159 lines (148 loc) · 5.02 KB
/
Map.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
import java.awt.Font;
import java.util.HashSet;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;
/**
* UI and logic for map. map operates with location represented by an int
* Includes move() method so that other classes don't have to worry too much
* about how the map works
*
* @author Ari
* @version 2019-12-17
*/
public class Map extends JFrame
{
private Font font;
private JPanel panel;
private JTextArea mapArea;
private String map;
//Using HashSets rather than ArrayLists because
//https://stackoverflow.com/a/32552348
private HashSet<Integer> northEdge = new HashSet<>();
private HashSet<Integer> eastEdge = new HashSet<>();
private HashSet<Integer> southEdge = new HashSet<>();
private HashSet<Integer> westEdge = new HashSet<>();
private HashSet<Integer> outOfBounds = new HashSet<>();
/**
* Constructor for objects of class Map
*/
public Map()
{
setSize(1000, 650);
font = new Font("Monospaced", Font.PLAIN, 18);
panel = new JPanel();
mapArea = new JTextArea(24, 80);
map = "";
mapArea.setFont(font);
mapArea.setEditable(false);
panel.add(mapArea);
add(panel);
setVisible(true);
setInitialMap();
setBounds();
}
/**
* Set up initial map.
* 89 characters per line including the \n (one character)
* Total of 2047 characters
*/
private void setInitialMap()
{
for(int i = 0; i < 23; i++) {
map += "........................................................................................\n";
}
mapArea.append(map);
}
/**
* Set up HashSets to hold edges and bounds
*/
private void setBounds()
{
for(int i = 0; i < 88; i++) {
northEdge.add(i);
}
for(int i = 87; i < 2046; i += 89) {
eastEdge.add(i);
}
for(int i = 1958; i < 2046; i++) {
southEdge.add(i);
}
for(int i = 0; i < 1959; i += 89) {
westEdge.add(i);
}
for(int i = 88; i < 2047; i += 89) {
outOfBounds.add(i);
}
}
/**
* Move character to new location one distance away.
* Includes checks so you don't wrap around the map
*
* @param character String of character used to represent character
* @param oldLocation int of location where character is prior to moving
* @param direction int of direction to move (from numpad)
* @return the new location
*/
public int move(String character, int oldLocation, int direction)
{
int location = oldLocation;
if(direction < 1 || direction == 5 || direction > 9 ||
outOfBounds.contains(location)) {
System.out.println("Please input a valid direction");
} else if(direction == 1 && !(southEdge.contains(location) ||
westEdge.contains(location))) {
location += 88;
} else if(direction == 2 && !southEdge.contains(location)) {
location += 89;
} else if(direction == 3 && !(southEdge.contains(location) ||
eastEdge.contains(location))) {
location += 90;
} else if(direction == 4 && !westEdge.contains(location)) {
location -= 1;
} else if(direction == 6 && !eastEdge.contains(location)) {
location += 1;
} else if(direction == 7 && !(northEdge.contains(location) ||
westEdge.contains(location))) {
location -= 90;
} else if(direction == 8 && !northEdge.contains(location)) {
location -=89;
} else if(direction == 9 && !(northEdge.contains(location) ||
eastEdge.contains(location))) {
location -= 88;
} else {
System.out.println("A fight has been had with the wall and");
System.out.println("the wall won. A cry of pain is heard:");
}
if(!(location == oldLocation) &&
getCharacter(location).equals(".")) {
mapArea.replaceRange(".", oldLocation, oldLocation + 1);
mapArea.replaceRange(character, location, location + 1);
}
return location;
}
/**
* Show new character in given location
*
* @param character String representation of character
* @param location int of location to be spawned
*/
public void setCharacter(String character, int location)
{
mapArea.replaceRange(character, location, location + 1);
}
/**
* @param location the location to check
* @return character at given location
*/
public String getCharacter(int location)
{
try {
return mapArea.getText(location, 1);
} catch(BadLocationException ble) {
System.out.println("Programmer dun goofed");
return "";
}
}
}