Need Help for one java class. MAP please assume that the spot and thing class work
class Map
A Map represents all the spots and things in a simulation, and provides some support methods for them as well. You will likely need to create the enumerations, interfaces, and many of the Thing classes before you can pull them all together with the Map class.
Fields
protected Spot[][] floorplan protected Thing[] things protected java.io.PrintStream log
Methods
Map(String filename, PrintStream log). Reads the given file to construct a map and all spots/things on it. Any messages (such as safety or death messages) from this map will be sent to log.
any "map file" that is named in the first argument ("String filename") is assumed to have the same number of characters in each line, each line ends with a " " character, and each character is assumed to be one of our valid characters (see Map Representations). If any of these assumptions is invalidated, or the file doesn't even exist, your program does not need to function (behavior is undefined). You may add throws IOException to your method signature as necessary. Manual Inspection Criteria (5%) : Well documented, elegant solution to reading the map text files. (See more details in the grading section of this project specification).
public boolean onMap(Coord c). Answers if the given Coord is on this Map. public Spot spotAt(Coord c). Returns the Spot at the given Coord, if present (from the floorplan). If the Coord isn't on the map, this method will return null. public int peopleRemaining(). Returns how many people are still trying to escape. Status.Safe and Status.Dead people don't count, but Status.Escaping people do get counted. Threats don't count either, of course. public void addThing(Thing a). Occasionally new things should be added (usually these are threats that have spawned more spots of slime or haze). Accept a new Thing (assumed to be on the map), and cause the things field to be one spot longer, with this new one at the end. Remember, arrays can't change length. How will you get around this? (you can't change the type of the field)
Manual Inspection Criteria (5%) : well-documented description of your approach to dealing with growing arrays (and a matching implementation).
public Thing[] thingsAt(Coord c). Return an array of all Thing values found at the given coordinate. Preserve the order of original appearance. If c isn't on the map, or no Thing is there, an empty array is returned. public boolean canLookThroughLocation(Coord c). Considering the spot and all things on that spot, can a person look through this location? If c isn't on the map, false is returned.This method is already created in spot!! public boolean canPassThroughLocation(Coord c). Considering the spots and all things on that spot, can a person pass through this location? If c isn't on the map, false is returned. This method is already created in spot!!
public void iterate(). For all Thing items we saw in their original order of discovery/spawning, allow them to perform an action by calling their doAction() method. This may alter the map by changing Thing positions or adding new Things.
after allowing all things to make their moves, the message "map:" must be sent to log, and then this map's toString() representation must also be sent to the log. if a GreenSlime is spawned into a spot containing people, it immediately kills them; it doesn't need to wait for the next iteration. Note, this doesn't count as taking an action (do not call doAction on the spawned threat).
@Override public String toString(). Returns a String that represents one entity per location. When more than one thing exists at a location, here is the order of preference (higher in the list is chosen):
Wall Haze GreenSlime any Person (prefer the last in the map's things array) Exit, Open, or Sign spots (any direction)
Tester for Map
import org.junit.*; import org.junit.Test; import org.junit.rules.Timeout; import static org.junit.Assert.*; import java.util.*; import java.io.*;
public class MapTests { public static void main(String args[]){ org.junit.runner.JUnitCore.main("MapTests"); } // public static String mapStrDefault = "... ... ... ... "; // public static Map defaultMap(){ return stringToMap(mapStrDefault); } public static final String inFileName = "TEST_FILES/ephemeral_testing_file.txt"; public final static String outFileName = "TEST_FILES/ephemeral_testing_output_file.txt"; public static Map stringToMap(String s) { try { // write the string to the file. File f = new File(inFileName); PrintWriter pw = new PrintWriter(f); pw.print(s); pw.close(); // create the Map. Map m = new Map(inFileName, new PrintStream(new File(outFileName))); // delete the file. f.delete(); return m; } // convert a checked exception (IOException+) to unchecked exception. catch (IOException e){ throw new RuntimeException("issues with stringToMap. " +e); } } public static void assertEqThings(Thing[] t1, Thing[] t2){ if (t1.length != t2.length){ fail("uneven Thing[] lengths: expected "+t1.length+", found +"+t2.length);} for (int i=0; iv^ "); assertEquals(Spot.Wall, m.spotAt(new Coord(0,0))); assertEquals(Spot.Open, m.spotAt(new Coord(0,1))); assertEquals(Spot.Exit, m.spotAt(new Coord(0,2))); assertEquals(Spot.SignW,m.spotAt(new Coord(0,3))); assertEquals(Spot.SignE,m.spotAt(new Coord(0,4))); assertEquals(Spot.SignS,m.spotAt(new Coord(0,5))); assertEquals(Spot.SignN,m.spotAt(new Coord(0,6))); } // off-map locations must return null. @Test public void map_spotAt2(){ Map m = stringToMap("|.e. <>v^ "); // 2x4 assertNull(m.spotAt(new Coord( 2, 4))); assertNull(m.spotAt(new Coord(10,10))); assertNull(m.spotAt(new Coord(-1,-1))); } @Test public void map_peopleRemaining1(){ Map m = stringToMap("zzz .|e "); // 2x4 assertEquals(3,m.peopleRemaining()); } @Test public void map_peopleRemaining2(){ Map m = stringToMap("zzz .|e "); // 2x4 assertEquals(3,m.peopleRemaining()); ((Person)m.things[0]).status = Status.Safe; assertEquals(2,m.peopleRemaining()); ((Person)m.things[1]).status = Status.Safe; assertEquals(1,m.peopleRemaining()); ((Person)m.things[2]).status = Status.Safe; assertEquals(0,m.peopleRemaining()); } @Test public void map_peopleRemaining3(){ Map m = stringToMap("f.f .f. "); // 2x4 assertEquals(3,m.peopleRemaining()); ((Follower)m.things[0]).status = Status.Safe; assertEquals(2,m.peopleRemaining()); ((Person)m.things[1]).status = Status.Safe; assertEquals(1,m.peopleRemaining()); ((Person)m.things[2]).status = Status.Safe; assertEquals(0,m.peopleRemaining()); } @Test public void map_peopleRemaining4(){ Map m = stringToMap("f.f .f. "); // 2x4 assertEquals(3,m.peopleRemaining()); ((Follower)m.things[0]).status = Status.Safe; assertEquals(2,m.peopleRemaining()); ((Person)m.things[1]).status = Status.Dead; assertEquals(1,m.peopleRemaining()); ((Person)m.things[2]).status = Status.Escaping; // still present... assertEquals(1,m.peopleRemaining()); } @Test public void map_addThing1(){ Map m = stringToMap("..... ..... ..... "); assertThingsMatch(m.things, new Thing[]{}); Zoolander z1 = new Zoolander(new Coord(0,0),m, m.log); Zoolander z2 = new Zoolander(new Coord(1,2),m, m.log); Zoolander z3 = new Zoolander(new Coord(2,1),m, m.log); Zoolander z4 = new Zoolander(new Coord(1,3),m, m.log); Zoolander z5 = new Zoolander(new Coord(2,0),m, m.log); Follower f1 = new Follower (new Coord(1,2),m, m.log); Follower f2 = new Follower (new Coord(1,0),m, m.log); Follower f3 = new Follower (new Coord(0,2),m, m.log); Follower f4 = new Follower (new Coord(2,4),m, m.log); m.addThing(z1); assertThingsMatch(m.things, new Thing[]{z1}); m.addThing(z2); assertThingsMatch(m.things, new Thing[]{z1,z2}); m.addThing(f1); assertThingsMatch(m.things, new Thing[]{z1,z2,f1}); m.addThing(z3); assertThingsMatch(m.things, new Thing[]{z1,z2,f1,z3}); m.addThing(f2); assertThingsMatch(m.things, new Thing[]{z1,z2,f1,z3,f2}); m.addThing(z4); assertThingsMatch(m.things, new Thing[]{z1,z2,f1,z3,f2,z4}); m.addThing(z5); assertThingsMatch(m.things, new Thing[]{z1,z2,f1,z3,f2,z4,z5}); } @Test public void map_addThing2(){ Map m = stringToMap("..... ..... ..... "); assertThingsMatch(m.things, new Thing[]{}); GreenSlime g1 = new GreenSlime(new Coord(2,3),m, m.log); GreenSlime g2 = new GreenSlime(new Coord(2,3),m, m.log); Haze h1 = new Haze (new Coord(2,3),m, m.log); Haze h2 = new Haze (new Coord(2,3),m, m.log); Zoolander z1 = new Zoolander(new Coord(0,0),m, m.log); Zoolander z2 = new Zoolander(new Coord(1,2),m, m.log); Zoolander z3 = new Zoolander(new Coord(2,1),m, m.log); Zoolander z4 = new Zoolander(new Coord(1,3),m, m.log); Zoolander z5 = new Zoolander(new Coord(2,0),m, m.log); Follower f1 = new Follower (new Coord(1,2),m, m.log); Follower f2 = new Follower (new Coord(1,0),m, m.log); Follower f3 = new Follower (new Coord(0,2),m, m.log); Follower f4 = new Follower (new Coord(2,4),m, m.log); m.addThing(g1); m.addThing(g2); m.addThing(h1); m.addThing(h2); m.addThing(z1); m.addThing(z2); m.addThing(z3); m.addThing(z4); m.addThing(z5); m.addThing(f1); m.addThing(f2); m.addThing(f3); m.addThing(f4); assertThingsMatch(m.things, new Thing[]{g1,g2,h1,h2,z1,z2,z3,z4,z5,f1,f2,f3,f4}); } @Test public void map_addThing3(){ Map m = stringToMap("..... ..... ..... "); Zoolander z1 = new Zoolander(new Coord(1,2),m, m.log); Zoolander z2 = new Zoolander(new Coord(1,2),m, m.log); Follower f1 = new Follower (new Coord(1,2),m, m.log); Follower f2 = new Follower (new Coord(1,2),m, m.log); Follower f3 = new Follower (new Coord(1,2),m, m.log); m.addThing(z1); assertThingsMatch(m.things, new Thing[]{z1}); m.addThing(z2); assertThingsMatch(m.things, new Thing[]{z1,z2}); m.addThing(f1); assertThingsMatch(m.things, new Thing[]{z1,z2,f1}); m.addThing(f2); assertThingsMatch(m.things, new Thing[]{z1,z2,f1,f2}); m.addThing(f3); assertThingsMatch(m.things, new Thing[]{z1,z2,f1,f2,f3}); } @Test public void map_thingsAt1(){ Map m = stringToMap("..... ...f. ..z.. "); Follower f = new Follower (new Coord(1,3), m, m.log); Zoolander z = new Zoolander(new Coord(2,2), m, m.log); assertThingsMatch(m.thingsAt(new Coord(1,3)), new Thing[]{f}); assertThingsMatch(m.thingsAt(new Coord(2,2)), new Thing[]{z}); } // empty spots on the map return an empty array, not null. @Test public void map_thingsAt2(){ Map m = stringToMap("..... ..... ..... "); assertThingsMatch(m.thingsAt(new Coord(1,1)), new Thing[]{}); } // spots not on the map return an empty array, not null. @Test public void map_thingsAt3(){ Map m = stringToMap("..... ..... ..... "); assertThingsMatch(m.thingsAt(new Coord(10,10)), new Thing[]{}); } // we might add more things to a spot somehow and we must find them all together. // this is hard to orchestrate in a live map, but is easy in a test case. @Test public void map_thingsAt4(){ Map m = stringToMap("..... ..z.. ..... "); Coord c12 = new Coord (1,2); Thing[] adds = { // z z z f f f new Zoolander(c12,m, m.log), new Zoolander(c12,m, m.log), new Zoolander(c12,m, m.log), new Follower (c12,m, m.log), new Follower (c12,m, m.log), new Follower (c12,m, m.log) }; // add them all except the first one, which was on the map. for (int i=1;i<^v| "); assertTrue (m.canLookThroughLocation(new Coord(0,0))); assertTrue (m.canLookThroughLocation(new Coord(0,1))); assertTrue (m.canLookThroughLocation(new Coord(0,2))); assertTrue (m.canLookThroughLocation(new Coord(0,3))); assertTrue (m.canLookThroughLocation(new Coord(0,4))); assertTrue (m.canLookThroughLocation(new Coord(0,5))); assertFalse(m.canLookThroughLocation(new Coord(0,6))); // wall } // check with no things on the map. @Test public void map_through2(){ Map m = stringToMap(".e><^v| "); assertTrue (m.canPassThroughLocation(new Coord(0,0))); assertTrue (m.canPassThroughLocation(new Coord(0,1))); assertTrue (m.canPassThroughLocation(new Coord(0,2))); assertTrue (m.canPassThroughLocation(new Coord(0,3))); assertTrue (m.canPassThroughLocation(new Coord(0,4))); assertTrue (m.canPassThroughLocation(new Coord(0,5))); assertFalse(m.canPassThroughLocation(new Coord(0,6))); // wall } // check with one of each kind of thing on a spot. // we can see through all the locations except haze. @Test public void map_through3(){ Map m = stringToMap("fzg~ "); assertTrue (m.canLookThroughLocation(new Coord(0,0))); assertTrue (m.canLookThroughLocation(new Coord(0,1))); assertTrue (m.canLookThroughLocation(new Coord(0,2))); assertFalse(m.canLookThroughLocation(new Coord(0,3))); // haze } // check with one of each kind of thing on a spot. // we can pass through all the locations. @Test public void map_through4(){ Map m = stringToMap("fzg~ "); assertTrue(m.canPassThroughLocation(new Coord(0,0))); assertTrue(m.canPassThroughLocation(new Coord(0,1))); assertTrue(m.canPassThroughLocation(new Coord(0,2))); assertTrue(m.canPassThroughLocation(new Coord(0,3))); } // a spot with a person and haze can't be seen through. @Test public void map_through5(){ Map m = stringToMap("||| " +"|f| " +"||| "); m.addThing(new Haze(new Coord(1,1),m, m.log)); assertFalse(m.canLookThroughLocation(new Coord(1,1))); m = stringToMap("||| " +"|z| " +"||| "); m.addThing(new Haze(new Coord(1,1),m, m.log)); assertFalse(m.canLookThroughLocation(new Coord(1,1))); } String mapStr1 = ".... eeee |||| <>v^ "; String mapStr2 = "|||| egf| |||| |z.e "; @Test public void map_toString1() { assertEquals(mapStr1, stringToMap(mapStr1).toString()); } @Test public void map_toString2() { assertEquals(mapStr2, stringToMap(mapStr2).toString()); } String mapStr3 = "|e| " +"|.| " +"|.| " +"|z| " +"||| "; @Test public void map_iterate1(){ Map m = stringToMap(mapStr3); m.iterate(); // simulate the correct movement. Zoolander z = new Zoolander(new Coord(3,1),m, m.log); z.setLoc(new Coord(2,1)); // check actual vs. expected movement. assertEqThing( m.thingsAt(new Coord(2,1))[0], z); } String mapStr4 = "||||||| " + "|.....| " + "|..~..| " + "|.....| " + "||||||| " ; @Test public void map_iterate2(){ Map m = stringToMap(mapStr4); assertEqThings(new Thing[]{}, m.thingsAt(new Coord(2,2))); // left of ~ m.iterate(); assertEqThings(new Thing[]{}, m.thingsAt(new Coord(2,2))); // left of ~ m.iterate(); // eventually, haze spreads to here. Haze h = new Haze(new Coord(2,2),m,m.log); assertEqThing(h, m.thingsAt(new Coord(2,2))[0]); // left of ~ } @Test public void map_iterate3(){ Map m = stringToMap(mapStr4); assertEqThings(new Thing[]{}, m.thingsAt(new Coord(2,2))); // left of ~ m.iterate(); assertEqThings(new Thing[]{}, m.thingsAt(new Coord(2,2))); // left of ~ m.iterate(); // eventually, haze spreads to here. Haze h = new Haze(new Coord(2,2),m,m.log); assertEqThing(h, m.thingsAt(new Coord(2,2))[0]); // left of ~ m.iterate(); h.doAction(); assertEqThing(h, m.thingsAt(new Coord(2,2))[0]); // left of ~ m.iterate(); h.doAction(); // but we never get duplicates. assertEqThing(new Haze(new Coord(2,2),m,m.log), m.thingsAt(new Coord(2,2))[0]); // left of ~ } }