0

Supposed I have this data structure:

Hexagon structure

I am using a hash map to represent the data structure:

 HashMap<Point, Tile> board;

Where Point class is just consisting of int x and int y which is used to represent the coordinate in the data structure above.

Point(int x, int y);

In Tile class, every tile has an animal token on top. Such that:

Tile(Habitat habitat, Animal animalToken);

My aim is to calculate the score according to the animal tokens placed on top of the tiles.

This link has the rules for scoring for each animal.

What I've tried is completely mess, which always end up with 100+ of codes just to calculate a score for one specific animal. Is there any algorithm/searching algorithm I should know about to make my life easier?

PS: I have little knowledge on graph structure.

Edit:

private static ArrayList<Tile> adjacentAvailableTile(Point point)
{
    return adjacentAvailableTile(adjacentAvailablePoints(point));
}


private static ArrayList<Tile> adjacentAvailableTile(ArrayList<Point> points)
{
    ArrayList<Tile> availableTiles = new ArrayList<>();
    for (Point point : points)
    {
        Tile tile = board.get(point);
        availableTiles.add(tile);
    }
    return availableTiles;
}


private static ArrayList<Point> adjacentAvailablePoints(Point originPoint)
{
    Point[] sixAdjacentPoint = new Point[6];
    int i = 0;
    for (Position pos : Position.values())      //loop through enum Position
    {
        //sixAdjacentPoint now has the adjacent points of the given point
        sixAdjacentPoint[i++] = getPoint(originPoint, pos);
    }

    ArrayList<Point> availablePoint = new ArrayList<>();
    for (Point point : sixAdjacentPoint)
    {
        if (board.containsKey(point))
            availablePoint.add(point);
    }
    return availablePoint;
}

I now can find adjacent tiles from a given point.

st3ems
  • 39
  • 3
  • 4
    some of the logic seems pretty complex, but I suspect you can break it down in smaller functions. For instance, a function that returns all adjacent tiles from a given tile will probably be very helpful. Without the code you've written, it is hard to say what is wrong/could be improved with it though – Florent Monin Mar 30 '23 at 12:07
  • That's a good suggestion. The codes are way too long to be reviewed. I was hoping to start again from scratch and try to look in another way. – st3ems Mar 30 '23 at 12:12
  • 1
    I think, having a `GetAdjacentTiles` function should be enough to solve the bear, Hawk and Fox rules. For the Salmon, you will need something looking like [this](https://stackoverflow.com/questions/16506150/longest-path-on-a-grid-without-revisiting-grid-cells). And for the Elk, I guess some function like `NumberOfElkInThisDirection` which walks through one of the 6 directions from a tile, and counts the number of elks in a row should do the trick – Florent Monin Mar 30 '23 at 12:23
  • @FlorentMonin if I have an arrayList `ArrayList bear;` which contains the point of all bears in the board. Is this going to help? – st3ems Mar 30 '23 at 13:30
  • for the bears, I think what you can do is run an algorithm to find the connected components (so you number each "group of bear") and then, for each group, if it has exactly 2 members, then it's a pair, otherwise it isn't. I think that should work – Florent Monin Mar 31 '23 at 08:28
  • Not directly related: Why using a `HashMap` instead of an 2D array (you already have x,y coordinates in your image)) which is a wildly used way to store such a hex-raster? here a nice resource for hex-grid algorithms and data-structures https://www.redblobgames.com/grids/hexagons/ – MrSmith42 Mar 31 '23 at 10:53
  • @MrSmith42 How would you implement this in 2D array? The coordinate in this data structure has negative coordinate, which I'm not sure of how to implement in 2D array.. – st3ems Mar 31 '23 at 12:23
  • @st3ems By adding an offset all coordinates become positive. – MrSmith42 Mar 31 '23 at 13:03
  • @MrSmith42 that makes sense. But a 2D array would take up more memory than I intended to use? I also have to define the size of the 2D array, which I'm not sure of since the user can place a tile at any position.. – st3ems Mar 31 '23 at 13:46

0 Answers0