# 移动与合并的算法

``````public class Number {
public int mScores;
public int mCurPosition;
public int mBeforePosition;
public boolean isNeedMove;
public boolean isNeedCombine;
public Number(int position,int scores){
mScores = scores;
mCurPosition = mBeforePosition = position;
isNeedMove = false;
isNeedCombine = false;
}
public void reset(){
mScores = 0;
isNeedMove = false;
isNeedCombine = false;
}
}``````

``````public class Numbers {
Number [][] mNumbers = new Number[Game2048StaticControl.gamePlayMode][Game2048StaticControl.gamePlayMode];
public Numbers(){
for(int i=0;i<Game2048StaticControl.gamePlayMode;i++){
for(int j=0;j<Game2048StaticControl.gamePlayMode;j++){
mNumbers[i][j] = new Number(0,0);
}
}
}
public Number getNumber(int x,int y){
return mNumbers[x][y];
}
public Number [][] getNumbers (){
return mNumbers;
}
public int getBlankCount(){
int count = 0;
for(int i=0;i<Game2048StaticControl.gamePlayMode;i++){
for(int j=0;j<Game2048StaticControl.gamePlayMode;j++){
if(mNumbers[i][j].mScores==0){
count++;
}
}
}
return count;
}
public int getPositonFromBlankCountTh(int blankTh){
int count = 0;
for(int i=0;i<Game2048StaticControl.gamePlayMode;i++){
for(int j=0;j<Game2048StaticControl.gamePlayMode;j++){
if(mNumbers[i][j].mScores==0){
if(count==blankTh){
return i*Game2048StaticControl.gamePlayMode+j;
}else {
count++;
}
}
}
}
return -1;
}
public void swapNumber(int position1,int position2){
mNumbers[position1/Game2048StaticControl.gamePlayMode][position1%Game2048StaticControl.gamePlayMode].mCurPosition = position2;
mNumbers[position1/Game2048StaticControl.gamePlayMode][position1%Game2048StaticControl.gamePlayMode].mBeforePosition = position1;
mNumbers[position2/Game2048StaticControl.gamePlayMode][position2%Game2048StaticControl.gamePlayMode].mCurPosition = position1;
mNumbers[position2/Game2048StaticControl.gamePlayMode][position2%Game2048StaticControl.gamePlayMode].mBeforePosition = position2;
Number tem = mNumbers[position1/Game2048StaticControl.gamePlayMode][position1%Game2048StaticControl.gamePlayMode];
mNumbers[position1/Game2048StaticControl.gamePlayMode][position1%Game2048StaticControl.gamePlayMode] = mNumbers[position2/Game2048StaticControl.gamePlayMode][position2%Game2048StaticControl.gamePlayMode];
mNumbers[position2/Game2048StaticControl.gamePlayMode][position2%Game2048StaticControl.gamePlayMode] = tem;
}
}``````

``````   //return 0:do nothing
//return 1:move
//return 2:combine
public int leftKeyDealAlgorithm(){
int i, j, k;
boolean isMoved = false;
boolean isFinalMove = false;
boolean isFinalCombie = false;
for(i=0;i<Game2048StaticControl.gamePlayMode;i++){
j=k=0;
isMoved = false;
while (true) {
while (j<Game2048StaticControl.gamePlayMode && !isPosionHasNumber(i,j))
j++;
if (j > Game2048StaticControl.gamePlayMode-1)
break;
if (j > k){
isMoved = true;
isFinalMove = true;
Number number = getNumber(i,j);
number.isNeedMove = true;
number.isNeedCombine = false;
swapNumber(i*Game2048StaticControl.gamePlayMode+k,i*Game2048StaticControl.gamePlayMode+j);
}
if (k > 0 && getNumber(i,k).mScores==getNumber(i,k-1).mScores && !getNumber(i,k-1).isNeedCombine){
isFinalCombie = true;
Number numberk = getNumber(i,k);
Number numberkl = getNumber(i,k-1);
if(isMoved){
numberkl.mBeforePosition = numberk.mBeforePosition;
}else {
numberkl.mBeforePosition =  i*Game2048StaticControl.gamePlayMode+k;
}
numberkl.mCurPosition = i*Game2048StaticControl.gamePlayMode+k-1;
numberkl.isNeedMove = true;
numberkl.isNeedCombine = true;
numberkl.mScores <<=1;
updateCurScoresAndHistoryScores(numberkl.mScores);
numberk.reset();
numberk.mCurPosition = numberk.mBeforePosition = i*Game2048StaticControl.gamePlayMode+k;
} else{
k++;
}
j++;
}
}
return isFinalCombie?2:(isFinalMove?1:0);
}
``````

