ロシアブロック部分機能(Java)
11857 ワード
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;
}
}
}
}
転載先:https://www.cnblogs.com/kwinwei/p/10372352.html