java基础俄罗斯方块加强版
2016-04-25 23:17
661 查看
<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>
相关文章推荐
- Java日期处理类
- Java中finalize()以及GC
- Spring之为什么要使用IOC
- Java内存泄漏分析与解决方案
- JAVA HashMap 实现原理
- java版斐波那契
- Java内存泄漏分析与解决方案
- 面包屑之java并发二
- java基本数据类型
- spring boot 在STS里将项目打包成war
- 有关java序列化问题
- java线程系列---Runnable和Thread的区别
- SpringMVC3.1+MyBatis3.2+Mysql5.5+ExtJS4.2实现角色权限管理系统
- Java集合排序
- Java实现AES256加解密
- 基于jdk和cglib的动态代理
- java进阶——类与对象
- SpringMVC Ajax 获取参数的方法
- SpringMVC环境下实现的Ajax异步请求(JSON格式数据) 推荐
- springMVC(4)------@RequestParam映射请求参数