package rubys.ninja.experiments.zahlenpuzzle.board;

import rubys.ninja.experiments.zahlenpuzzle.token.Token;

import java.util.List;

public class Board {
    private BoardSize size;
    private List<Token> tokens;

    public Board(BoardInitializer initializer) {
        this.size = initializer.getBoardSize();
        this.tokens = initializer.getTokens();
    }

    public Token getTokenAt(BoardSize location) throws Exception {
        int index = getIndexFromLocation(location);

        return tokens.get(index);
    }

    public BoardSize getSize() {
        return size;
    }

    public void swapTokens(BoardSize pos1, BoardSize pos2) throws Exception {
        Token token1 = getTokenAt(pos1);
        Token token2 = getTokenAt(pos2);

        int index1 = getIndexFromLocation(pos1);
        int index2 = getIndexFromLocation(pos2);

        tokens.set(index1, token2);
        tokens.set(index2, token1);
    }

    private int getIndexFromLocation(BoardSize location) {
        return location.getHeight() * size.getWidth() + location.getWidth();
    }
}

@@ -1063,11 +1063,660 @@
 );%0A    %7D
+%0A%0A    public boolean areAdjacent(BoardSize pos1, BoardSize pos2) %7B%0A        //     0  1   2   3%0A        //   %7C-------------%0A        // 0 %7C 0,  1,  2,  3%0A        // 1 %7C 4,  5,  6,  7%0A        // 2 %7C 8,  9,  10, 11%0A        // 3 %7C 12, 13, 14, 15%0A%0A        int width1 = pos1.getWidth();%0A        int width2 = pos2.getWidth();%0A%0A        int height1 = pos1.getHeight();%0A        int height2 = pos2.getHeight();%0A%0A        if (width1 == width2) %7B%0A            return height1 == height2 + 1 %7C%7C height2 == height1 + 1;%0A        %7D%0A%0A        if (height1 == height2) %7B%0A            return width1 == width2 + 1 %7C%7C width2 == width1 + 1;%0A        %7D%0A%0A        return false;%0A    %7D
 %0A%7D%0A

Implement areAdjacent