From 8cf834b9eacbb4459bb923cd94636fbc1a50f8d0 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 15 May 2020 19:34:43 +0200 Subject: [PATCH] add map test --- core/src/netwerkprog/game/client/Map.java | 27 ++++++++++++-- core/src/test/java/MapTest.java | 43 ++++++++++++++++++++++- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/core/src/netwerkprog/game/client/Map.java b/core/src/netwerkprog/game/client/Map.java index 672034a..cbb8e56 100644 --- a/core/src/netwerkprog/game/client/Map.java +++ b/core/src/netwerkprog/game/client/Map.java @@ -2,6 +2,7 @@ package netwerkprog.game.client; public class Map { private char[][] map; + private int tileWidth = 16; public Map(int size) { this(size,size); @@ -11,8 +12,30 @@ public class Map { this.map = new char[height][width]; } - public char get(int x, int y) { - return this.map[y][x]; + public void setTileWidth(int tileWidth) { + this.tileWidth = tileWidth; + } + + public int getTileWidth() { + return tileWidth; + } + + public char[][] get() { + return this.map; + } + + /** + * gets the char at the specified position + * @param row the row of the element + * @param col the column of the element + * @return Character.MIN_VALUE if the element does not exist, the specified element otherwise. + */ + public char get(int row, int col) { + return (row < this.map.length && col < this.map[0].length) ? this.map[row][col] : Character.MIN_VALUE; + } + + public Map(char[][] map) { + this.map = map; } /** diff --git a/core/src/test/java/MapTest.java b/core/src/test/java/MapTest.java index 8dec85d..722a14b 100644 --- a/core/src/test/java/MapTest.java +++ b/core/src/test/java/MapTest.java @@ -1,4 +1,45 @@ -package PACKAGE_NAME; +import netwerkprog.game.client.Map; +import org.junit.Assert; +import org.junit.Test; public class MapTest { + + @Test + public void testNull() { + Map map = new Map(null); + + Assert.assertEquals(-1,map.getHeight()); + Assert.assertEquals(-1,map.getWidth()); + } + + @Test + public void testWidthAndHeight() { + char[][] arr = new char[][] { + {'#','#','#','#'}, + {'#','#','#','#'}, + {'#','#','#','#'} + }; + + Map map = new Map(arr); + + Assert.assertEquals(4,map.getWidth()); + Assert.assertEquals(3,map.getHeight()); + } + + @Test + public void testGetElement() { + char[][] arr = new char[][] { + {'#','#','#','#'}, + {'#','#','#','#'}, + {'#','#','_','#'} + }; + + Map map = new Map(arr); + + Assert.assertEquals('#',map.get(1,1)); + Assert.assertEquals('_',map.get(2,2)); + Assert.assertEquals(Character.MIN_VALUE,map.get(1,8)); + Assert.assertEquals(Character.MIN_VALUE,map.get(4,2)); + Assert.assertEquals(Character.MIN_VALUE,map.get(4,8)); + } }