俄罗斯方块的model需要用到的类的详细解说

首先俄罗斯方块中model的大部分代码都被封装到了jimu包中,所以现在开始把jimu包中的类都发表出来:

首先是各个方块的父类:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jimu;

/**
 *
 * @author Administrator
 */
public interface Jimu {
    final String WHITE= "□";
    final String BLACK= "■";
    public boolean goDown();
    public boolean goLeft();
    public boolean goRight();
    public boolean change();
    public int getY();
}
然后是对应的实现的各个子类,在这里我就只是解释了一下反着的L的源代码,其他代码和它一样,只是if判断的值是根据各个积木的形状有所不同的。

下面是反着的L的代码:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jimu;

import java.util.Random;

/**
 *
 * @author Administrator
 */
public class FL implements Jimu {

    Random r = new Random();
    String[][] map;
    private int fx = 0;
    int x = r.nextInt(21) + 5;      //随机选取掉落的位置
    int y = 4;              //y就始终等于4,这样可以保证上面不越界

    public FL(String[][] map) {
        //这里是对该方块进行初始化
        this.map = map;
        setFL(x, y, fx);
    }

    private void setFL(int x, int y, int fx) {
        //通过方向,进行不同的方块的设置
        switch (fx) {
            case 0:
                map[x][y - 1] = BLACK;
                map[x][y] = BLACK;
                map[x + 1][y] = BLACK;
                map[x + 2][y] = BLACK;
                break;
            case 1:
                map[x][y - 2] = BLACK;
                map[x][y - 1] = BLACK;
                map[x - 1][y] = BLACK;
                map[x][y] = BLACK;
                break;
            case 2:
                map[x - 2][y] = BLACK;
                map[x - 1][y] = BLACK;
                map[x][y] = BLACK;
                map[x][y + 1] = BLACK;
                break;
            case 3:
                map[x][y] = BLACK;
                map[x + 1][y] = BLACK;
                map[x][y + 1] = BLACK;
                map[x][y + 2] = BLACK;
                break;
            default:
                System.out.println();
        }
    }

    private void clearFL(int x, int y, int fx) {
        //通过方向来的转变来进行对之前的方块进行清除
        switch (fx) {
            case 0:
                map[x][y - 1] = WHITE;
                map[x][y] = WHITE;
                map[x + 1][y] = WHITE;
                map[x + 2][y] = WHITE;
                break;
            case 1:
                map[x][y - 2] = WHITE;
                map[x][y - 1] = WHITE;
                map[x - 1][y] = WHITE;
                map[x][y] = WHITE;
                break;
            case 2:
                map[x - 2][y] = WHITE;
                map[x - 1][y] = WHITE;
                map[x][y] = WHITE;
                map[x][y + 1] = WHITE;
                break;
            case 3:
                map[x][y] = WHITE;
                map[x + 1][y] = WHITE;
                map[x][y + 1] = WHITE;
                map[x][y + 2] = WHITE;
                break;
            default:
                System.out.println();
        }
    }

    @Override
    public boolean goDown() {
        //向下走,获取调用方法返回的值,并返回给model
        switch (fx) {
            case 0:
                return downOne();
            case 1:
                return downTwo();
            case 2:
                return downThree();
            case 3:
                return downFour();
        }
        return false;
    }

    @Override
    public boolean goLeft() {
        //向左走
        switch (fx) {
            case 0:
                return leftOne();
            case 1:
                return leftTwo();
            case 2:
                return leftThree();
            case 3:
                return leftFour();
        }
        return false;
    }

    @Override
    public boolean goRight() {
        //向右走
        switch (fx) {
            case 0:
                return rightOne();
            case 1:
                return rightTwo();
            case 2:
                return rightThree();
            case 3:
                return rightFour();
        }
        return false;
    }

    @Override
    public boolean change() {
        //改变方向
        switch (fx) {
            case 0:
                return changeOne();
            case 1:
                return changeTwo();
            case 2:
                return changeThree();
            case 3:
                return changeFour();
        }
        return false;
    }

    @Override
    public int getY() {
       //用来返回当前行,判断游戏是否结束
        return y;
    }

    //下面就是每个积木每个方向的向下,向左,向右,和变形的方法
    //只有符合if里面的条件才可以进行走动,否则就没法动,游戏就结束了
    
