From aae0e2db9ac9db06552d5bed18c6fe6025bbca27 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 15 May 2020 20:07:59 +0200 Subject: [PATCH] added ability to generate map array from strings --- core/src/netwerkprog/game/client/map/Map.java | 19 ++++++++++++++ core/src/test/java/MapTest.java | 26 ++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/core/src/netwerkprog/game/client/map/Map.java b/core/src/netwerkprog/game/client/map/Map.java index 1d3f953..c9da100 100644 --- a/core/src/netwerkprog/game/client/map/Map.java +++ b/core/src/netwerkprog/game/client/map/Map.java @@ -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]; } diff --git a/core/src/test/java/MapTest.java b/core/src/test/java/MapTest.java index f91cc5e..46ef0bd 100644 --- a/core/src/test/java/MapTest.java +++ b/core/src/test/java/MapTest.java @@ -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()); + + } }