Android Open Source - 2048GameAndroid Board






From Project

Back to project page 2048GameAndroid.

License

The source code is released under:

Apache License

If you think the Android project 2048GameAndroid 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 zhengxiao.myapplication.game;
/*www  .j  ava 2  s  . co  m*/
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengxiao on 12/15/14.
 */
public class Board {
    private List<Integer> values;
    private int rowCount;


    public Board(int rowCount) {
        this.rowCount = rowCount;
        values = newValues();
    }

    /**
     * ????
     *
     * @param rowCount
     * @param integers
     */
    protected Board(int rowCount, ArrayList<Integer> integers) {
        this(rowCount);
        values = integers;
    }

    private List<Integer> newValues() {
        List<Integer> values = new ArrayList<>(rowCount * rowCount);
        for (int i = 0; i < rowCount * rowCount; i++) {
            values.add(i, 0);
        }
        return values;
    }

    /**
     * ??????block???2
     *
     * @return ????
     */
    public boolean insertBlockRandomly() {
        Position nextPosition = findRandomEmptyPosition();
        if (nextPosition == null) {
            return false;
        } else {
            setValueAtPosition(nextPosition, 2);
            return true;
        }
    }

    @Nullable
    protected Position findRandomEmptyPosition() {
        List<Position> positions = listAllEmptyPositions();
        if (positions.isEmpty()) {
            return null;
        } else {
            return positions.get(pickRandomNumberIn(positions.size()));
        }
    }

    private int pickRandomNumberIn(int size) {
        return (int) (Math.random() * size);
    }

    protected List<Position> listAllEmptyPositions() {
        List<Position> positions = new ArrayList<>();
        for (int i = 0; i < rowCount * rowCount; i++) {
            Integer value = values.get(i);
            if (value == 0) {
                positions.add(getPositionFromIndex(i));
            }
        }
        return positions;
    }

    private void setValueAtPosition(@NonNull Position position, int value) {
        values.set(getIndexFromPosition(position), value);
    }

    private Integer getValueAtPosition(Position position) {
        return values.get(getIndexFromPosition(position));
    }

    //region ??????

    public int getColumnFromIndex(int index) {
        return index % rowCount;
    }

    public int getRowFromIndex(int index) {
        return index / rowCount;
    }

    public Position getPositionFromIndex(int index) {
        return new Position(getRowFromIndex(index), getColumnFromIndex(index));
    }

    public int getIndexFromPosition(@NonNull Position position) {
        return position.y * rowCount + position.x;
    }

    // endregion

    /**
     * ????
     *
     * @return
     */
    public List<Integer> getValues() {
        return values;
    }

    /**
     * ?????????????????????true
     *
     * @param direction
     * @return
     */
    public boolean moveInDirection(Direction direction) {
        boolean canMove = false;
        switch (direction) {
            case LEFT:
                canMove = moveLeft();
                break;
            case RIGHT:
                rotateBoardClockWise();
                rotateBoardClockWise();
                canMove = moveLeft();
                rotateBoardClockWise();
                rotateBoardClockWise();
                break;
            case UP:
                rotateBoardClockWise();
                rotateBoardClockWise();
                rotateBoardClockWise();
                canMove = moveLeft();
                rotateBoardClockWise();
                break;
            case DOWN:
                rotateBoardClockWise();
                canMove = moveLeft();
                rotateBoardClockWise();
                rotateBoardClockWise();
                rotateBoardClockWise();
                break;

        }
        return canMove;
    }

    protected synchronized void rotateBoardClockWise() {
        int[][] array = Utils.makeMatrixFrom(Utils.toArray(values), rowCount);
        Utils.rotateMatrix(array);
        values = Utils.toList(Utils.makeBoardFrom(array, rowCount));
    }

    private synchronized boolean moveLeft() {
        boolean canMove = false;
        // ?
        for (int i = 0; i < rowCount; i++) {
            // ?
            Position pos = new Position(0, i);
            Position firstNonZeroPosition = null;
            int firstNonZeroValue = -1;
            while (pos.x < rowCount) {
                int value = getValueAtPosition(pos);

                if (firstNonZeroPosition == null) {
                    // ?????????0???????
                    if (value != 0) {
                        // ?????0??????
                        firstNonZeroPosition = new Position(pos);
                        firstNonZeroValue = value;

                    }
                    pos.move(Direction.RIGHT);
                    continue;
                }

                // ???????0?????????????
                if (value == 0) {
                    // ??0
                    pos.move(Direction.RIGHT);
                    continue;
                } else if (value == firstNonZeroValue) {
                    // ??????????????????????????
                    setValueAtPosition(firstNonZeroPosition, 0);
                    setValueAtPosition(pos, firstNonZeroValue + value);
                    firstNonZeroPosition = null;
                    firstNonZeroValue = -1;
                } else {
                    // ?????????0??????????????????????? firstNonZeroPosition (??????)
                    // ???
                    firstNonZeroPosition = null;
                    firstNonZeroValue = -1;
                    continue;
                }

                pos.move(Direction.RIGHT);
            }
            // ???????????????????

            // ??????????????
            Position posTo = new Position(0, i);
            Position posFrom = new Position(1, i);
            while (posFrom.x > posTo.x && posFrom.x < rowCount) {
                // ??to?0???
                if (getValueAtPosition(posTo) != 0) {
                    posTo.move(Direction.RIGHT);
                    posFrom.move(Direction.RIGHT);
                    continue;
                }
                Integer valueFrom = getValueAtPosition(posFrom);
                if (valueFrom != 0) {
                    setValueAtPosition(posTo, valueFrom);
                    posTo.move(Direction.RIGHT);

                    setValueAtPosition(posFrom, 0);
                    // ???????????????????
                    canMove = true;
                }
                posFrom.move(Direction.RIGHT);
            }
        }
        return canMove;
    }


    @Override
    public String toString() {
        StringBuilder b = new StringBuilder("\n");
        int column = 0;
        for (Integer value : values) {
            if (column >= rowCount) {
                column = 0;
                b.append("\n");
            }
            b.append(value);
            b.append(", ");

            column++;
        }
        return b.toString();
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof Board) {
            Board that = (Board) o;
            return toString().equals(that.toString());
        }
        return super.equals(o);
    }

    /**
     * ???
     */
    protected void setValues(List<Integer> newValues) {
        this.values = newValues;
    }
}




Java Source Code List

zhengxiao.myapplication.ApplicationTest.java
zhengxiao.myapplication.GameActivity.java
zhengxiao.myapplication.MainActivity.java
zhengxiao.myapplication.game.BoardTest.java
zhengxiao.myapplication.game.Board.java
zhengxiao.myapplication.game.Direction.java
zhengxiao.myapplication.game.GameTest.java
zhengxiao.myapplication.game.Game.java
zhengxiao.myapplication.game.Position.java
zhengxiao.myapplication.game.Utils.java
zhengxiao.myapplication.utils.OnGestureListenerAdapter.java