1 Packageoo.day04;2 //Lattice class3 Public classCell {4 5 intRow//Line number6 intCol//column number7 8Cell (intRowintCol) {9 This. Row =Row;Ten This. Col =Col; One } ACell (intN) { - This(n,n); - } the Cell () { - This(0,0); - } - + voidDrop () {//Drop 1 Grid -row++; + } A voidMoveLeft (intN) {//shift left N grid atcol-=N; - } -String Getcellinfo () {//get grid row numbers and column numbers - returnrow+ "," +Col; - } - in voidDropintN) {//Drop n Lattice -row+=N; to } + voidMoveLeft () { -col--; the } * $}
1 Packageoo.day04;2 //Type J----Sub-class3 Public classJextendsTetromino {4 J () {5 This(0,0);6 }7JintRowintCol) {8Cells[0] =NewCell (row,col);9CELLS[1] =NewCell (row,col+1);TenCELLS[2] =NewCell (row,col+2); OneCELLS[3] =NewCell (row+1,col+2); A } - - voidPrint () {//rewrite theSystem.out.println ("I am a J"); - Super. Print (); - } -}
1 Packageoo.day04;2 //T-type----sub-class3 Public classTextendsTetromino {4 T () {5 This(0,0);6 }7TintRowintCol) {8 Super();//by default, the constructor method of calling the parent class9 This. cells[0] =NewCell (row,col);Ten This. cells[1] =NewCell (row,col+1); One This. cells[2] =NewCell (row,col+2); A This. cells[3] =NewCell (row+1,col+1); - } - the voidPrint () {//rewrite -System.out.println ("I am a T"); - Super. print ();//Call the Print method of the parent class - } +}
1 Packageoo.day04;2 //Four panel----parent class3 Public classTetromino {4Cell[] cells;//Lattice Array5 Tetromino () {6Cells =NewCell[4];7 }8 9 voidDrop () {//whereaboutsTen for(inti=0;i<cells.length;i++){ Onecells[i].row++; A } - } - voidMoveLeft () {//Move left the for(inti=0;i<cells.length;i++){ -cells[i].col--; - } - } + voidMoveRight () {//Move Right - for(inti=0;i<cells.length;i++){ +cells[i].col++; A } at } - voidPrint () {//output grid coordinates - for(inti=0;i<cells.length;i++){ -String str =cells[i].getcellinfo (); - System.out.println (str); - } in } -}
1 Packageoo.day04;2 //Class T, J Test Class3 Public classTjtest {4 Public Static voidMain (string[] args) {5 /*6 Tetromino O1 = new T (2,5);//Upward styling7 Printcell (O1);//styling, re-passing value8 9 J O2 = new J (2,5);Ten Printcell (O2);//simultaneous modeling of values One */ A -T t =NewT (2,5); - t.print (); the -J J =NewJ (3,4); - j.print (); - + } - + //playing the Wall + playing T-type A Public Static voidPrintcell (Tetromino tt) { at for(inti=0;i<20;i++){ - for(intj=0;j<10;j++){ - - BooleanFlag =true;//1. Suppose to hit- - for(intk=0;k<tt.cells.length;k++){ - if(Tt.cells[k].row==i && tt.cells[k].col==j) { inFlag =false;//2. Play * - Break; to } + } - if(flag) {//3. Judging theSystem.out.print ("-"); *}Else{ $System.out.print ("*");Panax Notoginseng } - the } + System.out.println (); A } the } + -}
Print the case code for the T,J type