next = selectNextGrid(nextDirection,coords); if(board.contains(next) && hexNeedsBridge(board.getHex(next))) { Coords end = tryToBuildBridge(board, coords, nextDirection); if(null == end) break; coords = end; } else { connectHexes(board, coords, next, roadStyle); connectHexes(board, next, coords, roadStyle); coords = next; } if( grid.contains(coords) && x!=midX && y!=midY) {
coords = extendRoad(board, coords, nextDirection, roadStyle); if(coords == null || (grid.contains(coords) && x!=midX && y!=midY)) {
    private static HashSet<Coords> buildHubCity(int maxX, int maxY,int roads,IBoard board){        HashSet<Coords> grid = new HashSet<Coords>();        int midX = maxX/2;        int midY = maxY/2;                Vector<Integer> directions = new Vector<Integer>(8);                directions.add(N);        directions.add(NE);        directions.add(SE);        directions.add(S);        directions.add(SW);        directions.add(NW);        directions.add(E);        directions.add(W);                roads = Math.max(roads,8);        Random r = new Random(System.currentTimeMillis());        grid.add(new Coords(midX,midY));         int x=0;        int y=0;        for ( int dir = 0; dir < roads; dir++){            int baseDirection = -1;            int roadStyle = r.nextInt(2) + 1;                        if(dir < 8) {                x = midX;                y = midY;                baseDirection = directions.remove(r.nextInt(directions.size()));            } else {                switch(r.nextInt(4)) {                case 1:                    x = r.nextInt(maxX);                    y = -1;                    baseDirection = S;                    break;                case 2:                    x = r.nextInt(maxX);                    y = maxY;                    baseDirection = N;                    break;                case 3:                    x = -1;                    y = r.nextInt(maxY);                    baseDirection = NE + r.nextInt(2);                    break;                default:                    x = maxX;                    y = r.nextInt(maxY);                    baseDirection = SW + r.nextInt(2);                    break;                }            }            Coords coords = new Coords(x,y);            Coords next = null;                        int nextDirection = baseDirection;            while (coords.x >= -1 && coords.x <= maxX && coords.y >= -1 && coords.y <= maxY ){                int choice = r.nextInt(10);                if(board.contains(coords)) {                    //don't change direction offboard                    if(choice < 4) {                        //keep going                    } else if (choice < 6) {                        // turn left                        nextDirection = (5 + nextDirection) % 6;                    } else if (choice < 8) {                        //turn right                        nextDirection = (1 + nextDirection) % 6;                    } else {                        //turn towards base direction                        nextDirection = baseDirection;                    }                }                next = selectNextGrid(nextDirection,coords);                if(board.contains(next) && hexNeedsBridge(board.getHex(next))) {                    Coords end = tryToBuildBridge(board, coords, nextDirection);                     if(null == end) break;                    coords = end;                } else {                    connectHexes(board, coords, next, roadStyle);                    connectHexes(board, next, coords, roadStyle);                    coords = next;                }                if(/*dir >= 8 &&*/ grid.contains(coords) && x!=midX && y!=midY) {                    break;                }                grid.add(coords);                                x = coords.x;                y = coords.y;            }                    }        return grid;    }