import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
 
public class TemplateMIDlet extends MIDlet implements Runnable {
    
    int FIELD_WIDTH = 12;
    int FIELD_HEIGHT = 22;
    
    int[][][] pattern = {
        {
            {0, 1, 0},
            {0, 1, 0},
            {0, 1, 0}
        }, {
            {0, 1, 0},
            {0, 1, 1},
            {0, 0, 0}
        }
    };
    
    int mode = 0; // ゲーム内の状態を表すモード(0:落下開始、1:落下中、2:着地)
    Thread thread;
    StringBuffer keyBuffer = new StringBuffer();
    int[][] field = new int[FIELD_HEIGHT][FIELD_WIDTH];
    int[][] next = new int[pattern[0].length][pattern[0].length];
    
    Image offi;
    Canvas canvas;
    
    int COLOR_GRAY = 0xC0C0C0;
 
    int COLOR_RED = 0xFF0000;
    int COLOR_BLACK = 0x000000;
    
    public TemplateMIDlet() {
        canvas = new Canvas() {
            public void paint(Graphics g) {
                g.drawImage(offi, 0, 0, Graphics.TOP | Graphics.LEFT);
            }
            
            protected void keyPressed(int keyCode) {
                // 落下中でかつ先行入力が2つ未満の場合のみキー入力を受け付ける
                if (mode == 1 && keyBuffer.length() < 2) {
                    int ga = getGameAction(keyCode);
                    if (ga == Canvas.DOWN) {
                        keyBuffer.append('D');
                    } else if (ga == Canvas.LEFT) {
                        keyBuffer.append('L');
                    } else if (ga == Canvas.RIGHT) {
                        keyBuffer.append('R');
                    } else {
                        keyBuffer.append('A');
                    }
                }
            }
            
            protected void keyRepeated(int keyCode) {
                // キーを押しっぱなしの場合の動作はキーが押された場合と同じ
                keyPressed(keyCode);
            }
        };
        offi = Image.createImage(canvas.getWidth(), canvas.getHeight());
        Display.getDisplay(this).setCurrent(canvas);
    }
    
    protected void startApp() {
        if (thread == null) {
            thread = new Thread(this);
            thread.start();
        }
    }
    
    protected void pauseApp() {
        // このイベントは利用しないので実装する必要はない
    }
    
    protected void destroyApp(boolean unconditional) {
        // このイベントは利用しないので実装する必要はない
    }
    
    public void run() {
        int x = 0;
        int y = 0;
        int count = 0;
 
        Graphics g = offi.getGraphics();
        initField();
        int[][] current = new int[pattern[0].length][pattern[0].length];
        copy(pattern[0], next);
        while (true) {
            if (mode == 0) {
                if (keyBuffer.length() > 0) {
                    keyBuffer.delete(0, keyBuffer.length() - 1);
                }
                copy(next, current);
                copy(pattern[0], next);
                x = FIELD_WIDTH / 2 - (int)Math.sqrt(pattern[0].length);
                y = 0;
                mode = 1;
                count = 0;
            } else if (mode == 1) {
 
                if (count != 0) {
                    char key = 'N'; // 'N' は何も押していないことを表す
                    if (count % 20 == 0) {
                        if (!(keyBuffer.length() > 0 && keyBuffer.charAt(0) == 'D')) {
                            keyBuffer.insert(0, 'D');
                        }
                    }
                    if (keyBuffer.length() > 0) {
                        key = keyBuffer.charAt(0);
                        keyBuffer.deleteCharAt(0);
                        if (key == 'L') {
                            x--;
                        } else if (key == 'R') {
                            x++;
                        } else if (key == 'D') {
                            y++;
                        } else if (key == 'A') {
                            rotate(current);
                        }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
                    }
                }
                
                for (int i = 0; i < FIELD_HEIGHT; i++) {
                    for (int j = 0; j < FIELD_WIDTH; j++) {
                        if (field[i][j] == 1) {
                            field[i][j] = 0;
                        }
                    }
                }
                
                for (int i = 0; i < current.length; i++) {
                    for (int j = 0; j < current[i].length; j++) {
                        if (current[i][j] == 1) {
                            field[y + i][x + j] = 1;
 
 
 
                        }
                    }
                }
                
                // メインフィールドを描画
                drawBlocks(g);
                
 
 
 
                if (count == 0) {
                    sleep(150);
                } else {
                    sleep(50);
                }
                count++;
 
 
            }
            canvas.repaint();
            canvas.serviceRepaints();
        }
    }
    
