package OO.day01;
public class TetrisCell {
int totalRow = 20;
int totalcol = 10;
//定义横宽
int row;
int col;
//下降
public void drop(int d) {
row +=d;
}
//上升
public void up(int d) {
row -=d;
}
//左移
public void moveleft(int d) {
col-=d;
}
//右移
public void moveright(int d) {
col += d;
}
//输出格子的位置信息
public String getCellInfo() {
return row + "," + col ;
}
}
package OO.day01;
import java.util.Scanner;
/**
* 实现功能:
* 用户输入0表示结束,退出
* 用户输入1表示上落一格,并重新打印cell
* 用户输入2表示下落一格,并重新打印cell
* 用户输入3表示左移一格,并重新打印cell
* 用户输入4表示右移一格,并重新打印cell
* */
public class TetrisTest {
public static void main(String[] args) {
System.out.println("********欢迎玩CellGame********");
System.out.println("游戏规则如下:");
System.out.println("用户输入1表示上落一格,并重新打印cell;");
System.out.println("用户输入2表示下落一格,并重新打印cell;");
System.out.println("用户输入3表示左移一格,并重新打印cell;");
System.out.println("用户输入4表示右移一格,并重新打印cell;");
System.out.println("用户输入0表示结束,退出。");
System.out.println();
System.out.println("*********开始游戏:********");
TetrisCell cell = new TetrisCell();
cell.row = 0;
cell.col = 0;
printCell(cell);
System.out.println("当前坐标:" + "(" + cell.row + "," + cell.col + ")");
Scanner s = new Scanner(System.in);
int num = 0;
while(true) {
//输入0退出游戏
System.out.println("请输入");
num = s.nextInt();
if(num == 0) {
GameCell(num,cell);
break;
}
//实现移动
GameCell(num,cell);
}
s.close();
}
/**绘制表格并显示当前坐标
* 传递的参数为: 抽象数据类型 TetrisCell cell
* **/
public static void printCell(TetrisCell cell) {
System.out.println("********绘制Cell********");
//打印场地
for(int row=0; row< cell.totalRow;row++) {
System.out.print(row + "\t");
for(int col=0; col< cell.totalcol;col++) {
if(cell.row == row && cell.col ==col) {
System.out.print("*");
}else {
System.out.print("-");
}
}
System.out.println();
}
}
/**根据数字移位
* 传递的参数为: int类型 和 抽象数据类型 TetrisCell cell
* **/
public static void GameCell(int num, TetrisCell cell) {
switch(num) {
//退出
case 0:{
System.out.println("欢迎下次再玩,已退出!");
break;
}
//上升一格
case 1:{
cell.up(1);
//判断是否超出边界,若超出边界则坐标不变
if(cell.row<0 || cell.col <0 ||cell.row > (cell.totalRow-1) || cell.col > (cell.totalcol-1) ) {
System.out.println("超出显示边界,无效操作!");
cell.drop(1);
}
printCell(cell);
System.out.println("当前坐标:" + "(" + cell.row + "," + cell.col + ")");
break;
}
//下降一格
case 2:{
cell.drop(1);
//判断是否超出边界,若超出边界则坐标不变
if(cell.row<0 || cell.col <0 ||cell.row > (cell.totalRow-1) || cell.col > (cell.totalcol-1)) {
System.out.println("超出显示边界,无效操作!");
cell.up(1);
}
printCell(cell);
System.out.println("当前坐标:" + "(" + cell.row + "," + cell.col + ")");
break;
}
//左移一格
case 3:{
cell.moveleft(1);
//判断是否超出边界,若超出边界则坐标不变
if(cell.row<0 || cell.col <0 ||cell.row > (cell.totalRow-1) || cell.col > (cell.totalcol-1)) {
System.out.println("超出显示边界,无效操作!");
cell.moveright(1);
}
printCell(cell);
System.out.println("当前坐标:" + "(" + cell.row + "," + cell.col + ")");
break;
}
//右移一格
case 4:{
cell.moveright(1);
//判断是否超出边界,若超出边界则坐标不变
if(cell.row<0 || cell.col <0 ||cell.row > (cell.totalRow-1) || cell.col > (cell.totalcol-1)) {
System.out.println("超出显示边界,无效操作!");
cell.moveleft(1);
}
printCell(cell);
System.out.println("当前坐标:" + "(" + cell.row + "," + cell.col + ")");
break;
}
default:
{System.out.println("Error! 请重新输入!");
break;
}
}
}
}