java吧 关注:1,253,537贴子:12,739,908
  • 6回复贴,共1

【求助大神!!】求大神帮助2048编程

只看楼主收藏回复

完全初学者。。学校要求编2048啊T T虽然给了框架神马,但是我编的跑起来很有问题。。求大神知道怎么写往左往右移的那段。以下是弱渣编的。。问题百出。。求指导求改正T T谢谢大神!!!
public void play(int direction)
{
newSlot();
if (direction==LEFT_INPUT){
slidLeft();
}
else if(direction == DOWN_INPUT){
slidDown();
}
else if(direction == RIGHT_INPUT){
slidRight();
}
else if(direction == UP_INPUT){
slidUp();
}
gui.setGridByValue(grid);
// TO DO: implement the action to be taken after an arrow key of the
// specified direction is pressed.
// gui.setGridByIndex(grid);
}
public void slidLeft(){
for(int a=0; a < 3; a++){
for(int m = 0; m < ROW; m++){
for(int n = COL-1; n > 0; n--){
if(grid[m][n]!= EMPTY_SLOT && grid[m][n-1]==EMPTY_SLOT){
grid[m][n-1]=grid[m][n];
grid[m][n]=EMPTY_SLOT;
}
// else if(grid[m][n-2]==EMPTY_SLOT && grid[m][n-1]==EMPTY_SLOT && grid[m][n]!=EMPTY_SLOT){
// grid[m][n-2]=grid[m][n];
// grid[m][n]=EMPTY_SLOT;
// }
}
}
}
for(int m = 0; m < ROW; m++){
for(int n = 0; n < COL-1; n++){
if(grid[m][n]!=EMPTY_SLOT && grid[m][n+1]!=EMPTY_SLOT){
if(grid[m][n]== grid[m][n+1]){
grid[m][n]=grid[m][n+1]+grid[m][n];
grid[m][n+1]=grid[m][n+2];
grid[m][n+2]=grid[m][n+3];
}
// if(grid[m][n]!= EMPTY_SLOT && grid[m][n-1]==EMPTY_SLOT){
// grid[m][n-1]=grid[m][n];
// grid[m][n]=EMPTY_SLOT;
// }
}
// else if(grid[m][n]==EMPTY_SLOT && grid[m][n+1]==EMPTY_SLOT){
// grid[m][n]=grid[m][n+1];
// grid[m][n+1]=EMPTY_SLOT;
// }
}
}
}
public void slidDown(){
for(int a=0; a < 3; a++){
for(int n = 0; n < COL; n++){
for(int m = ROW-1; m > 0; m--){
if(grid[m][n]== EMPTY_SLOT && grid[m-1][n]!=EMPTY_SLOT){
grid[m][n]=grid[m-1][n];
grid[m-1][n]=EMPTY_SLOT;
}
}
}
}
for(int n = 0; n < COL; n++){
for(int m = ROW-1; m > 0; m--){
if(grid[m][n]!=EMPTY_SLOT && grid[m-1][n]!=EMPTY_SLOT){
if(grid[m][n]== grid[m-1][n]){
grid[m][n]=grid[m-1][n]+grid[m][n];
grid[m-1][n]=EMPTY_SLOT;
}
}
else if(grid[m][n]==EMPTY_SLOT && grid[m-1][n]==EMPTY_SLOT){
grid[m][n]=grid[m-1][n];
grid[m-1][n]=EMPTY_SLOT;
}
}
}
}
public void slidRight(){
for(int a=0; a < 3; a++){
for(int m = 0; m < ROW; m++){
for(int n = 0; n <COL-1; n++){
if(grid[m][n]!= EMPTY_SLOT && grid[m][n+1]==EMPTY_SLOT){
grid[m][n+1]=grid[m][n];
grid[m][n]=EMPTY_SLOT;
}
}
}
}
for(int m = 0; m < ROW; m++){
for(int n = COL-1; n > 0; n--){
if(grid[m][n]!=EMPTY_SLOT && grid[m][n-1]!=EMPTY_SLOT){
if(grid[m][n]== grid[m][n-1]){
grid[m][n]=grid[m][n-1]+grid[m][n];
grid[m][n-1]=EMPTY_SLOT;
}
}
else if(grid[m][n]==EMPTY_SLOT && grid[m][n-1]==EMPTY_SLOT){
grid[m][n]=grid[m][n-1];
grid[m][n-1]=EMPTY_SLOT;
}
}
}
}
public void slidUp(){
for(int a=0; a < 3; a++){
for(int n = 0; n < COL; n++){
for(int m = 1; m < ROW-1; m++){
if(grid[m][n]!= EMPTY_SLOT && grid[m-1][n]==EMPTY_SLOT){
grid[m-1][n]=grid[m][n];
grid[m][n]=EMPTY_SLOT;
}
}
}
}
for(int n = 0; n < COL; n++){
for(int m = 0; m < ROW-1; m++){
if(grid[m][n]!=EMPTY_SLOT && grid[m+1][n]!=EMPTY_SLOT){
if(grid[m][n]== grid[m+1][n]){
grid[m][n]=grid[m][n+1]+grid[m][n];
grid[m][n+1]=EMPTY_SLOT;
}
if(grid[m][n]==EMPTY_SLOT && grid[m][n+1]!=EMPTY_SLOT){
grid[m][n]=grid[m+1][n];
grid[m][n+1]=EMPTY_SLOT;
}
}
}
}
}
}


1楼2014-06-10 17:55回复
    2048不是开放源码的游戏吗?


    IP属地:辽宁来自Android客户端2楼2014-06-10 19:00
    收起回复
      我记得精贴有源码


      来自iPhone客户端3楼2014-06-11 00:39
      收起回复
        楼组,半年了,搞好了吗,求分享


        4楼2014-12-25 16:15
        回复