added ability to generate map array from strings
This commit is contained in:
@@ -10,6 +10,25 @@ public class Map {
|
||||
this(size,size);
|
||||
}
|
||||
|
||||
public Map(String[] strings) {
|
||||
if (strings == null) {
|
||||
throw new NullPointerException("The array of strings given cannot be null!");
|
||||
}
|
||||
|
||||
int width = 0;
|
||||
//get max width in string array
|
||||
for (String s : strings) {
|
||||
if (s.length() > width) {
|
||||
width = s.length();
|
||||
}
|
||||
}
|
||||
char[][] res = new char[strings.length][width];
|
||||
for (int i = 0; i < strings.length; i++) {
|
||||
res[i] = strings[i].toCharArray();
|
||||
}
|
||||
this.map = res;
|
||||
}
|
||||
|
||||
public Map(int width, int height) {
|
||||
this.map = new char[height][width];
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ public class MapTest {
|
||||
|
||||
@Test
|
||||
public void testNull() {
|
||||
Map map = new Map(null);
|
||||
Map map = new Map((char[][]) null);
|
||||
|
||||
Assert.assertEquals(-1,map.getHeight());
|
||||
Assert.assertEquals(-1,map.getWidth());
|
||||
@@ -42,4 +42,28 @@ public class MapTest {
|
||||
Assert.assertEquals(Character.MIN_VALUE,map.get(4,2));
|
||||
Assert.assertEquals(Character.MIN_VALUE,map.get(4,8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringArray() {
|
||||
String[] strings = new String[]{
|
||||
"####",
|
||||
"# #",
|
||||
"####"
|
||||
};
|
||||
|
||||
char[][] testArr = new char[][]{
|
||||
strings[0].toCharArray(),
|
||||
strings[1].toCharArray(),
|
||||
strings[2].toCharArray()
|
||||
};
|
||||
|
||||
Map map = new Map(strings);
|
||||
|
||||
Assert.assertEquals(4,map.getWidth());
|
||||
Assert.assertEquals(3,map.getHeight());
|
||||
Assert.assertEquals('#',map.get(0,0));
|
||||
Assert.assertEquals(' ',map.get(1,1));
|
||||
Assert.assertArrayEquals(testArr,map.get());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user