Skip to content

Commit 4fab3ed

Browse files
committed
added initial ant, timer, changed btn location
1 parent 891edf6 commit 4fab3ed

File tree

3 files changed

+143
-99
lines changed

3 files changed

+143
-99
lines changed

src/Ant.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public Ant(Tile start, Tile goal, int tileSize, Tile[][] tiles) {
2929
this.goal = goal;
3030
this.tiles = tiles;
3131
loadAntImg();
32-
antX = start.getX()*tileSize;
33-
antY = start.getY()*tileSize;
32+
antX = start.getXpixel();
33+
antY = start.getYpixel();
3434
}
3535

3636
private void loadAntImg() {

src/Game.java

Lines changed: 65 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ The robot character (an ant) should find its way from the start point (home) to
2424
import java.awt.BorderLayout;
2525
import java.awt.Color;
2626
import java.awt.Dimension;
27+
import java.awt.Font;
2728
import java.awt.Graphics;
2829
import java.awt.GridLayout;
2930
import java.awt.Image;
@@ -42,7 +43,7 @@ The robot character (an ant) should find its way from the start point (home) to
4243

4344
public class Game extends JPanel implements MouseListener, MouseMotionListener {
4445

45-
private final int TILE_SIZE = 40;
46+
private final static int TILE_SIZE = 40;
4647
private final int NUM_ROWS = 16;
4748
private final int NUM_COLS = 16;
4849

@@ -69,15 +70,12 @@ public class Game extends JPanel implements MouseListener, MouseMotionListener {
6970

7071
private int timerX;
7172
private int timerY;
72-
private long startTimeBeforeSearch;
7373

74+
private long startTimeBeforeSearch;
7475
private long elapsedTimeAfterSearch;
75-
7676
private String elapsedTimeStringBeforeSearch;
7777

78-
7978
private long startTimeBeforeAnimation;
80-
8179
private long elapsedTimeAfterAnimation;
8280
private String elapsedTimeStringAfterAnimation;
8381
private boolean antReached;
@@ -93,8 +91,8 @@ public Game(JFrame frame) {
9391
buttonPanel.setLayout(new GridLayout(1, buttonCount, 10, 10));
9492

9593
// set tile offset
96-
int xOffset = (int) ((frame.getWidth() - TILE_SIZE * NUM_COLS) / 2)/TILE_SIZE;
97-
int yOffset = (int) ((frame.getHeight() - TILE_SIZE * NUM_ROWS) / 2)/TILE_SIZE;
94+
int xOffset = (int) ((frame.getWidth() - TILE_SIZE * NUM_COLS) / 2);
95+
int yOffset = (int) ((frame.getHeight() - TILE_SIZE * NUM_ROWS) / 2);
9896

9997
Tile.setxOffset(xOffset);
10098
Tile.setyOffset(yOffset);
@@ -132,8 +130,8 @@ public Game(JFrame frame) {
132130
tobeDrawn = new LinkedList<Tile>();
133131

134132
// set timer
135-
elapsedTimeStringBeforeSearch = "00:00:000";
136-
elapsedTimeStringAfterAnimation = "00:00:000";
133+
elapsedTimeStringBeforeSearch = "0:00:000";
134+
elapsedTimeStringAfterAnimation = "0:00:000";
137135
timerX = 100;
138136
timerY = frame.getHeight() - 100;
139137
startTimeBeforeSearch = System.currentTimeMillis();
@@ -154,7 +152,14 @@ public void actionPerformed(ActionEvent e) {
154152
noPath = false;
155153
startMovingAnt = false;
156154
tobeDrawn = new LinkedList<Tile>();
155+
startTimeBeforeSearch = 0;
156+
elapsedTimeAfterSearch = 0;
157+
elapsedTimeStringBeforeSearch = "0:00:000";
157158

159+
startTimeBeforeAnimation = 0;
160+
elapsedTimeAfterAnimation = 0;
161+
elapsedTimeStringAfterAnimation = "0:00:000";
162+
antReached = false;
158163
createTiles();
159164
repaint();
160165
}
@@ -173,7 +178,7 @@ private void loadFoodImg() {
173178
// the selection mode of available buttons
174179
private void selectOpenTerrain() {
175180
// Create the reset button
176-
JButton result = new JButton("<html>Open Terrain<br/><center><font size='2'>Cost: 0</font></center></html>");
181+
JButton result = new JButton("<html>Open Terrain<br/><center><font size='2'>Cost: 1</font></center></html>");
177182
result.addActionListener(new ActionListener() {
178183
@Override
179184
public void actionPerformed(ActionEvent e) {
@@ -243,6 +248,7 @@ public void actionPerformed(ActionEvent e) {
243248
if (startTile != null && goalTile != null) {
244249
ant = new Ant(startTile, goalTile, TILE_SIZE, tiles);
245250
startClicked = true;
251+
startTimeBeforeAnimation = System.currentTimeMillis();
246252
ant.search();
247253
delayPaint();
248254
}
@@ -305,12 +311,8 @@ public void actionPerformed(ActionEvent e) {
305311
private void handleGoalLocationSelection(Tile clickedTile) {
306312
goalLocationSelectionMode = false;
307313

308-
for (int i = 0; i < NUM_ROWS; i++) {
309-
for (int j = 0; j < NUM_COLS; j++) {
310-
if (tiles[i][j].isGoal()) {
311-
tiles[i][j].resetGoal();
312-
}
313-
}
314+
if(goalTile != null){
315+
goalTile.resetGoal();
314316
}
315317
// Set the clicked tile as the start location
316318
clickedTile.setGoal();
@@ -321,12 +323,8 @@ private void handleGoalLocationSelection(Tile clickedTile) {
321323
private void handleStartLocationSelection(Tile clickedTile) {
322324
startLocationSelectionMode = false;
323325

324-
for (int i = 0; i < NUM_ROWS; i++) {
325-
for (int j = 0; j < NUM_COLS; j++) {
326-
if (tiles[i][j].isStart()) {
327-
tiles[i][j].resetStart();
328-
}
329-
}
326+
if(startTile != null){
327+
startTile.resetStart();
330328
}
331329
// Set the clicked tile as the start location
332330
clickedTile.setStart();
@@ -337,29 +335,26 @@ private void handleStartLocationSelection(Tile clickedTile) {
337335
private void changeTileState(MouseEvent e) {
338336
int row = e.getX();
339337
int col = e.getY();
340-
341-
if (row >= 0 && row < NUM_ROWS * TILE_SIZE && col >= 0 && col < NUM_COLS * TILE_SIZE) {
342-
343-
Tile clickedTile = fetchTile(row, col);
344-
345-
if (clickedTile != null && clickedTile != lastDraggedTile) {
346-
lastDraggedTile = clickedTile;
347-
348-
if (startLocationSelectionMode) {
349-
handleStartLocationSelection(clickedTile);
350-
} else if (goalLocationSelectionMode) {
351-
handleGoalLocationSelection(clickedTile);
352-
} else if (obstacleSelectionMode) {
353-
clickedTile.setObstacle();
354-
} else if (openTerrainSelectionMode) {
355-
clickedTile.setOpenTerrain();
356-
} else if (swamplandSelectionMode) {
357-
clickedTile.setSwampland();
358-
} else if (grasslandSelectionMode) {
359-
clickedTile.setGrassland();
360-
}
361-
repaint();
338+
339+
Tile clickedTile = fetchTile(row, col);
340+
341+
if (clickedTile != null && clickedTile != lastDraggedTile) {
342+
lastDraggedTile = clickedTile;
343+
344+
if (startLocationSelectionMode) {
345+
handleStartLocationSelection(clickedTile);
346+
} else if (goalLocationSelectionMode) {
347+
handleGoalLocationSelection(clickedTile);
348+
} else if (obstacleSelectionMode) {
349+
clickedTile.setObstacle();
350+
} else if (openTerrainSelectionMode) {
351+
clickedTile.setOpenTerrain();
352+
} else if (swamplandSelectionMode) {
353+
clickedTile.setSwampland();
354+
} else if (grasslandSelectionMode) {
355+
clickedTile.setGrassland();
362356
}
357+
repaint();
363358
}
364359
}
365360

@@ -392,7 +387,6 @@ public void mouseExited(MouseEvent e) {}
392387

393388
private void createTiles() {
394389
tiles = new Tile[NUM_ROWS][NUM_COLS];
395-
396390

397391
for (int i = 0; i < NUM_ROWS; i++) {
398392
for (int j = 0; j < NUM_COLS; j++) {
@@ -403,11 +397,12 @@ private void createTiles() {
403397
}
404398

405399
private Tile fetchTile(int mouseX, int mouseY) {
400+
406401
Tile result = null;
407402
for (int i = 0; i < NUM_ROWS; i++) {
408403
for (int j = 0; j < NUM_COLS; j++) {
409404
Tile tile = tiles[i][j];
410-
if (tile.getTile(mouseX, mouseY, TILE_SIZE)) {
405+
if (tile.isWithinTile(mouseX, mouseY)) {
411406
result = tile;
412407
}
413408
}
@@ -432,7 +427,7 @@ public void actionPerformed(ActionEvent e) {
432427
startMovingAnt = true;
433428
tobeDrawn = ant.getPath();
434429
((Timer) e.getSource()).stop();
435-
startTimeBeforeAnimation = System.currentTimeMillis();
430+
436431
animateAnt();
437432
} else {
438433

@@ -492,9 +487,14 @@ public void animateAnt() {
492487
noPath = true;
493488

494489
} else {
490+
// first remove the start tile static image
491+
path.get(path.size() - 1).removeAntImage(true);
492+
495493
// set the ant to the start location
496-
ant.setX(path.get(path.size() - 1).getX() * TILE_SIZE);
497-
ant.setY(path.get(path.size() - 1).getY() * TILE_SIZE);
494+
ant.setX(path.get(path.size() - 1).getXpixel());
495+
ant.setY(path.get(path.size() - 1).getYpixel());
496+
497+
498498

499499
Timer timer = new Timer(delay, new ActionListener() {
500500
int i;
@@ -508,8 +508,8 @@ public void actionPerformed(ActionEvent e) {
508508

509509
Tile nextTile = path.get(i);
510510

511-
double dx = nextTile.getX() * TILE_SIZE - ant.getX();
512-
double dy = nextTile.getY() * TILE_SIZE - ant.getY();
511+
double dx = nextTile.getXpixel() - ant.getX();
512+
double dy = nextTile.getYpixel() - ant.getY();
513513

514514
// angle from current tile to next tile
515515
double angle = Math.atan2(dy, dx);
@@ -521,6 +521,11 @@ public void actionPerformed(ActionEvent e) {
521521
// if the ant is close to the next tile, remove the tile from the path
522522
if (distance < speed) {
523523
path.remove(i);
524+
525+
// if the ant reached the goal, stop the timer
526+
if (path.isEmpty()) {
527+
antReached = true;
528+
}
524529
} else {
525530
// move the ant
526531
ant.setX((int) newAntX);
@@ -548,22 +553,20 @@ protected void paintComponent(Graphics g) {
548553
// draw tiles
549554
for (int i = 0; i < NUM_ROWS; i++) {
550555
for (int j = 0; j < NUM_COLS; j++) {
551-
tiles[i][j].draw(g, TILE_SIZE);
556+
tiles[i][j].draw(g);
552557
}
553558
}
554559

555560
// draw food
556561
if (goalTile != null && goalTile.isGoal()) {
557-
g.drawImage(foodImg, goalTile.getX() * TILE_SIZE, goalTile.getY() * TILE_SIZE, TILE_SIZE, TILE_SIZE, null);
562+
g.drawImage(foodImg, goalTile.getXpixel(), goalTile.getYpixel(), TILE_SIZE, TILE_SIZE, null);
558563
}
559564

560565
// draw ant
561566
if (ant != null) {
562567
ant.draw(g, TILE_SIZE);
563568
}
564569

565-
566-
567570
// draw path
568571
drawPath(g, TILE_SIZE, tobeDrawn);
569572

@@ -575,10 +578,8 @@ protected void paintComponent(Graphics g) {
575578
}
576579

577580
// Draw the elapsed time
578-
579-
580-
581581
g.setColor(Color.RED); // Sets the color to red.
582+
g.setFont(new Font("Courier New", Font.PLAIN, 20));
582583
g.drawString("Mage Create: "+elapsedTimeStringBeforeSearch, timerX, timerY);
583584
g.drawString("Solved Time: "+elapsedTimeStringAfterAnimation, timerX, timerY + 20);
584585

@@ -639,19 +640,19 @@ public void drawPath(Graphics g, int tileSize, LinkedList<Tile> LinkedList) {
639640
Tile current = LinkedList.get(i);
640641

641642
Tile next = LinkedList.get(i + 1);
642-
643-
int x1 = (int) current.getX() * tileSize + tileSize / 2;
644-
int y1 = (int) current.getY() * tileSize + tileSize / 2;
645-
int x2 = (int) next.getX() * tileSize + tileSize / 2;
646-
int y2 = (int) next.getY() * tileSize + tileSize / 2;
643+
System.out.println("current: " + current.getXpixel() + ", " + current.getYpixel());
644+
int x1 = (int) current.getXpixel() + tileSize / 2;
645+
int y1 = (int) current.getYpixel() + tileSize / 2;
646+
int x2 = (int) next.getXpixel() + tileSize / 2;
647+
int y2 = (int) next.getYpixel() + tileSize / 2;
647648

648649
g.drawLine(x1, y1, x2, y2);
649650
// repaint();
650651
}
651652
}
652653
}
653654

654-
public int getTileSize() {
655+
public static int getTileSize() {
655656
return TILE_SIZE;
656657
}
657658

0 commit comments

Comments
 (0)