    void copy(int[][] src, int[][] dest) {
        for (int i = 0; i < src.length; i++) {
            System.arraycopy(src[i], 0, dest[i], 0, src[i].length);
        }
    }
    
    void rotate(int[][] omino) {
        int[][] temp = new int[omino[0].length][omino[0].length];
        temp[0][2] = omino[0][0];
        temp[1][2] = omino[0][1];
        temp[2][2] = omino[0][2];
        temp[0][1] = omino[1][0];
        temp[1][1] = omino[1][1];
        temp[2][1] = omino[1][2];
        temp[0][0] = omino[2][0];
        temp[1][0] = omino[2][1];
        temp[2][0] = omino[2][2];
        for (int i = 0; i < omino.length; i++) {
            System.arraycopy(temp[i], 0, omino[i], 0, temp[i].length);
        }
    }
    
 
 
 
 
 
 
 
 
 
 
 
 
 
    void initField() {
        for (int i = 0; i < FIELD_HEIGHT; i++) {
            for (int j = 0; j < FIELD_WIDTH; j++) {
                if (j == 0 || j == FIELD_WIDTH - 1) {
                    field[i][j] = -1;
                } else if (i == FIELD_HEIGHT - 1) {
                    field[i][j] = -1;
                } else {
                    field[i][j] = 0;
                }
            }
        }
        for (int i = 1; i < FIELD_WIDTH - 1; i++) {
            field[FIELD_HEIGHT - 1][i] = -1;
        }
        for (int i = 0; i < FIELD_HEIGHT; i++) {
            field[i][0] = -1;
            field[i][FIELD_WIDTH - 1] = -1;
        }
    }
    
    void drawBlocks(Graphics g) {
        int blockSize = canvas.getHeight() / (FIELD_HEIGHT + 2);
        for (int i = 0; i < FIELD_HEIGHT; i++) {
            for (int j = 0; j < FIELD_WIDTH; j++) {
                if (field[i][j] == -1) {
                    g.setColor(COLOR_GRAY);
                    g.fillRect(j * blockSize, i * blockSize, blockSize, blockSize);
                } else if (field[i][j] == 1) {
                    g.setColor(COLOR_RED);
                    g.fillRect(j * blockSize, i * blockSize, blockSize, blockSize);
 
 
 
                } else {
                    g.setColor(COLOR_BLACK);
                    g.fillRect(j * blockSize, i * blockSize, blockSize, blockSize);
                }
            }
        }
    }
    
    void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            // 例外は発生しない
        }
    }
    
}
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
 
public class TemplateMIDlet extends MIDlet implements Runnable {
    
    int FIELD_WIDTH = 12;
    int FIELD_HEIGHT = 22;
    
    int[][][] pattern = {
        {
            {0, 1, 0},
            {0, 1, 0},
            {0, 1, 0}
        }, {
            {0, 1, 0},
            {0, 1, 1},
            {0, 0, 0}
        }
    };
    
    int mode = 0; // ゲーム内の状態を表すモード(0:落下開始、1:落下中、2:着地)
    Thread thread;
    StringBuffer keyBuffer = new StringBuffer();
    int[][] field = new int[FIELD_HEIGHT][FIELD_WIDTH];
    int[][] next = new int[pattern[0].length][pattern[0].length];
    
