/** * Calculates distance between two locations on the game map. * * @param t1 one location on the game map * @param t2 another location on the game map * @return distance between <code>t1</code> and <code>t2</code> */ public int getDistance(Tile t1, Tile t2) { int rowDelta = Math.abs(t1.getRow() - t2.getRow()); int colDelta = Math.abs(t1.getCol() - t2.getCol()); rowDelta = Math.min(rowDelta, rows - rowDelta); colDelta = Math.min(colDelta, cols - colDelta); return rowDelta * rowDelta + colDelta * colDelta; }
/** * Returns location with the specified offset from the specified location. * * @param tile location on the game map * @param offset offset to look up * @return location with <code>offset</code> from <cod>tile</code> */ public Tile getTile(Tile tile, Tile offset) { int row = (tile.getRow() + offset.getRow()) % rows; if (row < 0) { row += rows; } int col = (tile.getCol() + offset.getCol()) % cols; if (col < 0) { col += cols; } return new Tile(row, col); }
/** * Returns one or two orthogonal directions from one location to the another. * * @param t1 one location on the game map * @param t2 another location on the game map * @return orthogonal directions from <code>t1</code> to <code>t2</code> */ public List<Aim> getDirections(Tile t1, Tile t2) { List<Aim> directions = new ArrayList<Aim>(); if (t1.getRow() < t2.getRow()) { if (t2.getRow() - t1.getRow() >= rows / 2) { directions.add(Aim.NORTH); } else { directions.add(Aim.SOUTH); } } else if (t1.getRow() > t2.getRow()) { if (t1.getRow() - t2.getRow() >= rows / 2) { directions.add(Aim.SOUTH); } else { directions.add(Aim.NORTH); } } if (t1.getCol() < t2.getCol()) { if (t2.getCol() - t1.getCol() >= cols / 2) { directions.add(Aim.WEST); } else { directions.add(Aim.EAST); } } else if (t1.getCol() > t2.getCol()) { if (t1.getCol() - t2.getCol() >= cols / 2) { directions.add(Aim.EAST); } else { directions.add(Aim.WEST); } } return directions; }
/** * Returns one or two orthogonal directions from one location to the another. * * @param t1 one location on the game map * @param t2 another location on the game map * @return orthogonal directions from <code>t1</code> to <code>t2</code> * @throws Exception */ public Aim getDirections(Tile t1, Tile t2) throws InvalidParameterException { Aim direction = null; if (t1.getRow() < t2.getRow()) { if (t2.getRow() - t1.getRow() >= rows / 2) { direction = Aim.NORTH; } else { direction = Aim.SOUTH; } } else if (t1.getRow() > t2.getRow()) { if (t1.getRow() - t2.getRow() >= rows / 2) { direction = Aim.SOUTH; } else { direction = Aim.NORTH; } } else if (t1.getCol() < t2.getCol()) { if (t2.getCol() - t1.getCol() >= cols / 2) { direction = Aim.WEST; } else { direction = Aim.EAST; } } else if (t1.getCol() > t2.getCol()) { if (t1.getCol() - t2.getCol() >= cols / 2) { direction = Aim.EAST; } else { direction = Aim.WEST; } } else { direction = Aim.WEST; } return direction; }
/** Clears game state information about enemy ants locations. */ public void clearEnemyAnts() { Set<Tile> anemys = enemyAnts; for (Tile enemyAnt : anemys) { map[enemyAnt.getRow()][enemyAnt.getCol()] = Ilk.LAND; } anemys.clear(); }
/** Clears game state information about my ants locations. */ public void clearMyAnts() { Set<Tile> ants = myAnts; for (Tile myAnt : ants) { map[myAnt.getRow()][myAnt.getCol()] = Ilk.LAND; } ants.clear(); }
/** Calculates visible information */ public void setVision() { for (Tile antLoc : myAnts) { for (Tile locOffset : visionOffsets) { Tile newLoc = getTile(antLoc, locOffset); visible[newLoc.getRow()][newLoc.getCol()] = true; } } }
/** Calculates visible information */ public void setVision() { Set<Tile> ants = myAnts; Set<Tile> offSets = visionOffsets; for (Tile antLoc : ants) { for (Tile locOffset : offSets) { Tile newLoc = getTile(antLoc, locOffset); visible[newLoc.getRow()][newLoc.getCol()] = true; } } }
/** * Returns location in the specified direction from the specified location. * * @param tile location on the game map * @param direction direction to look up * @return location in <code>direction</code> from <cod>tile</code> */ public Tile getTile(Tile tile, Aim direction) { int row = (tile.getRow() + direction.getRowDelta()) % rows; if (row < 0) { row += rows; } int col = (tile.getCol() + direction.getColDelta()) % cols; if (col < 0) { col += cols; } return new Tile(row, col); }
/** * Updates game state information about new ants and food locations. * * @param ilk ilk to be updated * @param tile location on the game map to be updated */ public void update(Ilk ilk, Tile tile) { map[tile.getRow()][tile.getCol()] = ilk; switch (ilk) { case FOOD: foodTiles.add(tile); break; case MY_ANT: myAnts.add(tile); break; case ENEMY_ANT: enemyAnts.add(tile); break; } }
/** Clears game state information about food locations. */ public void clearFood() { for (Tile food : foodTiles) { map[food.getRow()][food.getCol()] = Ilk.LAND; } foodTiles.clear(); }
/** Clears game state information about enemy ants locations. */ public void clearEnemyAnts() { for (Tile enemyAnt : enemyAnts) { map[enemyAnt.getRow()][enemyAnt.getCol()] = Ilk.LAND; } enemyAnts.clear(); }
/** * Returns true if a location is visible this turn * * @param tile location on the game map * @return true if the location is visible */ public boolean isVisible(Tile tile) { return visible[tile.getRow()][tile.getCol()]; }
/** Clears game state information about my ants locations. */ public void clearMyAnts() { for (Tile myAnt : myAnts) { map[myAnt.getRow()][myAnt.getCol()] = Ilk.LAND; } myAnts.clear(); }
/** * Returns ilk at the location in the specified direction from the specified location. * * @param tile location on the game map * @param direction direction to look up * @return ilk at the location in <code>direction</code> from <cod>tile</code> */ public Ilk getIlk(Tile tile, Aim direction) { Tile newTile = getTile(tile, direction); return map[newTile.getRow()][newTile.getCol()]; }
/** * Sets ilk at the specified location. * * @param tile location on the game map * @param ilk ilk to be set at <code>tile</code> */ public void setIlk(Tile tile, Ilk ilk) { map[tile.getRow()][tile.getCol()] = ilk; }
/** * Returns ilk at the specified location. * * @param tile location on the game map * @return ilk at the <cod>tile</code> */ public Ilk getIlk(Tile tile) { return map[tile.getRow()][tile.getCol()]; }