``````                while (j<Game2048StaticControl.gamePlayMode && !isPosionHasNumber(i,j))
j++;``````

``if (k > 0 && getNumber(i,k).mScores==getNumber(i,k-1).mScores && !getNumber(i,k-1).isNeedCombine``

``````   public int rightKeyDealAlgorithm(){
int i, j, k;
boolean isMoved = false;
boolean isFinalMove = false;
boolean isFinalCombie = false;
for(i=0;i<Game2048StaticControl.gamePlayMode;i++){
j=k=Game2048StaticControl.gamePlayMode-1;
isMoved = false;
while (true) {
while (j>-1 && !isPosionHasNumber(i,j))
j--;
if (j < 0)
break;
if (j < k){
isMoved = true;
isFinalMove = true;
Number number = getNumber(i,j);
number.isNeedMove = true;
number.isNeedCombine = false;
swapNumber(i*Game2048StaticControl.gamePlayMode+k,i*Game2048StaticControl.gamePlayMode+j);
}
if (k < Game2048StaticControl.gamePlayMode-1 && getNumber(i,k).mScores==getNumber(i,k+1).mScores && !getNumber(i,k+1).isNeedCombine){
isFinalCombie = true;
Number numberk = getNumber(i,k);
Number numberkl = getNumber(i,k+1);
if(isMoved){
numberkl.mBeforePosition = numberk.mBeforePosition;
}else {
numberkl.mBeforePosition =  i*Game2048StaticControl.gamePlayMode+k;
}
numberkl.mCurPosition = i*Game2048StaticControl.gamePlayMode+k+1;
numberkl.isNeedMove = true;
numberkl.isNeedCombine = true;
numberkl.mScores <<=1;
updateCurScoresAndHistoryScores(numberkl.mScores);
numberk.reset();
numberk.mCurPosition = numberk.mBeforePosition = i*Game2048StaticControl.gamePlayMode+k;
} else{
k--;
}
j--;
}
}
return isFinalCombie?2:(isFinalMove?1:0);
}
public int upKeyDealAlgorithm(){
int i, j, k;
boolean isMoved = false;
boolean isFinalMove = false;
boolean isFinalCombie = false;
for(i=0;i<Game2048StaticControl.gamePlayMode;i++){
j=k=0;
isMoved = false;
while (true) {
while (j<Game2048StaticControl.gamePlayMode && !isPosionHasNumber(j,i))
j++;
if (j > Game2048StaticControl.gamePlayMode-1)
break;
if (j > k){
isMoved = true;
isFinalMove = true;
Number number = getNumber(j,i);
number.isNeedMove = true;
number.isNeedCombine = false;
swapNumber(k*Game2048StaticControl.gamePlayMode+i,j*Game2048StaticControl.gamePlayMode+i);
}
if (k > 0 && getNumber(k,i).mScores==getNumber(k-1,i).mScores && !getNumber(k-1,i).isNeedCombine){
isFinalCombie = true;
Number numberk = getNumber(k,i);
Number numberkl = getNumber(k-1,i);
if(isMoved){
numberkl.mBeforePosition = numberk.mBeforePosition;
}else {
numberkl.mBeforePosition =  k*Game2048StaticControl.gamePlayMode+i;
}
numberkl.mCurPosition = (k-1)*Game2048StaticControl.gamePlayMode+i;
numberkl.isNeedMove = true;
numberkl.isNeedCombine = true;
numberkl.mScores <<=1;
updateCurScoresAndHistoryScores(numberkl.mScores);
numberk.reset();
numberk.mCurPosition = numberk.mBeforePosition = k*Game2048StaticControl.gamePlayMode+i;
} else{
k++;
}
j++;
}
}
return isFinalCombie?2:(isFinalMove?1:0);
}
public int downKeyDealAlgorithm(){
int i, j, k;
boolean isMoved = false;
boolean isFinalMove = false;
boolean isFinalCombie = false;
for(i=0;i<Game2048StaticControl.gamePlayMode;i++){
j=k=Game2048StaticControl.gamePlayMode-1;
isMoved = false;
while (true) {
while (j>-1 && !isPosionHasNumber(j,i))
j--;
if (j < 0)
break;
if (j < k){
isMoved = true;
isFinalMove = true;
Number number = getNumber(j,i);
number.isNeedMove = true;
number.isNeedCombine = false;
swapNumber(k*Game2048StaticControl.gamePlayMode+i,j*Game2048StaticControl.gamePlayMode+i);
}
if (k < Game2048StaticControl.gamePlayMode-1 && getNumber(k,i).mScores==getNumber(k+1,i).mScores && !getNumber(k+1,i).isNeedCombine){
isFinalCombie = true;
Number numberk = getNumber(k,i);
Number numberkl = getNumber(k+1,i);
if(isMoved){
numberkl.mBeforePosition = numberk.mBeforePosition;
}else {
numberkl.mBeforePosition =  k*Game2048StaticControl.gamePlayMode+i;
}
numberkl.mCurPosition = (k+1)*Game2048StaticControl.gamePlayMode+i;
numberkl.isNeedMove = true;
numberkl.isNeedCombine = true;
numberkl.mScores <<=1;
updateCurScoresAndHistoryScores(numberkl.mScores);
numberk.reset();
numberk.mCurPosition = numberk.mBeforePosition = k*Game2048StaticControl.gamePlayMode+i;
} else{
k--;
}
j--;
}
}
return isFinalCombie?``````2:(isFinalMove?1:0);
}``````

# 动画

``````                    case Game2048StaticControl.DIRECT_LEFT:{
mNumberQueue.pushItem(mGAM.getmNumbers());
int ret =  mGAM.leftKeyDealAlgorithm();
if (ret>0){
startAnimation(mHolder,mPaint,Game2048StaticControl.DIRECT_LEFT);
mGAM.updateNumbers();
doDrawGameSurface();
sendEmptyMessage(Game2048StaticControl.GENERATE_NUMBER);
playSoundEffect(ret);
}
break;
}``````

mGAM.leftKeyDealAlgorithm();

startAnimation(mHolder,mPaint,Game2048StaticControl.DIRECT_LEFT);

sendEmptyMessage(Game2048StaticControl.GENERATE_NUMBER);

startAnimation定义例如以下：

``````    public void startAnimation(SurfaceHolder holder,Paint paint,int direct){
int count = 0;
RectF rectF = new RectF();
while (count++<Game2048StaticControl.ANIMATION_MOVE_STEP) {
Canvas canvas = holder.lockCanvas();
mDrawTools.initSurfaceBg(canvas, paint);
mDrawTools.drawSurfaceMap(canvas, paint);
mDrawTools.drawSurfaceMapAndNumbersWhoIsNeedCombine(canvas,paint);
for (int i = 0; i < Game2048StaticControl.gamePlayMode; i++) {
for (int j = 0; j < Game2048StaticControl.gamePlayMode; j++) {
mGAM.aniInsertValue(i, j, count, Game2048StaticControl.ANIMATION_MOVE_STEP,direct,rectF);
if(rectF != null && mGAM.isPosionHasNumber(i,j) && mGAM.getNumber(i,j).isNeedMove){
mDrawTools.drawNumberByRectF(i,j,canvas,paint,rectF);
}
}
}
holder.unlockCanvasAndPost(canvas);
}
}``````

``````  public void aniInsertValue(int x,int y,int count,int insertCount,int direct,RectF rectF){
Number number = getNumber(x,y);
if(number.mCurPosition ==  number.mBeforePosition){
return;
}
[number.mCurPosition%Game2048StaticControl.gamePlayMode].left
[number.mBeforePosition%Game2048StaticControl.gamePlayMode].left;
[number.mCurPosition%Game2048StaticControl.gamePlayMode].top
[number.mBeforePosition%Game2048StaticControl.gamePlayMode].top;
xDiffPixels = Math.abs(xDiffPixels);
yDiffPixels = Math.abs(yDiffPixels);
float xStep = xDiffPixels/insertCount;
float yStep = yDiffPixels/insertCount;
[number.mCurPosition%Game2048StaticControl.gamePlayMode].left;
[number.mCurPosition%Game2048StaticControl.gamePlayMode].top;;
switch (direct){
case DIRECT_UP:{
[number.mBeforePosition%Game2048StaticControl.gamePlayMode].top
- yStep*count;
break;
}
case DIRECT_DOWN:{
[number.mBeforePosition%Game2048StaticControl.gamePlayMode].top
+ yStep*count;
break;
}
case DIRECT_LEFT:{
[number.mBeforePosition%Game2048StaticControl.gamePlayMode].left
- xStep*count;
break;
}
case DIRECT_RIGHT:{
[number.mBeforePosition%Game2048StaticControl.gamePlayMode].left
+ xStep*count;
break;
}
default:break;
}
}``````

# 在随机位置随机生成2或者4

``````    public int getBlankCount(){
return mNumbers.getBlankCount();
}``````

``````    public int getBlankCount(){
int count = 0;
for(int i=0;i<Game2048StaticControl.gamePlayMode;i++){
for(int j=0;j<Game2048StaticControl.gamePlayMode;j++){
if(mNumbers[i][j].mScores==0){
count++;
}
}
}
return count;
}``````

``````    public int  setOneRandomNumberInRandomPosition(){
int scores = Game2048Algorithm.getRandom2Or4();
int blankCount = getBlankCount();
Log.d(TAG,"blankCount:"+blankCount);
int blankTh = 0;
if(blankCount<=0){
return -1;
}else{
blankTh = Game2048Algorithm.getRandomPosition(blankCount);
}
int position = mNumbers.getPositonFromBlankCountTh(blankTh);
if (position<0){
Log.d(TAG,"getPositonFromBlankCountTh return error");
return -1;
}
Number num = mNumbers.getNumber(position/Game2048StaticControl.gamePlayMode,position%Game2048StaticControl.gamePlayMode);
num.mScores = scores;
num.mBeforePosition = num.mCurPosition = position;
num.isNeedCombine = num.isNeedMove = false;
return position;
}``````

# 检測游戏失败

``````   public boolean checkGameOver(){
Log.d(TAG,"checkGameOver");
for (int i = 0; i < Game2048StaticControl.gamePlayMode; i++)
{
for (int j = 0; j < Game2048StaticControl.gamePlayMode; j++)
{
if (j != Game2048StaticControl.gamePlayMode-1 && getNumber(i,j).mScores == getNumber(i,j+1).mScores)
return false;
if (i != Game2048StaticControl.gamePlayMode-1 && getNumber(i,j).mScores == getNumber(i+1,j).mScores)
return false;
}
}
if (mListener!=null){
mListener.onGameOver();
}
return true;
}``````

2048游戏回想二：算法总结（移动、合并、动画等）

(0)
(0)

0条