aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl/xblast/SubCell.java
blob: a95e65e6bec9e0c5e73ed36f831386f3b1e805b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package ch.epfl.xblast;

/**
 * A SubCell.
 *
 * @author Pacien TRAN-GIRARD (261948)
 * @author Timothée FLOURE (257420)
 */
public final class SubCell {

    /**
     * The number of x-subdivisions of each Cell.
     */
    public static final int SUB_COL_DIVISIONS = 16;

    /**
     * The number of y-subdivisions of each Cell.
     */
    public static final int SUB_ROW_DIVISIONS = 16;

    /**
     * The width of the board (total of sub-columns).
     */
    public static final int SUB_COLUMNS = SUB_COL_DIVISIONS * Cell.COLUMNS;

    /**
     * The height of the board (total of sub-rows).
     */
    public static final int SUB_ROWS = SUB_ROW_DIVISIONS * Cell.ROWS;

    /**
     * Returns the central SubCell of the given Cell.
     *
     * @param cell the reference Cell
     * @return the central SubCell
     */
    public static SubCell centralSubCellOf(Cell cell) {
        return new SubCell(
                cell.x() * SUB_COL_DIVISIONS + (SUB_COL_DIVISIONS / 2),
                cell.y() * SUB_ROW_DIVISIONS + (SUB_ROW_DIVISIONS / 2)
        );
    }

    /**
     * The coordinates of the SubCell.
     */
    private int x, y;

    /**
     * Instantiates a new SubCell with the given coordinates.
     *
     * @param x the x-coordinate
     * @param y the y-coordinate
     */
    public SubCell(int x, int y) {
        this.x = Cell.normalize(SUB_COLUMNS, x);
        this.y = Cell.normalize(SUB_ROWS, y);
    }

    /**
     * Returns the normalized x-coordinate of the SubCell.
     *
     * @return the x-coordinate
     */
    public int x() {
        return this.x;
    }

    /**
     * Returns the normalized y-coordinate of the SubCell.
     *
     * @return the y-coordinate
     */
    public int y() {
        return this.y;
    }

    /**
     * Computes the Manhattan distance to the given SubCell.
     *
     * @param subCell the other SubCell
     * @return the distance
     */
    private int distanceTo(SubCell subCell) {
        return Math.abs(this.x - subCell.x) + Math.abs(this.y - subCell.y);
    }

    /**
     * Returns the Manhattan distance to the closest central SubCell.
     *
     * @return the distance
     */
    public int distanceToCentral() {
        return this.distanceTo(centralSubCellOf(this.containingCell()));
    }

    /**
     * Returns T(this SubCell is central).
     *
     * @return T(this SubCell is central)
     */
    public boolean isCentral() {
        return this.distanceToCentral() == 0;
    }

    /**
     * Returns the neighboring SubCell at the given Direction.
     *
     * @param dir the Direction
     * @return the neighboring SubCell
     */
    public SubCell neighbor(Direction dir) {
        return new SubCell(this.x + dir.xVector(), this.y + dir.yVector());
    }

    /**
     * Returns the Cell containing this SubCell.
     *
     * @return the containing Cell
     */
    public Cell containingCell() {
        return new Cell(this.x / SUB_COL_DIVISIONS, this.y / SUB_ROW_DIVISIONS);
    }

    /**
     * Returns T(the given Object is equal to this SubCell (have the same coordinates)).
     *
     * @param that the Object to compare against
     * @return T(the given Object is equal to this SubCell)
     */
    @Override
    public boolean equals(Object that) {
        if (that == null) return false;
        if (that == this) return true;
        if (!(that instanceof SubCell)) return false;
        return (((SubCell) that).x == this.x && ((SubCell) that).y == this.y);
    }

    /**
     * Returns a String representation of the coordinates of the SubCell.
     *
     * @return a String representation of the coordinates of the SubCell.
     */
    @Override
    public String toString() {
        return String.format("(%d,%d)", this.x, this.y);
    }

}