完全初学者。。学校要求编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;
}
}
}
}
}
}
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;
}
}
}
}
}
}