فهرست منبع

count number of active towns

Kajetan Johannes Hammerle 5 سال پیش
والد
کامیت
079623c0cb
2فایلهای تغییر یافته به همراه24 افزوده شده و 1 حذف شده
  1. 1 1
      src/pathgame/PathGame.java
  2. 23 0
      src/pathgame/tilemap/TileMap.java

+ 1 - 1
src/pathgame/PathGame.java

@@ -61,7 +61,7 @@ public class PathGame implements IGame
         if(Keys.TEST_KEY.getTime() == 1)
         {
             map = TileMapGenerator.getMap(50, 50, 15);
-
+            System.out.println(map.getNumberOfTowns());
             TravellingSalesAlg.calcSalesPathLen(map);
         }
     }

+ 23 - 0
src/pathgame/tilemap/TileMap.java

@@ -18,6 +18,7 @@ public class TileMap
         private int index = -1;
     }
     
+    private int towns = 0;
     private final int width;
     private final int height;
     private final int[][] tiles;
@@ -63,6 +64,14 @@ public class TileMap
     public void setTile(int x, int y, Tile tile)
     {
         dirty = true;
+        if(tiles[x][y] == Tiles.TOWN.getId())
+        {
+            towns--;
+        }
+        if(tile == Tiles.TOWN)
+        {
+            towns++;
+        }
         tiles[x][y] = tile.getId();
     }
     
@@ -114,6 +123,11 @@ public class TileMap
         }
     }
     
+    /** Slowly Converts a tile into blocked town.
+     *
+     * @param x the x coordinate of the tile
+     * @param y the y coordinate of the tile
+     */
     public void convertTown(int x, int y)
     {
         TownConverter tc = new TownConverter();
@@ -121,5 +135,14 @@ public class TileMap
         tc.y = y;
         townConverters.add(tc);
     }
+    
+    /** Returns the number of active towns.
+     *
+     * @return the number of active towns
+     */
+    public int getNumberOfTowns()
+    {
+        return towns;
+    }
 }