javaoop-tetris

Cell

package com.oop.tetris;

/**
 * 使用行号row和列号col,就可以描述出来一种类型:单元格
 */
public class Cell {
    private int row;
    private int col;
    public Cell(int row, int col) {
        this.row = row;
        this.col = col;
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getCol() {
        return col;
    }

    public void setCol(int col) {
        this.col = col;
    }

    public void down() {
        this.row++;
    }
    public void left() {
        this.col--;
    }
    public void right() {
        this.col++;
    }
}

Shape

package com.oop.tetris;

/**
 * 定义各种形状的父类型,因此每一种形状都有四个单元格
 * 这些形状都有向下,向左,向右的行为
 */
public class Shape {
    //共同特征:四个单元格
    private Cell[] cells;

    //因为父类型不知道每一种子类型的单元格的位置,因此不需要设置
    public Shape(){
        cells = new Cell[4];
    }

    public Cell[] getCells() {
        return cells;
    }

    public void setCells(Cell[] cells) {
        this.cells = cells;
    }

    //整个形状向下,其实就是每个单元格向下
    public void down(){
        for (int i = 0; i < cells.length; i++){
            cells[i].down();
        }
    }
    //整个形状向左,其实就是每个单元格向左
    public void left(){
        for (int i = 0; i < cells.length; i++){
            cells[i].left();
        }
    }
    //整个形状向右,其实就是每个单元格向右
    public void right(){
        for (int i = 0; i < cells.length; i++){
            cells[i].right();
        }
    }
}

Tetris

package com.oop.tetris;

public class Tetris {
    Cell[] cells;
    public Tetris() {
        cells =new Cell[4];
    }
    public static void printT(Tetris tetris){
        for (int i = 0; i <10 ; i++) {
            for (int j = 0; j <10 ; j++) {
                int row = i;
                int col = j;
                boolean f = true;
                for (Cell cell : tetris.cells) {
                    if(cell.getRow() ==row && cell.getCol() ==col){
                        f = false;
                    }
                }
                if(f){
                    System.out.print("*");
                }else{
                    System.out.print("#");
                }
            }
            System.out.println();
        }
    }
}

TetrisTest

package com.oop.tetris;

import java.util.Scanner;

public class TetrisTest {
    public static void main(String[] args) {
        int num = (int)(Math.random()*7);
        Shape s  = null;
        switch (num) {
            case 0: s = new T();break;
            case 1: s = new S();break;
            case 2: s = new O();break;
            case 3: s = new J();break;
            case 4: s = new I();break;
            case 5: s = new Z();break;
            case 6: s = new L();
        }
        printShape(s);
        //
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入 a(向左),s(向下),d(向右),q(退出)");
            String input = scanner.nextLine();
            if(input.equalsIgnoreCase("a")) {
                s.left();
            }else if(input.equalsIgnoreCase("s")) {
                s.down();
            }else if(input.equalsIgnoreCase("d")) {
                s.right();
            }else if(input.equalsIgnoreCase("q")) {
                break;
            }
            printShape(s);
        }
        System.out.println("------------------------");
    }
    public static void printShape(Shape s){
        for (int i = 0; i <10 ; i++) {
            for (int j = 0; j <10 ; j++) {
                int row = i;
                int col = j;
                boolean f = false;
                for (int k = 0; k < s.getCells().length; k++) {
                    Cell cell = s.getCells()[k];
                    if(row==cell.getRow() && col==cell.getCol()){
                        f = true;
                    }
                }
                if(f){
                    System.out.print("#");
                }else{
                    System.out.print("*");
                }
            }
            System.out.println();
        }
    }
}

I

package com.oop.tetris;

public class I extends Shape {
    public I(){
        //获取自己的四个单元格
        Cell[] cells = getCells();
        cells[0] = new Cell(0,3);
        cells[1] = new Cell(0,4);
        cells[2] = new Cell(0,5);
        cells[3] = new Cell(0,6);
    }
}

J

package com.oop.tetris;

public class J extends Shape {
    public J(){
        //获取自己的四个单元格
        Cell[] cells = getCells();
        cells[0] = new Cell(0,4);
        cells[1] = new Cell(0,5);
        cells[2] = new Cell(0,6);
        cells[3] = new Cell(1,6);
    }
}

L

package com.oop.tetris;

public class L extends Shape {
    public L(){
        //获取自己的四个单元格
        Cell[] cells = getCells();
        cells[0] = new Cell(0,4);
        cells[1] = new Cell(0,5);
        cells[2] = new Cell(0,6);
        cells[3] = new Cell(1,4);
    }
}

O

package com.oop.tetris;

public class O extends Shape {
    public O(){
        //获取自己的四个单元格
        Cell[] cells = getCells();
        cells[0] = new Cell(0,4);
        cells[1] = new Cell(0,5);
        cells[2] = new Cell(1,4);
        cells[3] = new Cell(1,5);
    }
}

S

package com.oop.tetris;

public class S extends Shape {
    public S(){
        //获取自己的四个单元格
        Cell[] cells = getCells();
        cells[0] = new Cell(0,5);
        cells[1] = new Cell(0,6);
        cells[2] = new Cell(1,4);
        cells[3] = new Cell(1,5);
    }
}

T

package com.oop.tetris;

public class T extends Shape {
    public T(){
        //获取自己的四个单元格
        Cell[] cells = getCells();
        cells[0] = new Cell(0,4);
        cells[1] = new Cell(0,5);
        cells[2] = new Cell(0,6);
        cells[3] = new Cell(1,5);
    }
}

Z

package com.oop.tetris;

public class Z extends Shape {
    public Z(){
        //获取自己的四个单元格
        Cell[] cells = getCells();
        cells[0] = new Cell(0,4);
        cells[1] = new Cell(0,5);
        cells[2] = new Cell(1,5);
        cells[3] = new Cell(1,6);
    }
}

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值