    private boolean downOne() {
        //通过上面的方法判断来调用向下的方法,四个方向,有四种方法
        if (!(map[x][y + 1].equals(BLACK) || map[x + 1][y + 1].equals(BLACK) || map[x + 2][y + 1].equals(BLACK))) {
            //条件符合就先清除,再生成,然后返回true给向下走的方法
            clearFL(x, y, fx);
            setFL(x, ++y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean downTwo() {
        if (!(map[x - 1][y + 1].equals(BLACK) || map[x][y + 1].equals(BLACK))) {
            clearFL(x, y, fx);
            setFL(x, ++y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean downThree() {
        if (!(map[x - 2][y + 1].equals(BLACK) || map[x - 1][y + 1].equals(BLACK) || map[x][y + 2].equals(BLACK))) {
            clearFL(x, y, fx);
            setFL(x, ++y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean downFour() {
        if (!(map[x][y + 3].equals(BLACK) || map[x + 1][y + 1].equals(BLACK))) {
            clearFL(x, y, fx);
            setFL(x, ++y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean leftOne() {
        if (!(map[x - 1][y - 1].equals(BLACK) || map[x - 1][y].equals(BLACK))) {
            clearFL(x, y, fx);
            setFL(--x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean leftTwo() {
        if (!(map[x - 1][y - 2].equals(BLACK) || map[x - 1][y - 1].equals(BLACK) || map[x - 2][y].equals(BLACK))) {
            clearFL(x, y, fx);
            setFL(--x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean leftThree() {
        if (!(map[x - 3][y].equals(BLACK) || map[x - 1][y + 1].equals(BLACK))) {
            clearFL(x, y, fx);
            setFL(--x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean leftFour() {
        if (!(map[x - 1][y].equals(BLACK) || map[x - 1][y + 1].equals(BLACK) || map[x - 1][y + 2].equals(BLACK))) {
            clearFL(x, y, fx);
            setFL(--x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean rightOne() {
        if (!(map[x + 1][y - 1].equals(BLACK) || map[x + 3][y].equals(BLACK))) {
            clearFL(x, y, fx);
            setFL(++x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean rightTwo() {
        if (!(map[x + 1][y - 2].equals(BLACK) || map[x + 1][y - 1].equals(BLACK) || map[x + 1][y].equals(BLACK))) {
            clearFL(x, y, fx);
            setFL(++x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean rightThree() {
        if (!(map[x + 1][y].equals(BLACK) || map[x + 1][y + 1].equals(BLACK))) {
            clearFL(x, y, fx);
            setFL(++x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean rightFour() {
        if (!(map[x + 2][y].equals(BLACK) || map[x + 1][y + 1].equals(BLACK) || map[x + 1][y + 2].equals(BLACK))) {
            clearFL(x, y, fx);
            setFL(++x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean changeOne() {
        if (!(map[x][y - 2].equals(BLACK) || map[x + 1][y - 2].equals(BLACK) || map[x + 2][y - 2].equals(BLACK) ||
                map[x - 1][y - 1].equals(BLACK) || map[x + 1][y - 1].equals(BLACK) || map[x + 2][y - 1].equals(BLACK) ||
                map[x - 1][y].equals(BLACK))) {
            clearFL(x, y, fx);
            setFL(x, y, ++fx);
            System.out.println("1:       " + fx);
            return true;
        }
        return false;
    }

    private boolean changeTwo() {
        if (!(map[x - 2][y - 2].equals(BLACK) || map[x - 1][y - 2].equals(BLACK) || map[x - 2][y - 1].equals(BLACK) ||
                map[x - 1][y - 1].equals(BLACK) || map[x - 2][y].equals(BLACK) || map[x - 1][y + 1].equals(BLACK) ||
                map[x][y + 1].equals(BLACK))) {
            clearFL(x, y, fx);
            setFL(x, y, ++fx);
            System.out.println("2:       " + fx);
            return true;
        }
        return false;
    }

    private boolean changeThree() {
        if (!(map[x + 1][y].equals(BLACK) || map[x - 2][y + 1].equals(BLACK) || map[x - 1][y + 1].equals(BLACK) ||
                map[x + 1][y + 1].equals(BLACK) || map[x - 2][y + 2].equals(BLACK) || map[x - 1][y + 2].equals(BLACK) ||
                map[x][y + 2].equals(BLACK))) {
            clearFL(x, y, fx);
            setFL(x, y, ++fx);
            System.out.println("3:       " + fx);
            return true;
        }
        return false;
    }

    private boolean changeFour() {
        if (!(map[x][y - 1].equals(BLACK) || map[x + 1][y - 1].equals(BLACK) || map[x + 2][y].equals(BLACK) ||
                map[x + 1][y + 1].equals(BLACK) || map[x + 2][y + 1].equals(BLACK) || map[x + 1][y + 2].equals(BLACK) ||
                map[x + 2][y + 2].equals(BLACK))) {
            clearFL(x, y, fx);
            if ((++fx) > 2) {
                fx = 0;
            }
            setFL(x, y, fx);
            System.out.println("4:       " + fx);
            return true;
        }
        return false;
    }
}
下面是反着的Z的代码:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jimu;

import java.util.Random;

/**
 *
 * @author Administrator
 */
public class FZ implements Jimu {

    Random r = new Random();
    String[][] map;
    private int fx = 0;
    int x = r.nextInt(21) + 5;
    int y = 4;

    public FZ(String[][] map) {
        this.map = map;
        setFZ(x, y, fx);
    }

    private void setFZ(int x, int y, int fx) {
        switch (fx) {
            case 0:
                map[x][y - 1] = BLACK;
                map[x + 1][y - 1] = BLACK;
                map[x - 1][y] = BLACK;
                map[x][y] = BLACK;
                break;
            case 1:
                map[x - 1][y - 1] = BLACK;
                map[x - 1][y] = BLACK;
                map[x][y] = BLACK;
                map[x][y + 1] = BLACK;
                break;
            case 2:
                map[x][y] = BLACK;
                map[x + 1][y] = BLACK;
                map[x - 1][y + 1] = BLACK;
                map[x][y + 1] = BLACK;
                break;
            case 3:
                map[x][y - 1] = BLACK;
                map[x][y] = BLACK;
                map[x + 1][y] = BLACK;
                map[x + 1][y + 1] = BLACK;
        }
    }

    public void clearFZ(int x, int y, int fx) {
        switch (fx) {
            case 0:
                map[x][y - 1] = WHITE;
                map[x + 1][y - 1] = WHITE;
                map[x - 1][y] = WHITE;
                map[x][y] = WHITE;
                break;
            case 1:
                map[x - 1][y - 1] = WHITE;
                map[x - 1][y] = WHITE;
                map[x][y] = WHITE;
                map[x][y + 1] = WHITE;
                break;
            case 2:
                map[x][y] = WHITE;
                map[x + 1][y] = WHITE;
                map[x - 1][y + 1] = WHITE;
                map[x][y + 1] = WHITE;
                break;
            case 3:
                map[x][y - 1] = WHITE;
                map[x][y] = WHITE;
                map[x + 1][y] = WHITE;
                map[x + 1][y + 1] = WHITE;
        }
    }

    public boolean goDown() {
        switch (fx) {
            case 0:
                return downOne();
            case 1:
                return downTwo();
            case 2:
                return downThree();
            case 3:
                return downFour();
        }
        return false;
    }

    public boolean goLeft() {
        switch (fx) {
            case 0:
                return leftOne();
            case 1:
                return leftTwo();
            case 2:
                return leftThree();
            case 3:
                return leftFour();
        }
        return false;
    }

    public boolean goRight() {
        switch (fx) {
            case 0:
                return rightOne();
            case 1:
                return rightTwo();
            case 2:
                return rightThree();
            case 3:
                return rightFour();
        }
        return false;
    }

    public boolean change() {
        switch (fx) {
            case 0:
                return changeOne();
            case 1:
                return changeTwo();
            case 2:
                return changeThree();
            case 3:
                return changeFour();
        }
        return false;
    }

    public int getY() {

        return y;

    }

    private boolean downOne() {
        if (!(map[x - 1][y + 1].equals(BLACK) || map[x][y + 1].equals(BLACK) || map[x + 1][y].equals(BLACK))) {
            clearFZ(x, y, fx);
            setFZ(x, ++y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean downTwo() {
        if (!(map[x - 1][y + 1].equals(BLACK) || map[x][y + 2].equals(BLACK))) {
            clearFZ(x, y, fx);
            setFZ(x, ++y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean downThree() {
        if (!(map[x - 1][y + 2].equals(BLACK) || map[x][y + 2].equals(BLACK) || map[x + 1][y + 1].equals(BLACK))) {
            clearFZ(x, y, fx);
            setFZ(x, ++y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean downFour() {
        if (!(map[x][y + 1].equals(BLACK) || map[x + 1][y + 2].equals(BLACK))) {
            clearFZ(x, y, fx);
            setFZ(x, ++y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean leftOne() {
        if (!(map[x - 1][y - 1].equals(BLACK) || map[x - 2][y].equals(BLACK))) {
            clearFZ(x, y, fx);
            setFZ(--x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean leftTwo() {
        if (!(map[x - 2][y - 1].equals(BLACK) || map[x - 2][y].equals(BLACK) || map[x - 1][y + 1].equals(BLACK))) {
            clearFZ(x, y, fx);
            setFZ(--x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean leftThree() {
        if (!(map[x - 1][y].equals(BLACK) || map[x - 2][y + 1].equals(BLACK))) {
            clearFZ(x, y, fx);
            setFZ(--x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean leftFour() {
        if (!(map[x - 1][y - 1].equals(BLACK) || map[x - 1][y].equals(BLACK) || map[x][y + 1].equals(BLACK))) {
            clearFZ(x, y, fx);
            setFZ(--x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean rightOne() {
        if (!(map[x + 2][y - 1].equals(BLACK) || map[x + 1][y].equals(BLACK))) {
            clearFZ(x, y, fx);
            setFZ(++x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean rightTwo() {
        if (!(map[x][y - 1].equals(BLACK) || map[x + 1][y].equals(BLACK) || map[x + 1][y + 1].equals(BLACK))) {
            clearFZ(x, y, fx);
            setFZ(++x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean rightThree() {
        if (!(map[x + 2][y].equals(BLACK) || map[x + 1][y + 1].equals(BLACK))) {
            clearFZ(x, y, fx);
            setFZ(++x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean rightFour() {
        if (!(map[x + 1][y - 1].equals(BLACK) || map[x + 2][y].equals(BLACK) || map[x + 2][y + 1].equals(BLACK))) {
            clearFZ(x, y, fx);
            setFZ(++x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean changeOne() {
        if (!(map[x - 1][y - 2].equals(BLACK) || map[x][y - 2].equals(BLACK) || map[x + 1][y - 2].equals(BLACK) ||
                map[x - 1][y - 1].equals(BLACK) || map[x - 1][y + 1].equals(BLACK) || map[x][y + 1].equals(BLACK))) {
            clearFZ(x, y, fx);
            setFZ(x, y, ++fx);
            System.out.println("1:       " + fx);
            return true;
        }
        return false;
    }

    private boolean changeTwo() {
        if (!(map[x - 2][y - 1].equals(BLACK) || map[x - 2][y].equals(BLACK) || map[x + 1][y].equals(BLACK) ||
                map[x - 2][y + 1].equals(BLACK) || map[x - 1][y + 1].equals(BLACK) || map[x + 1][y + 1].equals(BLACK))) {
            clearFZ(x, y, fx);
            setFZ(x, y, ++fx);
            System.out.println("2:       " + fx);
            return true;
        }
        return false;
    }

    private boolean changeThree() {
        if (!(map[x][y - 1].equals(BLACK) || map[x + 1][y - 1].equals(BLACK) || map[x + 1][y + 1].equals(BLACK) ||
                map[x - 1][y + 2].equals(BLACK) || map[x][y + 2].equals(BLACK) || map[x + 1][y + 2].equals(BLACK))) {
            clearFZ(x, y, fx);
            setFZ(x, y, ++fx);
            System.out.println("3:       " + fx);
            return true;
        }
        return false;
    }

    private boolean changeFour() {
        if (!(map[x - 1][y - 1].equals(BLACK) || map[x + 1][y - 1].equals(BLACK) || map[x + 2][y - 1].equals(BLACK) ||
                map[x - 1][y].equals(BLACK) || map[x + 2][y].equals(BLACK) || map[x + 2][y + 1].equals(BLACK))) {
            clearFZ(x, y, fx);
            if ((++fx) > 2) {
                fx = 0;
            }
            setFZ(x, y, fx);
            System.out.println("4:       " + fx);
            return true;
        }
        return false;
    }
}
下面是正着的L的代码
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jimu;

import java.util.Random;

/**
 *
 * @author Administrator
 */
public class L implements Jimu {

    Random r = new Random();
    String[][] map;
    private int fx = 0;
    int x = r.nextInt(21) + 5;
    int y = 4;

    public L(String[][] map) {
        this.map = map;
        setL(x, y, fx);
    }

    private void setL(int x, int y, int fx) {
        switch (fx) {
            case 0:
                map[x][y - 2] = BLACK;
                map[x][y - 1] = BLACK;
                map[x][y] = BLACK;
                map[x + 1][y] = BLACK;
                break;
            case 1:
                map[x][y - 1] = BLACK;
                map[x - 2][y] = BLACK;
                map[x - 1][y] = BLACK;
                map[x][y] = BLACK;
                break;
            case 2:
                map[x - 1][y] = BLACK;
                map[x][y] = BLACK;
                map[x][y + 1] = BLACK;
                map[x][y + 2] = BLACK;
                break;
            case 3:
                map[x][y] = BLACK;
                map[x + 1][y] = BLACK;
                map[x + 2][y] = BLACK;
                map[x][y + 1] = BLACK;
                break;
            default:
                System.out.println();
        }
    }

    private void clearL(int x, int y, int fx) {
        switch (fx) {
            case 0:
                map[x][y - 2] = WHITE;
                map[x][y - 1] = WHITE;
                map[x][y] = WHITE;
                map[x + 1][y] = WHITE;
                break;
            case 1:
                map[x][y - 1] = WHITE;
                map[x - 2][y] = WHITE;
                map[x - 1][y] = WHITE;
                map[x][y] = WHITE;
                break;
            case 2:
                map[x - 1][y] = WHITE;
                map[x][y] = WHITE;
                map[x][y + 1] = WHITE;
                map[x][y + 2] = WHITE;
                break;
                
            case 3:
                map[x][y] = WHITE;
                map[x + 1][y] = WHITE;
                map[x + 2][y] = WHITE;
                map[x][y + 1] = WHITE;
                break;
            default:
                System.out.println();
        }
    }

    public boolean goDown() {
        switch (fx) {
            case 0:
                return downOne();
            case 1:
                return downTwo();
            case 2:
                return downThree();
            case 3:
                return downFour();
        }
        return false;
    }

    public boolean goLeft() {
        switch (fx) {
            case 0:
                return leftOne();
            case 1:
                return leftTwo();
            case 2:
                return leftThree();
            case 3:
                return leftFour();
        }
        return false;
    }

    public boolean goRight() {
        switch (fx) {
            case 0:
                return rightOne();
            case 1:
                return rightTwo();
            case 2:
                return rightThree();
            case 3:
                return rightFour();
        }
        return false;
    }

    public boolean change() {
        switch (fx) {
            case 0:
                return changeOne();
            case 1:
                return changeTwo();
            case 2:
                return changeThree();
            case 3:
                return changeFour();
        }
        return false;
    }

    public int getY() {
        return y;
    }

    private boolean downOne() {
        if (!(map[x][y + 1].equals(BLACK) || map[x + 1][y + 1].equals(BLACK))) {
            clearL(x, y, fx);
            setL(x, ++y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean downTwo() {
        if (!(map[x - 2][y + 1].equals(BLACK) || map[x - 1][y + 1].equals(BLACK) || map[x][y + 1].equals(BLACK))) {
            clearL(x, y, fx);
            setL(x, ++y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean downThree() {
        if (!(map[x - 1][y + 1].equals(BLACK) || map[x][y + 3].equals(BLACK))) {
            clearL(x, y, fx);
            setL(x, ++y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean downFour() {
        if (!(map[x][y + 2].equals(BLACK) || map[x + 1][y + 1].equals(BLACK) || map[x + 2][y + 1].equals(BLACK))) {
            clearL(x, y, fx);
            setL(x, ++y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean leftOne() {
        if (!(map[x - 1][y - 2].equals(BLACK) || map[x - 1][y - 1].equals(BLACK) || map[x - 1][y].equals(BLACK))) {
            clearL(x, y, fx);
            setL(--x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean leftTwo() {
        if (!(map[x - 1][y - 1].equals(BLACK) || map[x - 3][y].equals(BLACK))) {
            clearL(x, y, fx);
            setL(--x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean leftThree() {
        if (!(map[x - 2][y].equals(BLACK) || map[x - 1][y + 1].equals(BLACK) || map[x - 1][y + 2].equals(BLACK))) {
            clearL(x, y, fx);
            setL(--x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean leftFour() {
        if (!(map[x - 1][y].equals(BLACK) || map[x - 1][y + 1].equals(BLACK))) {
            clearL(x, y, fx);
            setL(--x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean rightOne() {
        if (!(map[x + 1][y - 2].equals(BLACK) || map[x + 1][y - 1].equals(BLACK) || map[x + 2][y].equals(BLACK))) {
            clearL(x, y, fx);
            setL(++x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean rightTwo() {
        if (!(map[x + 1][y - 1].equals(BLACK) || map[x + 1][y].equals(BLACK))) {
            clearL(x, y, fx);
            setL(++x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean rightThree() {
        if (!(map[x + 1][y].equals(BLACK) || map[x + 1][y + 1].equals(BLACK) || map[x + 1][y + 2].equals(BLACK))) {
            clearL(x, y, fx);
            setL(++x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean rightFour() {
        if (!(map[x + 3][y].equals(BLACK) || map[x + 1][y + 1].equals(BLACK))) {
            clearL(x, y, fx);
            setL(++x, y, fx);
            return true;
        } else {
            return false;
        }
    }

        private boolean changeOne() {
     if (!(map[x+1][y-1].equals(BLACK) ||map[x-2][y-2].equals(BLACK) ||map[x-1][y-2].equals(BLACK) ||
             map[x-2][y-1].equals(BLACK) ||map[x-1][y-1].equals(BLACK) ||map[x-2][y].equals(BLACK) ||
             map[x-1][y].equals(BLACK) )) {
         clearL(x, y, fx);
            setL(x, y, ++fx);
            System.out.println("1:       " + fx);
            return true;
        }
        return false;
    }

    private boolean changeTwo() {
     if (!(map[x-1][y-1].equals(BLACK) ||map[x-2][y+1].equals(BLACK) ||map[x-1][y+1].equals(BLACK) ||
             map[x][y+1].equals(BLACK) ||map[x-2][y+2].equals(BLACK) ||map[x-1][y+2].equals(BLACK) ||
             map[x][y+2].equals(BLACK) )) {
         clearL(x, y, fx);
            setL(x, y, ++fx);
            System.out.println("2:       " + fx);
            return true;
        }
        return false;
    }

    private boolean changeFour() {
     if (!(map[x][y-2].equals(BLACK) ||map[x+1][y-2].equals(BLACK) ||map[x+2][y-2].equals(BLACK) ||
             map[x][y-1].equals(BLACK) ||map[x+1][y-1].equals(BLACK) ||map[x+2][y-1].equals(BLACK) ||
             map[x+1][y+1].equals(BLACK) )) {
         clearL(x, y, fx);
         if ((++fx) > 2) {
                fx = 0;
            }
            setL(x, y, fx);
            System.out.println("4:       " + fx);
            return true;
        }
        return false;
    }

    private boolean changeThree() {
     if (!(map[x+1][y].equals(BLACK) ||map[x+2][y].equals(BLACK) ||map[x-1][y+1].equals(BLACK) ||
             map[x+1][y+1].equals(BLACK) ||map[x+2][y+1].equals(BLACK) ||map[x+1][y+2].equals(BLACK) ||
             map[x+2][y+2].equals(BLACK) )) {
         clearL(x, y, fx);
            
            setL(x, y, ++fx);
            System.out.println("3:       " + fx);
            return true;
        }
        return false;
    }
}
下面是一竖的代码:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jimu;

import java.util.Random;

/**
 *
 * @author Administrator
 */
public class One implements Jimu {

    Random r = new Random();
    String[][] map;
    private int fx = 0;
    int x = r.nextInt(21) + 5;
    int y = 4;

    public One(String[][] map) {
        this.map = map;
        setOne(x, y, fx);
    }

    public void setOne(int x, int y, int fx) {
//        num=fx-1;
        switch (fx) {
            case 0:
                map[x][y] = BLACK;
                map[x][y - 1] = BLACK;
                map[x][y - 2] = BLACK;
                map[x][y - 3] = BLACK;
                break;
            case 1:
                map[x][y] = BLACK;
                map[x - 1][y] = BLACK;
                map[x - 2][y] = BLACK;
                map[x - 3][y] = BLACK;
                break;
            case 2:
                map[x][y] = BLACK;
                map[x][y + 1] = BLACK;
                map[x][y + 2] = BLACK;
                map[x][y + 3] = BLACK;
                break;
            case 3:
                map[x][y] = BLACK;
                map[x + 1][y] = BLACK;
                map[x + 2][y] = BLACK;
                map[x + 3][y] = BLACK;
                break;
            default:
                System.out.println("方向出错了set:" + fx);
        }
    }

    public void clearOne(int x, int y, int fx) {
        switch (fx) {
            case 0:
                map[x][y] = WHITE;
                map[x][y - 1] = WHITE;
                map[x][y - 2] = WHITE;
                map[x][y - 3] = WHITE;
                break;
            case 1:
                map[x][y] = WHITE;
                map[x - 1][y] = WHITE;
                map[x - 2][y] = WHITE;
                map[x - 3][y] = WHITE;
                break;
            case 2:
                map[x][y] = WHITE;
                map[x][y + 1] = WHITE;
                map[x][y + 2] = WHITE;
                map[x][y + 3] = WHITE;
                break;
            case 3:
                map[x][y] = WHITE;
                map[x + 1][y] = WHITE;
                map[x + 2][y] = WHITE;
                map[x + 3][y] = WHITE;
                break;
            default:
                System.out.println("方向出错了clear:" + fx);
        }
    }

    public boolean goDown() {
        switch (fx) {
            case 0:
                return downOne();
            case 1:
                return downSecond();
            case 2:
                return downThree();
            case 3:
                return downFour();
        }
        return false;
    }

    public boolean goLeft() {
        switch (fx) {
            case 0:
                return leftOne();
            case 1:
                return leftSecond();
            case 2:
                return leftThree();
            case 3:
                return leftFour();
        }
        return false;
    }

    public boolean goRight() {
        switch (fx) {
            case 0:
                return rightOne();
            case 1:
                return rightSecond();
            case 2:
                return rightThree();
            case 3:
                return rightFour();
        }
        return false;
    }

    public boolean change() {
        switch (fx) {
            case 0:
                return changeOne();
            case 1:
                return changeTwo();
            case 2:
                return changeThree();
            case 3:
                return changeFour();
        }
        return false;
    }

    public int getY() {
        return y;
    }

    private boolean downOne() {
        if (!map[x][y + 1].equals(BLACK)) {
            clearOne(x, y, fx);
            setOne(x, ++y, fx);
            return true;
        }
        return false;
    }

    private boolean downSecond() {
        if ((!map[x][y + 1].equals(BLACK)) && (!map[x - 1][y + 1].equals(BLACK)) && (!map[x - 2][y + 1].equals(BLACK)) && (!map[x - 3][y + 1].equals(BLACK))) {
            clearOne(x, y, fx);
            setOne(x, ++y, fx);
            return true;
        }
        return false;
    }

    private boolean downThree() {
        if (!map[x][y + 4].equals(BLACK)) {
            clearOne(x, y, fx);
            setOne(x, ++y, fx);
            return true;
        }
        return false;
    }

    private boolean downFour() {
        if (!(map[x][y + 1].equals(BLACK) || map[x + 1][y + 1].equals(BLACK) || map[x + 2][y + 1].equals(BLACK) || map[x + 3][y + 1].equals(BLACK))) {
            clearOne(x, y, fx);
            setOne(x, ++y, fx);
            return true;
        }
        return false;
    }

    private boolean leftOne() {
        if (!(map[x - 1][y].equals(BLACK) || map[x - 1][y - 1].equals(BLACK) || map[x - 1][y - 2].equals(BLACK) || map[x - 1][y - 3].equals(BLACK))) {
            clearOne(x, y, fx);
            setOne(--x, y, fx);
            return true;
        }
        return false;
    }

    private boolean leftSecond() {
        if (!map[x - 4][y].equals(BLACK)) {
            clearOne(x, y, fx);
            setOne(--x, y, fx);
            return true;
        }
        return false;
    }

    private boolean leftThree() {
        if (!(map[x - 1][y].equals(BLACK) || map[x - 1][y + 1].equals(BLACK) || map[x - 1][y + 2].equals(BLACK) || map[x - 1][y + 3].equals(BLACK))) {
            clearOne(x, y, fx);
            setOne(--x, y, fx);
            return true;
        }
        return false;
    }

    private boolean leftFour() {
        if (!map[x - 1][y].equals(BLACK)) {
            clearOne(x, y, fx);
            setOne(--x, y, fx);
            return true;
        }
        return false;
    }

    private boolean rightOne() {
        if (!(map[x + 1][y].equals(BLACK) || map[x + 1][y - 1].equals(BLACK) || map[x + 1][y - 2].equals(BLACK) || map[x + 1][y - 3].equals(BLACK))) {
            clearOne(x, y, fx);
            setOne(++x, y, fx);
            return true;
        }
        return false;
    }

    private boolean rightSecond() {
        if (!map[x + 1][y].equals(BLACK)) {
            clearOne(x, y, fx);
            setOne(++x, y, fx);
            return true;
        }
        return false;
    }

    private boolean rightThree() {
        if (!(map[x + 1][y].equals(BLACK) || map[x + 1][y + 1].equals(BLACK) || map[x + 1][y + 2].equals(BLACK) || map[x + 1][y + 3].equals(BLACK))) {
            clearOne(x, y, fx);
            setOne(++x, y, fx);
            return true;
        }
        return false;
    }

    private boolean rightFour() {
        if (!map[x + 4][y].equals(BLACK)) {
            clearOne(x, y, fx);
            setOne(++x, y, fx);
            return true;
        }
        return false;
    }

    private boolean changeOne() {
        if (!(map[x - 3][y - 3].equals(BLACK) || map[x - 2][y - 3].equals(BLACK) || map[x - 1][y - 3].equals(BLACK) ||
                map[x - 3][y - 2].equals(BLACK) || map[x - 2][y - 2].equals(BLACK) || map[x - 1][y - 2].equals(BLACK) ||
                map[x - 3][y - 1].equals(BLACK) || map[x - 2][y - 1].equals(BLACK) || map[x - 1][y - 1].equals(BLACK) ||
                map[x - 3][y].equals(BLACK) || map[x - 2][y].equals(BLACK) || map[x - 1][y].equals(BLACK))) {
            clearOne(x, y, fx);
            setOne(x, y, ++fx);
            System.out.println("1:       " + fx);
            return true;
        }
        return false;
    }

    private boolean changeTwo() {
        if (!(map[x - 3][y + 1].equals(BLACK) || map[x - 2][y + 1].equals(BLACK) || map[x - 1][y + 1].equals(BLACK) ||
                map[x][y + 1].equals(BLACK) || map[x - 3][y + 2].equals(BLACK) || map[x - 2][y + 2].equals(BLACK) ||
                map[x - 1][y + 2].equals(BLACK) || map[x][y + 2].equals(BLACK) || map[x - 3][y + 3].equals(BLACK) ||
                map[x - 2][y + 3].equals(BLACK) || map[x - 1][y + 3].equals(BLACK) || map[x][y + 3].equals(BLACK))) {
            clearOne(x, y, fx);
            setOne(x, y, ++fx);
            System.out.println("2:       " + fx);
            return true;
        }
        return false;
    }

    private boolean changeThree() {
        if (!(map[x + 1][y].equals(BLACK) || map[x + 2][y].equals(BLACK) || map[x + 3][y].equals(BLACK) ||
                map[x + 1][y + 1].equals(BLACK) || map[x + 2][y + 1].equals(BLACK) || map[x + 3][y + 1].equals(BLACK) ||
                map[x + 1][y + 2].equals(BLACK) || map[x + 2][y + 2].equals(BLACK) || map[x + 3][y + 2].equals(BLACK) ||
                map[x + 1][y + 3].equals(BLACK) || map[x + 2][y + 3].equals(BLACK) || map[x + 3][y + 3].equals(BLACK))) {
            clearOne(x, y, fx);
            setOne(x, y, ++fx);
            System.out.println("3:       " + fx);
            return true;
        }
        return false;
    }

    private boolean changeFour() {
        if (!(map[x][y - 3].equals(BLACK) || map[x + 1][y - 3].equals(BLACK) || map[x + 2][y - 3].equals(BLACK) ||
                map[x + 3][y - 3].equals(BLACK) || map[x][y - 2].equals(BLACK) || map[x + 1][y - 2].equals(BLACK) ||
                map[x + 2][y - 2].equals(BLACK) || map[x + 3][y - 2].equals(BLACK) || map[x][y - 1].equals(BLACK) ||
                map[x + 1][y - 1].equals(BLACK) || map[x + 2][y - 1].equals(BLACK) || map[x + 3][y - 1].equals(BLACK))) {
            clearOne(x, y, fx);
            if ((++fx) > 2) {
                fx = 0;
            }
            setOne(x, y, fx);
            System.out.println("4:       " + fx);
            return true;
        }
        return false;
    }
}
下面是田字形的方块的代码:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jimu;

import java.util.Random;

/**
 *
 * @author Administrator
 */
public class Tian implements Jimu {

    Random r = new Random();
    String[][] map;
    private int fx = 0;
    int x = r.nextInt(21) + 5;
    int y = 4;

    public Tian(String[][] map) {
        this.map = map;
        setTian(x, y, fx);
    }

    public void setTian(int x, int y, int fx) {
        switch (fx) {
            case 0:
                map[x][y - 1] = BLACK;
                map[x + 1][y - 1] = BLACK;
                map[x][y] = BLACK;
                map[x + 1][y] = BLACK;
                break;
            case 1:
                map[x - 1][y - 1] = BLACK;
                map[x][y - 1] = BLACK;
                map[x - 1][y] = BLACK;
                map[x][y] = BLACK;
                break;
            case 2:
                map[x - 1][y] = BLACK;
                map[x][y] = BLACK;
                map[x - 1][y + 1] = BLACK;
                map[x][y + 1] = BLACK;
                break;
            case 3:
                map[x][y] = BLACK;
                map[x + 1][y] = BLACK;
                map[x][y + 1] = BLACK;
                map[x + 1][y + 1] = BLACK;
                break;
            default:
                System.out.println("方向出错了:" + fx);
        }
    }

    public void clearTian(int x, int y, int fx) {
        switch (fx) {
            case 0:
                map[x][y - 1] = WHITE;
                map[x + 1][y - 1] = WHITE;
                map[x][y] = WHITE;
                map[x + 1][y] = WHITE;
                break;
            case 1:
                map[x - 1][y - 1] = WHITE;
                map[x][y - 1] = WHITE;
                map[x - 1][y] = WHITE;
                map[x][y] = WHITE;
                break;
            case 2:
                map[x - 1][y] = WHITE;
                map[x][y] = WHITE;
                map[x - 1][y + 1] = WHITE;
                map[x][y + 1] = WHITE;
                break;
            case 3:
                map[x][y] = WHITE;
                map[x + 1][y] = WHITE;
                map[x][y + 1] = WHITE;
                map[x + 1][y + 1] = WHITE;
                break;
            default:
                System.out.println("方向出错了:" + fx);
        }
    }

    public boolean goDown() {
        switch (fx) {
            case 0:
                return downOne();
            case 1:
                return downTwo();
            case 2:
                return downThree();
            case 3:
                return downFour();
        }
        return false;
    }

    public boolean goLeft() {
        switch (fx) {
            case 0:
                return leftOne();
            case 1:
                return leftTwo();
            case 2:
                return leftThree();
            case 3:
                return leftFour();
        }
        return false;
    }

    public boolean goRight() {
        switch (fx) {
            case 0:
                return rightOne();
            case 1:
                return rightTwo();
            case 2:
                return rightThree();
            case 3:
                return rightFour();
        }
        return false;
    }

    public boolean change() {
        switch (fx) {
            case 0:
                return changeOne();
            case 1:
                return changeTwo();
            case 2:
                return changeThree();
            case 3:
                return changeFour();
        }
        return false;
    }

    public int getY() {
        return y;
    }

    private boolean downOne() {
        if (!(map[x][y + 1].equals(BLACK) || map[x + 1][y + 1].equals(BLACK))) {
            clearTian(x, y, fx);
            setTian(x, ++y, fx);
            return true;
        }
        return false;
    }

    private boolean downTwo() {
        if (!(map[x - 1][y + 1].equals(BLACK) || map[x][y + 1].equals(BLACK))) {
            clearTian(x, y, fx);
            setTian(x, ++y, fx);
            return true;
        }
        return false;
    }

    private boolean downThree() {
        if (!(map[x - 1][y + 2].equals(BLACK) || map[x][y + 2].equals(BLACK))) {
            clearTian(x, y, fx);
            setTian(x, ++y, fx);
            return true;
        }
        return false;
    }

    private boolean downFour() {
        if (!(map[x][y + 2].equals(BLACK) || map[x + 1][y + 2].equals(BLACK))) {
            clearTian(x, y, fx);
            setTian(x, ++y, fx);
            return true;
        }
        return false;
    }

    private boolean leftOne() {
        if (!(map[x - 1][y - 1].equals(BLACK) || map[x - 1][y].equals(BLACK))) {
            clearTian(x, y, fx);
            setTian(--x, y, fx);
            return true;
        }
        return false;
    }

    private boolean leftTwo() {
        if (!(map[x - 2][y - 1].equals(BLACK) || map[x - 2][y].equals(BLACK))) {
            clearTian(x, y, fx);
            setTian(--x, y, fx);
            return true;
        }
        return false;
    }

    private boolean leftThree() {
        if (!(map[x - 2][y + 1].equals(BLACK) || map[x - 2][y].equals(BLACK))) {
            clearTian(x, y, fx);
            setTian(--x, y, fx);
            return true;
        }
        return false;
    }

    private boolean leftFour() {
        if (!(map[x - 1][y + 1].equals(BLACK) || map[x - 1][y].equals(BLACK))) {
            clearTian(x, y, fx);
            setTian(--x, y, fx);
            return true;
        }
        return false;
    }

    private boolean rightOne() {
        if (!(map[x + 2][y - 1].equals(BLACK) || map[x + 2][y].equals(BLACK))) {
            clearTian(x, y, fx);
            setTian(++x, y, fx);
            return true;
        }
        return false;
    }

    private boolean rightTwo() {
        if (!(map[x + 1][y - 1].equals(BLACK) || map[x + 1][y].equals(BLACK))) {
            clearTian(x, y, fx);
            setTian(++x, y, fx);
            return true;
        }
        return false;
    }

    private boolean rightThree() {
        if (!(map[x + 1][y].equals(BLACK) || map[x + 1][y + 1].equals(BLACK))) {
            clearTian(x, y, fx);
            setTian(++x, y, fx);
            return true;
        }
        return false;
    }

    private boolean rightFour() {
        if (!(map[x + 2][y + 1].equals(BLACK) || map[x + 2][y].equals(BLACK))) {
            clearTian(x, y, fx);
            setTian(++x, y, fx);
            return true;
        }
        return false;
    }

    private boolean changeOne() {
        if (!(map[x][y - 2].equals(BLACK) || map[x + 1][y - 2].equals(BLACK) || map[x - 1][y - 2].equals(BLACK) ||
                map[x - 1][y - 1].equals(BLACK) || map[x - 1][y].equals(BLACK))) {
            clearTian(x, y, fx);
            setTian(x, y, ++fx);
            System.out.println("1:       " + fx);
            return true;
        }
        return false;
    }

    private boolean changeTwo() {
        if (!(map[x - 2][y - 1].equals(BLACK) || map[x - 2][y].equals(BLACK) || map[x - 2][y + 1].equals(BLACK) ||
                map[x - 1][y + 1].equals(BLACK) || map[x][y + 1].equals(BLACK))) {
            clearTian(x, y, fx);
            setTian(x, y, ++fx);
            System.out.println("2:       " + fx);
            return true;
        }
        return false;
    }

    private boolean changeThree() {
        if (!(map[x + 1][y].equals(BLACK) || map[x + 1][y + 1].equals(BLACK) || map[x - 1][y + 2].equals(BLACK) ||
                map[x][y + 2].equals(BLACK) || map[x + 1][y + 2].equals(BLACK))) {
            clearTian(x, y, fx);
            setTian(x, y, ++fx);
            System.out.println("3:       " + fx);
            return true;
        }
        return false;
    }

    private boolean changeFour() {
        if (!(map[x][y - 1].equals(BLACK) || map[x + 1][y - 1].equals(BLACK) || map[x + 2][y - 1].equals(BLACK) ||
                map[x + 2][y].equals(BLACK) || map[x + 2][y + 1].equals(BLACK))) {
            clearTian(x, y, fx);
            if ((++fx) > 2) {
                fx = 0;
            }
            setTian(x, y, fx);
            System.out.println("4:       " + fx);
            return true;
        }
        return false;
    }
}
再下面是正Z的代码:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jimu;

import java.util.Random;

/**
 *
 * @author Administrator
 */
public class Z implements Jimu {

    Random r = new Random();
    String[][] map;
    private int fx = 0;
    int x = r.nextInt(21) + 5;
    int y = 4;

    public Z(String[][] map) {
        this.map = map;
        setZ(x, y, fx);
    }

    private void setZ(int x, int y, int fx) {
        switch (fx) {
            case 0:
                map[x - 1][y - 1] = BLACK;
                map[x][y - 1] = BLACK;
                map[x][y] = BLACK;
                map[x + 1][y] = BLACK;
                break;
            case 1:
                map[x][y - 1] = BLACK;
                map[x - 1][y] = BLACK;
                map[x][y] = BLACK;
                map[x - 1][y + 1] = BLACK;
                break;
            case 2:
                map[x - 1][y] = BLACK;
                map[x][y] = BLACK;
                map[x][y + 1] = BLACK;
                map[x + 1][y + 1] = BLACK;
                break;
            case 3:
                map[x + 1][y - 1] = BLACK;
                map[x][y] = BLACK;
                map[x + 1][y] = BLACK;
                map[x][y + 1] = BLACK;
                break;
            default:
                System.out.println();
        }
    }

    private void clearZ(int x, int y, int fx) {
        switch (fx) {
            case 0:
                map[x - 1][y - 1] = WHITE;
                map[x][y - 1] = WHITE;
                map[x][y] = WHITE;
                map[x + 1][y] = WHITE;
                break;
            case 1:
                map[x][y - 1] = WHITE;
                map[x - 1][y] = WHITE;
                map[x][y] = WHITE;
                map[x - 1][y + 1] = WHITE;
                break;
            case 2:
                map[x - 1][y] = WHITE;
                map[x][y] = WHITE;
                map[x][y + 1] = WHITE;
                map[x + 1][y + 1] = WHITE;
                break;
            case 3:
                map[x + 1][y - 1] = WHITE;
                map[x][y] = WHITE;
                map[x + 1][y] = WHITE;
                map[x][y + 1] = WHITE;
                break;
            default:
                System.out.println();
        }
    }

    public boolean goDown() {
        switch (fx) {
            case 0:
                return downOne();
            case 1:
                return downTwo();
            case 2:
                return downThree();
            case 3:
                return downFour();
        }
        return false;
    }

    public boolean goLeft() {
        switch (fx) {
            case 0:
                return leftOne();
            case 1:
                return leftTwo();
            case 2:
                return leftThree();
            case 3:
                return leftFour();
        }
        return false;
    }

    public boolean goRight() {
        switch (fx) {
            case 0:
                return rightOne();
            case 1:
                return rightTwo();
            case 2:
                return rightThree();
            case 3:
                return rightFour();
        }
        return false;
    }

    public boolean change() {
        switch (fx) {
            case 0:
                return changeOne();
            case 1:
                return changeTwo();
            case 2:
                return changeThree();
            case 3:
                return changeFour();
        }
        return false;
    }

    public int getY() {
        return y;
    }

    private boolean downOne() {
        if (!(map[x - 1][y].equals(BLACK) || map[x][y + 1].equals(BLACK) || map[x + 1][y + 1].equals(BLACK))) {
            clearZ(x, y, fx);
            setZ(x, ++y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean downTwo() {
        if (!(map[x - 1][y + 2].equals(BLACK) || map[x][y + 1].equals(BLACK))) {
            clearZ(x, y, fx);
            setZ(x, ++y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean downThree() {
        if (!(map[x - 1][y + 1].equals(BLACK) || map[x][y + 2].equals(BLACK) || map[x + 1][y + 2].equals(BLACK))) {
            clearZ(x, y, fx);
            setZ(x, ++y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean downFour() {
        if (!(map[x][y + 2].equals(BLACK) || map[x + 1][y + 1].equals(BLACK))) {
            clearZ(x, y, fx);
            setZ(x, ++y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean leftOne() {
        if (!(map[x - 2][y - 1].equals(BLACK) || map[x - 1][y].equals(BLACK))) {
            clearZ(x, y, fx);
            setZ(--x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean leftTwo() {
        if (!(map[x - 1][y - 1].equals(BLACK) || map[x - 2][y].equals(BLACK) || map[x - 2][y + 1].equals(BLACK))) {
            clearZ(x, y, fx);
            setZ(--x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean leftThree() {
        if (!(map[x - 2][y].equals(BLACK) || map[x - 1][y + 1].equals(BLACK))) {
            clearZ(x, y, fx);
            setZ(--x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean leftFour() {
        if (!(map[x][y - 1].equals(BLACK) || map[x - 1][y].equals(BLACK) || map[x - 1][y + 1].equals(BLACK))) {
            clearZ(x, y, fx);
            setZ(--x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean rightOne() {
        if (!(map[x + 1][y - 1].equals(BLACK) || map[x + 2][y].equals(BLACK))) {
            clearZ(x, y, fx);
            setZ(++x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean rightTwo() {
        if (!(map[x + 1][y - 1].equals(BLACK) || map[x + 1][y].equals(BLACK) || map[x][y + 1].equals(BLACK))) {
            clearZ(x, y, fx);
            setZ(++x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean rightThree() {
        if (!(map[x + 1][y].equals(BLACK) || map[x + 2][y + 1].equals(BLACK))) {
            clearZ(x, y, fx);
            setZ(++x, y, fx);
            return true;
        } else {
            return false;
        }
    }

    private boolean rightFour() {
        if (!(map[x + 2][y - 1].equals(BLACK) || map[x + 2][y].equals(BLACK) || map[x + 1][y + 1].equals(BLACK))) {
            clearZ(x, y, fx);
            setZ(++x, y, fx);
            return true;
        } else {
            return false;
        }
    }
        private boolean changeOne() {
        if (!(map[x+1][y-1].equals(BLACK) || map[x-1][y].equals(BLACK) ||
                map[x-1][y+1].equals(BLACK) || map[x][y+1].equals(BLACK))) {
            clearZ(x, y, fx);
            setZ(x, y, ++fx);
            System.out.println("1:       " + fx);
            return true;
        }
        return false;
    }

    private boolean changeTwo() {
        if (!(map[x-1][y-1].equals(BLACK)||map[x+1][y].equals(BLACK)||
                map[x][y+1].equals(BLACK)||map[x+1][y+1].equals(BLACK) )) {
            clearZ(x, y, fx);
            setZ(x, y, ++fx);
            System.out.println("2:       " + fx);
            return true;
        }
        return false;
    }

    private boolean changeThree() {
        if (!(map[x][y-1].equals(BLACK)||map[x+1][y-1].equals(BLACK)||
                map[x+1][y].equals(BLACK)||map[x-1][y+1].equals(BLACK)  )) {
            clearZ(x, y, fx);
            setZ(x, y, ++fx);
            System.out.println("3:       " + fx);
            return true;
        }
        return false;
    }

    private boolean changeFour() {
        if (!(map[x-1][y-1].equals(BLACK)||map[x][y-1].equals(BLACK)||
                map[x-1][y].equals(BLACK)||map[x+1][y+1].equals(BLACK)  )) {
            clearZ(x, y, fx);
            if ((++fx) > 2) {
                fx = 0;
            }
            setZ(x, y, fx);
            System.out.println("4:       " + fx);
            return true;
        }
        return false;
    }
}
最后是删除满行的代码:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jimu;

/**
 *
 * @author Administrator
 */
public class DeleteJimu {

    String[][] map;

    final String s1 = "□";
    //这里是定义你需要的字符串,一个为空心方,一个是实心方
    final String s2 = "■";
    final int x = 30;
    //这里定义的是行数和列数,用来实现对应的字符的排列
    final int y = 30;
    public int k = 0;           //判断全部黑色方块的行数
    public int count=0;     //初始分数为0
    public DeleteJimu(String[][] map) {
        this.map = map;     //创建一个二维数组的元素,并传值
      
    }


    public int upsideDown() {       //进行颜色的判断,返回对应的boolean类型的结果
        boolean b = true;
        for (int j = 2; j <=y - 2; j++) {
            for (int i = 1; i < x - 2; i++) {
                b = map[i][j].equals(s2) && b;  //只要有一个不是黑色的,b为false,否则b为true
                //也就是全黑b才为true 
            }
            if(b){      //如果为true就进行删除,并再次进行判断,直到最后一行
                setA(j);
                k = j;
            }
            b=true;     //当为false的时候,修改值为true
        }
        return k;       //返回k的值

    }

    public boolean panduan() {
        k=upsideDown();
        //获取k的值,进行判断
        //如果k不是边框和0就返回false,也就是没有满行
        //否则,返回true,删除对应的满行
        //这里k=0的原因是因为俄罗斯方块没有办法达到最上面的几行,所以,k最大可以取值到3
        //要进行优化的话,这里也是一个不错的选择点
        if(k<=y-1&&k!=0){
            k=0;
            return true;
        }
        return false;
        }
   

    public void setA(int num) {
        //这里就是把满行的替换成上面一行,并以此类推的替换掉上面的全部的
        for(int i=1;i<x-1;i++){
            map[i][num]=s1;
        }
        //首先是把当前行设置为白色的,然后下面就进行替换
        for(int j=num;j>5;j--){
            for(int i=1;i<x-1;i++){
                map[i][j]=map[i][j-1];
            }
        }
        //每删除一次,分数进行加法运算
        count+=1000;
    }
}
到这里,基本上jimu包中的代码就全部完成了,在这里主要是每一个方块的设计和if的判断是很有关系的。

下一博文我会上传我的设置的样子,然后作为上面源代码的参考。








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值