public class TetrominoGame {
//测试
public static void main(String[] args) {
//构造T类
TetrominoT t = new TetrominoT(0,4);
printTetrominoGame(t);
//构造J类
TetrominoJ j = new TetrominoJ(0,4);
printTetrominoGame(j);
}
//打印场地
public static void printTetrominoGame(Tetromino tetromino) {
int totalRows = 20;
int totalCols = 10;
Cell[] cells = tetromino.cells;
for(int row=0;row<totalRows;row++) {
for(int col=0;col<totalCols;col++) {
boolean isInCells = false;
for(int i=0;i<cells.length;i++) {
if(row == cells[i].row && col == cells[i].col) {
System.out.println("*");
isInCells = true;
break;
}
}
}
System.out.println();
}
}
}
class Tetromino {
Cell[] cells;
public Tetromino() {
cells = new Cell[4];
}
//打印
public void print() {
String str = "";
for(int i=0;i<cells.length;i++) {
str += "(" + cells[i].getCellInfo() + ")";
}
System.out.println(str);
}
//下落
public void drop() {
for(int i=0;i<cells.length;i++) {
cells[i].row++;
}
}
//左移
public void leftMove() {
for(int i=0;i<cells.length;i++) {
cells[i].col--;
}
}
//右移
public void rightMove() {
for(int i=0;i<cells.length;i++) {
cells[i].col++;
}
}
}
//T类
class TetrominoT extends Tetromino{
Cell[] cells;
//构造方法
public TetrominoT(int row,int col) {
super();
cells = new Cell[4];
cells[0] = new Cell(row,col);
cells[1] = new Cell(row,col + 1);
cells[2] = new Cell(row,col + 2);
cells[3] = new Cell(row + 1,col);
}
}
//J类
class TetrominoJ extends Tetromino {
Cell[] cells;
//构造方法
public TetrominoJ(int row,int col) {
super();
cells = new Cell[4];
cells[0] = new Cell(row,col);
cells[1] = new Cell(row,col + 1);
cells[2] = new Cell(row,col + 2);
cells[3] = new Cell(row + 1,col);
}
}
//格子
class Cell {
int row;
int col;
public Cell(int row,int col) {
this.row = row;
this.col = col;
}
public String getCellInfo() {
return row + "," + col;
}
}
这是源代码
这是错误信息
请各位帮我调一调
cells[i].row跟cells[i].col为空,取不出来值
主要是你的printTetrominoGame的第22行有错,仔细看看