Back to project page 2048GameAndroid.
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.
package zhengxiao.myapplication.game; /*from www . j ava2 s. co m*/ import android.support.annotation.NonNull; /** * Created by zhengxiao on 12/15/14. */ public class Game { private final Callback activity; public Board board = new Board(ROW_COUNT); public final static int ROW_COUNT = 4; public Game(Callback activity) { this.activity = activity; setUpGame(); } public static interface Callback{ void render(@NonNull Board board); void gameOver(int finalScore); } private boolean setUpGame() { board.insertBlockRandomly(); board.insertBlockRandomly(); notifyRenderer(); return true; } /** * ??Activity???????? */ private void notifyRenderer() { activity.render(board); } private void notifyGameOver() { activity.gameOver(0); } /** * ???????????? * @return ?????? */ public boolean moveInDirection(Direction direction){ boolean canMove = board.moveInDirection(direction); if(canMove) { populateOnceAtEmptyBlock(); } return canMove; } /** * ????????????????????????????? GameOver???????Activity ?? * @return ????? */ private void populateOnceAtEmptyBlock() { boolean hasNextPosition = board.insertBlockRandomly(); if(!hasNextPosition){ notifyGameOver(); }else{ notifyRenderer(); } } }