    Image offi;
    Canvas canvas;
    
    int COLOR_GRAY = 0xC0C0C0;
    int COLOR_BLUE = 0x0000FF;
    int COLOR_RED = 0xFF0000;
    int COLOR_BLACK = 0x000000;
    
    public TemplateMIDlet() {
        canvas = new Canvas() {
            public void paint(Graphics g) {
                g.drawImage(offi, 0, 0, Graphics.TOP | Graphics.LEFT);
            }
            
            protected void keyPressed(int keyCode) {
                // 落下中でかつ先行入力が2つ未満の場合のみキー入力を受け付ける
                if (mode == 1 && keyBuffer.length() < 2) {
                    int ga = getGameAction(keyCode);
                    if (ga == Canvas.DOWN) {
                        keyBuffer.append('D');
                    } else if (ga == Canvas.LEFT) {
                        keyBuffer.append('L');
                    } else if (ga == Canvas.RIGHT) {
                        keyBuffer.append('R');
                    } else {
                        keyBuffer.append('A');
                    }
                }
            }
            
            protected void keyRepeated(int keyCode) {
                // キーを押しっぱなしの場合の動作はキーが押された場合と同じ
                keyPressed(keyCode);
            }
        };
        offi = Image.createImage(canvas.getWidth(), canvas.getHeight());
        Display.getDisplay(this).setCurrent(canvas);
    }
    
    protected void startApp() {
        if (thread == null) {
            thread = new Thread(this);
            thread.start();
        }
    }
    
    protected void pauseApp() {
        // このイベントは利用しないので実装する必要はない
    }
    
    protected void destroyApp(boolean unconditional) {
        // このイベントは利用しないので実装する必要はない
    }
    
    public void run() {
        int x = 0;
        int y = 0;
        int count = 0;
        int prevKey = '0';
        Graphics g = offi.getGraphics();
        initField();
        int[][] current = new int[pattern[0].length][pattern[0].length];
        copy(pattern[0], next);
        while (true) {
            if (mode == 0) {
                if (keyBuffer.length() > 0) {
                    keyBuffer.delete(0, keyBuffer.length() - 1);
                }
                copy(next, current);
                copy(pattern[0], next);
                x = FIELD_WIDTH / 2 - (int)Math.sqrt(pattern[0].length);
                y = 0;
                mode = 1;
                count = 0;
            } else if (mode == 1) {
                boolean fix = false;
                if (count != 0) {
                    char key = 'N'; // 'N' は何も押していないことを表す
                    if (count % 20 == 0) {
                        if (!(keyBuffer.length() > 0 && keyBuffer.charAt(0) == 'D')) {
                            keyBuffer.insert(0, 'D');
                        }
                    }
                    if (keyBuffer.length() > 0) {
                        key = keyBuffer.charAt(0);
                        keyBuffer.deleteCharAt(0);
                        if (key == 'L') {
                            x--;
                        } else if (key == 'R') {
                            x++;
                        } else if (key == 'D') {
                            y++;
                        } else if (key == 'A') {
                            rotate(current);
                        }
                        if (isHit(x, y, current)) {
                            if (key == 'L') {
                                x++;
                            } else if (key == 'R') {
                                x--;
                            } else if (key == 'A') {
                                if (prevKey == 'L') {
                                    if (!isHit(x + 1, y, current)) {
                                        x++;
                                    } else {
                                        rotate(current);
                                        rotate(current);
                                        rotate(current);
                                    }
                                } else if (prevKey == 'R') {
                                    if (!isHit(x - 1, y, current)) {
                                        x--;
                                    } else {
                                        rotate(current);
                                        rotate(current);
                                        rotate(current);
                                    }
                                }
                            } else if (key == 'D') {
                                y--;
                                fix = true;
                            }
                        }
                        if (key == 'L' || key == 'R') {
                            prevKey = key;
                        }
                    }
                }
                
                for (int i = 0; i < FIELD_HEIGHT; i++) {
                    for (int j = 0; j < FIELD_WIDTH; j++) {
                        if (field[i][j] == 1) {
                            field[i][j] = 0;
                        }
                    }
                }
                
                for (int i = 0; i < current.length; i++) {
                    for (int j = 0; j < current[i].length; j++) {
                        if (current[i][j] == 1) {
                            field[y + i][x + j] = 1;
                            if (fix) {
                                field[y + i][x + j] = 2;
                            }
                        }
                    }
                }
                
                // メインフィールドを描画
                drawBlocks(g);
                
                if (fix) {
                    mode = 2;
                }
                if (count == 0) {
                    sleep(150);
                } else {
                    sleep(50);
                }
                count++;
            } else if (mode == 2) {
                mode = 0;
            }
            canvas.repaint();
            canvas.serviceRepaints();
        }
    }
    
