/* 
 * Domino.java
 * 
 * Computer Science E-22
 */

/**
 * A class representing a domino tile
 * 
 * For example, the 5-2 domino looks like this:
 * 
 *   ╭───────────┬───────────╮
 *   │  ●     ●  │   ●       │
 *   │     ●     │           │
 *   │  ●     ●  │        ●  │
 *   ╰───────────┴───────────╯
 *
 * Instances of this class are immutable. To obtain the flipped version of
 * a domino, use the return value of the flipped() method. The equals() method
 * returns true when both sides of two dominoes are the same, flipped or not.
 */
public class Domino {
    private final int left;
    private final int right;

    public Domino(int left, int right) {
        if (left < 0 || right < 0) {
            throw new IllegalArgumentException("must have positive # of dots");
        }
        if (left > 6 || right > 6) {
            throw new IllegalArgumentException("# of dots must be 6 or less");
        }
        this.left = left;
        this.right = right;
    }

    /**
     * Return the left side of this domino.
     */
    public int getLeft() {
        return this.left;
    }

    /**
     * Return the right side of this domino.
     */
    public int getRight() {
        return this.right;
    }

    /**
     * Return a flipped version of this domino.
     */
    public Domino flipped() {
        return new Domino(this.right, this.left);
    }

    /**
     * Return true if the domino is a "double" (same value on both sides).
     */
    public boolean isDouble() {
        return this.right == this.left;
    }

    /**
     * Two dominoes are equal when they have the same sides, flipped or not.
     */
    @Override
    public boolean equals(Object otherObj) {
        if (!(otherObj instanceof Domino)) {
            return false;
        }

        Domino other = (Domino)otherObj;
        return this.left == other.left && this.right == other.right ||
               this.left == other.right && this.right == other.left;
    }

    @Override
    public String toString() {
        return String.format("%d-%d", this.left, this.right);
    }

    public static void main(String[] args) {
        Domino oneFour = new Domino(1, 4);
        Domino fourOne = oneFour.flipped();     // i.e., new Domino(4, 1)

        System.out.println("1-4 equals 4-1? " + oneFour.equals(fourOne));

        // Two different objects in memory, so == gives us false
        System.out.println("1-4 == 4-1? " + (oneFour == fourOne));
    }
}
