16Java面向对象-------针对15的代码练习

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_44787898/article/details/102645775

格子类

package oo.day02;
//格子类
public class Cell {
	int row;//行号
	int col;//列号
	Cell(){
		this(0);
	}
	Cell(int n){
		this(n, n);
	}
	Cell(int row,int col){
		this.row=row;
		this.col=col;
	}
	void drop(){//下落一格
		row++;//行号增一
	}
	void moveleft(int n) {//左移n格
		col-=n;//列号减一
	}
	String getCellInfo() {//获取行号和列号
		return row+","+col;//返回行列号
	}
    void drop(int n) {
    	row+=n;
    }
    void moveLeft() {
    	col--;
    }
}

格子类测试类

package oo.day02;
//格子类的测试类
public class CellTest {

	public static void main(String[] args) {
		Cell c=new Cell();
		Cell c2=new Cell(1);
		Cell c3=new Cell(1,1);
		printWall(c3);
		
	}
	  public static void printWall(Cell cc) {
		  for(int i=0;i<20;i++) {//行
				for (int j = 0; j<10; j++) {//列
					if(i==cc.row&&j==cc.col) {
						System.out.print("* ");
					}else {
						System.out.print("- ");
					}
				}
				System.out.println();//换行
			}  
	  }
}

T型类(俄罗斯方块中一种方块形状)

package oo.day02;
//T型
public class T {
	Cell[] cells;
	T(){
		this(0,0);
	}
	T(int row,int col){
	    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+1);
	}

	void drop(){  //下落
		for(int i=0;i<cells.length;i++){
			cells[i].row++;
		}
	}
	void moveLeft(){  //左移
		for(int i=0;i<cells.length;i++){
			cells[i].col--;
		}
	}
	void moveRight(){ //右移
		for(int i=0;i<cells.length;i++){
			cells[i].col++;
		}
	}
	void print(){ //打印每个格子的坐标
		for(int i=0;i<cells.length;i++){
			String str = cells[i].getCellInfo();
			System.out.println(str);
		}
	}
	
}

J型类

package oo.day02;
//J型
public class J {
	Cell[] cells;
	J(){
		this(0,0);
	}
	J(int row,int col){
		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+2);
	}

	void drop(){  //下落
		for(int i=0;i<cells.length;i++){
			cells[i].row++;
		}
	}
	void moveLeft(){  //左移
		for(int i=0;i<cells.length;i++){
			cells[i].col--;
		}
	}
	void moveRight(){ //右移
		for(int i=0;i<cells.length;i++){
			cells[i].col++;
		}
	}
	void print(){ //打印每个格子的坐标
		for(int i=0;i<cells.length;i++){
			String str = cells[i].getCellInfo();
			System.out.println(str);
		}
	}
	
}

T、J类的测试类

package oo.day02;
//T型、J型测试类
public class TJtest {
	public static void main(String[] args) {
		System.out.println("原始:");
		T t = new T(2,3);
		t.print();
		
		System.out.println("下落:");
		t.drop();
		t.print();
	}
}

猜你喜欢

转载自blog.csdn.net/qq_44787898/article/details/102645775