package box;
import java.util.Scanner;
public class TerominGame {
public static void main(String[] args){
J j=new J(0,4);//新建一个J类对象
j.printJ();//利用J类print函数输出J类测试数据
T t=new T(0,4);//新建一个T类对象
t.printT();//利用T类print函数输出T类测试数据
Scanner sc=new Scanner(System.in);
int c=0;
do{
printTerominGame(t);//输出t对象的测试结果
System.out.println("1——下落,2——向左,3——向右,0——退出");
c=sc.nextInt();
}while(c!=0);
}
public static void printTerominGame( Teromin t){//用于输出测试结果
Cell[] cells=t.cells;//新建一个数组类型的对象,用于存储四个格子的坐标
final int TOTALROW=20;//定义一个常量表示格子总共有20行
final int TOTALCOL=10;//表示有十列
one:for(int i=0;i
{
two:for(int j=0;j
{
there:for(int k=0;k
{
if(i==cells[k].row&&j==cells[k].col)
{
System.out.print("*");
break there;//跳出there这层for循环
}
}
System.out.print("-");
}
System.out.println();
}
}
}
class Cell{//存储格子坐标的类
int row;//行
int col;//列
Cell(){
}
public Cell(int row, int col) {
this.row = row;
this.col = col;
}
public String getCellInfo(){
return row+","+col;
}
}
class Teromin{
Cell[] cells;
public Teromin(){//父类构造函数用于新建一个长度为4的数组类型对象
cells=new Cell[4];
}
public void printTeromin(){//输出测试坐标
String temp="";
for(int i=0;i
{
temp=temp+"("+cells[i].getCellInfo()+")"+",";
}
temp=temp+"("+cells[cells.length-1].getCellInfo()+")";
System.out.println(temp);
}
public void drop(){//实现格子下落的方法
for(int i=0;i
{
cells[i].row--;
}
}
public void moveRight(){//实现格子右移的方法
for(int i=0;i
{
cells[i].col++;
}
}
public void moveLeft(){//实现格子右移的方法
for(int i=0;i
{
cells[i].col--;
}
}
}
class T extends Teromin{
public T(int row,int col){//子类构造函数
super();//用于表示父类构造函数
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+1);
}
public void printT(){
System.out.println("i am a T");
super.printTeromin();//在子类中引用父类的函数
}
}
class J extends Teromin{//与T类同理
public J(int row,int col){
super();
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+2);
}
public void printJ(){
System.out.println("i am a J");
super.printTeromin();
}
}