Skip to content

Commit 2db18e3

Browse files
committed
closes #2 added counter for each tile
1 parent 6ee0001 commit 2db18e3

File tree

1 file changed

+57
-6
lines changed

1 file changed

+57
-6
lines changed

src/Game.java

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public class Game extends JPanel implements MouseListener, MouseMotionListener {
4646
private final static int TILE_SIZE = 40;
4747
private final int NUM_ROWS = 16;
4848
private final int NUM_COLS = 16;
49+
private int obstacleCount;
50+
private int swamplandCount;
51+
private int grasslandCount;
52+
private int openTerrainCount;
53+
4954

5055
private Ant ant;
5156
private Tile[][] tiles;
@@ -70,6 +75,8 @@ public class Game extends JPanel implements MouseListener, MouseMotionListener {
7075

7176
private int timerX;
7277
private int timerY;
78+
private int terrainCountX;
79+
private int terrainCountY;
7380

7481
private long startTimeBeforeSearch;
7582
private long elapsedTimeAfterSearch;
@@ -127,12 +134,19 @@ public Game(JFrame frame) {
127134
startMovingAnt = false;
128135
noPath = false;
129136
tobeDrawn = new LinkedList<Tile>();
137+
obstacleCount = 0;
138+
swamplandCount = 0;
139+
grasslandCount = 0;
140+
openTerrainCount = 0;
130141

131142
// set timer
132143
elapsedTimeStringBeforeSearch = "0:00:000";
133144
elapsedTimeStringAfterAnimation = "0:00:000";
134145
timerX = 100;
135146
timerY = frame.getHeight() - 100;
147+
148+
terrainCountX = frame.getWidth() - 300;
149+
terrainCountY = frame.getHeight() - 150;
136150

137151
setTimerMageCreation();
138152
setTimerSolving();
@@ -152,6 +166,10 @@ public void actionPerformed(ActionEvent e) {
152166
noPath = false;
153167
startMovingAnt = false;
154168
tobeDrawn = new LinkedList<Tile>();
169+
obstacleCount = 0;
170+
swamplandCount = 0;
171+
grasslandCount = 0;
172+
openTerrainCount = 0;
155173

156174
startClicked = false;
157175
startTimeBeforeSearch = System.currentTimeMillis();
@@ -553,12 +571,7 @@ public void actionPerformed(ActionEvent e) {
553571
protected void paintComponent(Graphics g) {
554572
super.paintComponent(g);
555573

556-
// draw tiles
557-
for (int i = 0; i < NUM_ROWS; i++) {
558-
for (int j = 0; j < NUM_COLS; j++) {
559-
tiles[i][j].draw(g);
560-
}
561-
}
574+
printBoardAndCount(g);
562575

563576

564577

@@ -584,9 +597,47 @@ protected void paintComponent(Graphics g) {
584597
g.drawString("Mage Create: "+elapsedTimeStringBeforeSearch, timerX, timerY);
585598
g.drawString("Solved Time: "+elapsedTimeStringAfterAnimation, timerX, timerY + 20);
586599

600+
// draw the counts of each terrain
601+
g.setColor(Color.BLACK);
602+
g.setFont(new Font("Courier New", Font.PLAIN, 20));
603+
g.drawString("Obstacle: "+obstacleCount, terrainCountX, terrainCountY);
604+
g.drawString("Swampland: "+swamplandCount, terrainCountX, terrainCountY + 20);
605+
g.drawString("Grassland: "+grasslandCount, terrainCountX, terrainCountY + 40);
606+
g.drawString("Open Terrain: "+openTerrainCount, terrainCountX, terrainCountY + 60);
607+
587608

588609
} // end paintComponent
589610

611+
private void printBoardAndCount(Graphics g) {
612+
// draw tiles and add counter for each terrain
613+
614+
int obstacle = 0;
615+
int swampland = 0;
616+
int grassland = 0;
617+
int openTerrain = 0;
618+
619+
for (int i = 0; i < NUM_ROWS; i++) {
620+
for (int j = 0; j < NUM_COLS; j++) {
621+
tiles[i][j].draw(g);
622+
if(tiles[i][j].isObstacle()){
623+
obstacle++;
624+
} else if(tiles[i][j].isSwampland()){
625+
swampland++;
626+
} else if(tiles[i][j].isGrassland()){
627+
grassland++;
628+
} else if(tiles[i][j].isOpenTerrain()){
629+
openTerrain++;
630+
}
631+
}
632+
}
633+
obstacleCount = obstacle;
634+
swamplandCount = swampland;
635+
grasslandCount = grassland;
636+
openTerrainCount = openTerrain;
637+
}
638+
639+
640+
590641
private void setTimerMageCreation() {
591642
int delay = 50;
592643
Timer timer = new Timer(delay, new ActionListener() {

0 commit comments

Comments
 (0)