Android Open Source - blocks-game Linear Grid






From Project

Back to project page blocks-game.

License

The source code is released under:

Apache License

If you think the Android project blocks-game listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package cz.kotu.grids;
//  w ww  .j  av a  2 s.c  o m
import java.util.Iterator;
import java.util.Random;

/**
 * @author Kotuc
 *         Immutable
 */
public final class LinearGrid implements Iterable<LinPos> {

    final int sizeX;
    final int sizeY;

    public LinearGrid(int sizeX, int sizeY) {
        this.sizeX = sizeX;
        this.sizeY = sizeY;
    }

    public int getSizeX() {
        return sizeX;
    }

    public int getSizeY() {
        return sizeY;
    }

    public int getTotalNum() {

        return sizeX * sizeY;
    }

    public int index(int tilex, int tiley) {
        assertTileBounds(tilex, tiley);
        return (tilex + sizeX * tiley);
    }

    public int index(Pos pos) {
        return index(pos.x, pos.y);

    }

    void assertTileBounds(int tilex, int tiley) {

        if (isOutOfBounds(tilex, tiley)) {
            throw new ArrayIndexOutOfBoundsException("tile out of bounds " + tilex + ", " + tiley);
        }
    }

    public boolean isOutOfBounds(int tilex, int tiley) {
        return (tilex < 0 || sizeX <= tilex || tiley < 0 || sizeY <= tiley);
    }

    public boolean isOutOfBounds(Pos pos) {
        return isOutOfBounds(pos.x, pos.y);
    }


    public int getX(int index) {
        return index % sizeX;
    }

    public int getY(int index) {
        return index / sizeX;
    }

    public Iterator<LinPos> iterator() {
        return new Iterator<LinPos>() {

            int i = -1;

            public boolean hasNext() {
                return i < getTotalNum() - 1;
            }

            public LinPos next() {
                i++;
                return new LinPos(getX(i), getY(i), i);
            }

            public void remove() {
                throw new UnsupportedOperationException("Not supported.");
            }
        };
    }

    public Pos getPos(int index) {
        return new Pos(getX(index), getY(index));
    }

    public Pos randomPos(Random random) {
        return getPos(random.nextInt(getTotalNum()));
    }


}




Java Source Code List

com.badlogic.gradletest.DesktopLauncher.java
com.badlogicgames.gradletest.MainActivity.java
cz.kotu.game.blocks.BaseStage.java
cz.kotu.game.blocks.Block.java
cz.kotu.game.blocks.Draggable.java
cz.kotu.game.blocks.Follower.java
cz.kotu.game.blocks.GridStage.java
cz.kotu.game.blocks.GridUtils.java
cz.kotu.game.blocks.HelloApp.java
cz.kotu.game.blocks.MoveUtils.java
cz.kotu.game.blocks.Slider.java
cz.kotu.game.blocks.T.java
cz.kotu.game.blocks.hex.Axial.java
cz.kotu.game.blocks.hex.HexCoords3.java
cz.kotu.game.blocks.hex.HexGrid.java
cz.kotu.game.blocks.hex.HexGroup.java
cz.kotu.game.blocks.hex.HexPos.java
cz.kotu.game.blocks.hex.HexSet.java
cz.kotu.game.blocks.hex.HexStage.java
cz.kotu.game.blocks.hex.Hex.java
cz.kotu.grids.Dir.java
cz.kotu.grids.GenericGrid.java
cz.kotu.grids.LinPos.java
cz.kotu.grids.LinearGrid.java
cz.kotu.grids.Pos.java