    void copy(int[][] src, int[][] dest) {
        for (int i = 0; i < src.length; i++) {
            System.arraycopy(src[i], 0, dest[i], 0, src[i].length);
        }
    }
    
    void rotate(int[][] omino) {
        int[][] temp = new int[omino[0].length][omino[0].length];
        temp[0][2] = omino[0][0];
        temp[1][2] = omino[0][1];
        temp[2][2] = omino[0][2];
        temp[0][1] = omino[1][0];
        temp[1][1] = omino[1][1];
        temp[2][1] = omino[1][2];
        temp[0][0] = omino[2][0];
        temp[1][0] = omino[2][1];
        temp[2][0] = omino[2][2];
        for (int i = 0; i < omino.length; i++) {
            System.arraycopy(temp[i], 0, omino[i], 0, temp[i].length);
        }
    }
    
    boolean isHit(int x, int y, int[][] omino) {
        for (int i = 0; i < omino.length; i++) {
            for (int j = 0; j < omino[i].length; j++) {
                if (omino[i][j] == 1) {
                    if (field[y + i][x + j] == -1 || field[y + i][x + j] == 2) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
    
    void initField() {
        for (int i = 0; i < FIELD_HEIGHT; i++) {
            for (int j = 0; j < FIELD_WIDTH; j++) {
                if (j == 0 || j == FIELD_WIDTH - 1) {
                    field[i][j] = -1;
                } else if (i == FIELD_HEIGHT - 1) {
                    field[i][j] = -1;
                } else {
                    field[i][j] = 0;
                }
            }
        }
        for (int i = 1; i < FIELD_WIDTH - 1; i++) {
            field[FIELD_HEIGHT - 1][i] = -1;
        }
        for (int i = 0; i < FIELD_HEIGHT; i++) {
            field[i][0] = -1;
            field[i][FIELD_WIDTH - 1] = -1;
        }
    }
    
    void drawBlocks(Graphics g) {
        int blockSize = canvas.getHeight() / (FIELD_HEIGHT + 2);
        for (int i = 0; i < FIELD_HEIGHT; i++) {
            for (int j = 0; j < FIELD_WIDTH; j++) {
                if (field[i][j] == -1) {
                    g.setColor(COLOR_GRAY);
                    g.fillRect(j * blockSize, i * blockSize, blockSize, blockSize);
                } else if (field[i][j] == 1) {
                    g.setColor(COLOR_RED);
                    g.fillRect(j * blockSize, i * blockSize, blockSize, blockSize);
                } else if (field[i][j] == 2) {
                    g.setColor(COLOR_BLUE);
                    g.fillRect(j * blockSize, i * blockSize, blockSize, blockSize);
                } else {
                    g.setColor(COLOR_BLACK);
                    g.fillRect(j * blockSize, i * blockSize, blockSize, blockSize);
                }
            }
        }
    }
    
    void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            // 例外は発生しない
        }
    }
    
}