java基础俄罗斯方块加强版

来源:互联网 发布:java 换行符替换 编辑:程序博客网 时间:2024/05/22 14:41
<span style="font-size:18px;"><strong>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<TOTALROW;i++){two:for(int j=0;j<TOTALCOL;j++){there:for(int k=0;k<cells.length;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<cells.length-1;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.length;i++){cells[i].row--;}}public void moveRight(){//实现格子右移的方法for(int i=0;i<cells.length;i++){cells[i].col++;}}public void moveLeft(){//实现格子右移的方法for(int i=0;i<cells.length;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();}}</strong></span>

0 0
原创粉丝点击