<small>
圖形下落
本案例要求完成 CellGame,用戶可以在控制臺上操作格子的下落择诈、左移和右移。
游戲剛開始出皇,將在界面上顯示一個(gè)格子羞芍,界面效果如圖-2上左圖中的藍(lán)色圈內(nèi)所示,用戶可以在控制臺選擇輸入各種操作:1表示下落一行郊艘,2表示左移一列荷科,3表示右移一列,0表示退出纱注。如果用戶錄入1畏浆,則格子下落一行,并重新打印顯示狞贱,界面效果如圖-2上右圖中的藍(lán)色圈內(nèi)所示:
如果用戶錄入2刻获,則格子左移一列,并重新打印顯示瞎嬉,界面效果如圖-3上左圖中藍(lán)色圈內(nèi)所示蝎毡;如果用戶錄入3厚柳,則格子右移一列,并重新打印顯示沐兵,界面如圖-3上右圖中藍(lán)色圈內(nèi)所示:
星星圖形的對象
package com.ksxx.oop.day03.am.exercise;
/**
* *類:
* 屬性:
* int x,y;//坐標(biāo)
String CHARACTER = "*";//組成元素
方法:
printCell()//打印
moveLeft()//左移
moveRight()//右移
drop()//下落
* @author chengcheng
*
*/
public class Cell {
//屬性:
int row,col;//坐標(biāo)
String CHARACTER = "* ";//組成元素
//無參構(gòu)造
public Cell(){
//默認(rèn)位置(第一行别垮,第五列)
// this.x = 0;
// this.y = 4;
this(0,4);
}
//有參構(gòu)造
public Cell(int row,int col){
//默認(rèn)位置(第一行,第五列)
this.row = row;
this.col = col;
}
//方法:
//打印
public void printCell(){
System.out.print(this.CHARACTER);
}
//右移:row行不變扎谎,col列每移動一次碳想,++一次
public void moveRight(){
col++;
}
}
墻對象:
package com.ksxx.oop.day03.am.exercise;
/**
* Wall墻面:
* 由 200個(gè)-號對象 組成 無意義
*
* 屬性:
* 多少行、多少列
* 組成元素
*
* 方法:
* 打印墻體
* @author chengcheng
*
*/
public class Wall {
//如下設(shè)計(jì)無意義
/*Line[][] lines;
public Wall(){
lines = new Line[20][10];
for(int i=0; i<lines.length; i++){//行:y
for(int j=0; j<lines[i].length; j++){//列:x
//矩陣中每個(gè) - 從出現(xiàn)開始簿透,坐標(biāo)就已經(jīng)確定移袍。
lines[i][j] = new Line(i,j);
}
}
}*/
//屬性:
int rows;//行數(shù)
int cols;//列數(shù)
String CHARACTER = "- ";//組成元素
Cell cell;//另一個(gè)組成元素
//無參構(gòu)造,默認(rèn)大小墻體
public Wall(){
// this.rows = 20;//20行
// this.cols = 10;//10列
this(20,10);
}
//有參構(gòu)造老充,自定義大小墻體葡盗,默認(rèn)cell位置
public Wall(int rows, int cols){
this.rows = rows;
this.cols = cols;
//默認(rèn)cell在墻體的位置
cell = new Cell();
}
//有參構(gòu)造,默認(rèn)大小墻體啡浊,自定義cell位置
public Wall(Cell cell){
this(20,10);
//自定義cell位置
this.cell = cell;
}
//有參構(gòu)造觅够,自定義大小墻體,自定義cell位置
public Wall(int rows, int cols, Cell cell){
this.rows = rows;
this.cols = cols;
//自定義cell位置
this.cell = cell;
}
//方法:
//墻體的打印巷嚣。
public void printWall(){
for(int i=0; i<rows; i++){//行:rows
for(int j=0; j<cols; j++){//列:cols
if(i == this.cell.row &&
j == this.cell.col){
System.out.print(this.cell.CHARACTER);
continue;
}
System.out.print(this.CHARACTER);
}
System.out.println();//換行
}
}
}
測試方法
package com.ksxx.oop.day03.am.exercise;
import java.util.Scanner;
public class GameStart_V2 {
public static void main(String[] args) {
//創(chuàng)建墻體對象
Wall wall = new Wall(new Cell(9,0));
wall.printWall();
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("3 - 向右喘先,0 - 退出");
int number = sc.nextInt();
if(number == 0){
//退出
break;
}else if(number == 3){
//右移:
//定位墻體中的cell元素,向右移:
wall.cell.moveRight();
}
//重繪
wall.printWall();
}
}
}
如果圖形由※號改為 T 形圖廷粒,可以將代碼修改如下
圖
package com.ksxx.oop.day05.am.exercise;
/**
* Wall墻面:
* 由 200個(gè)-號對象 組成 無意義
*
* 屬性:
* 多少行窘拯、多少列
* 組成元素
*
* 方法:
* 打印墻體
* @author chengcheng
*
*/
public class Wall {
//屬性:
int rows;//行數(shù)
int cols;//列數(shù)
String CHARACTER = "- ";//組成元素
// Cell cell;//另一個(gè)組成元素
T t;//組成元素由1個(gè)Cell變成4個(gè)Cell組成的T型。
//無參構(gòu)造坝茎,默認(rèn)大小墻體
public Wall(){
// this.rows = 20;//20行
// this.cols = 10;//10列
this(20,10);
}
//有參構(gòu)造涤姊,自定義大小墻體,默認(rèn)cell位置
public Wall(int rows, int cols){
this.rows = rows;
this.cols = cols;
//默認(rèn)T在墻體的位置
t = new T();
}
//有參構(gòu)造嗤放,默認(rèn)大小墻體思喊,自定義T位置
public Wall(Cell cell){
this(20,10);
//自定義cell位置
this.t = new T(cell);
}
//有參構(gòu)造,自定義大小墻體次酌,自定義t位置
public Wall(int rows, int cols, Cell cell){
this.rows = rows;
this.cols = cols;
//自定義t位置
this.t = new T(cell);
}
//方法:
//墻體的打印恨课。
public void printWall(){
//開關(guān)
/*boolean flag;
for(int i=0; i<rows; i++){//行:rows
for(int j=0; j<cols; j++){//列:cols
flag = true;//開關(guān)開啟
//對T型進(jìn)行打印
for(int k=0; k<t.cells.length; k++){
if(i == t.cells[k].row &&
j == t.cells[k].col){
System.out.print(t.cells[k].CHARACTER);
flag = false;//開關(guān)關(guān)閉
break;
}
}
if(flag){
System.out.print(this.CHARACTER);
}
}
System.out.println();//換行
}*/
for(int i=0; i<rows; i++){//行:rows
second:for(int j=0; j<cols; j++){//列:cols
//對T型進(jìn)行打印
for(int k=0; k<t.cells.length; k++){
if(i == t.cells[k].row &&
j == t.cells[k].col){
System.out.print(t.cells[k].CHARACTER);
continue second;
}
}
System.out.print(this.CHARACTER);
}
System.out.println();//換行
}
}
}
若要求發(fā)生改變?yōu)椋阂褕D形從 T 改變?yōu)?O、L岳服、J剂公、S、Z吊宋、I 等圖形的的隨機(jī)出現(xiàn)可以將代碼改為:
package com.ksxx.oop.day05.am.exercise;
import java.util.Random;
/**
* Wall墻面:
* 由 200個(gè)-號對象 組成 無意義
*
* 屬性:
* 多少行诬留、多少列
* 組成元素
*
* 方法:
* 打印墻體
* @author chengcheng
*
*/
public class Wall {
//屬性:
int rows;//行數(shù)
int cols;//列數(shù)
String CHARACTER = "- ";//組成元素
Tetramino newTetramino;//抽象的俄羅斯方塊
//無參構(gòu)造,默認(rèn)大小墻體
public Wall(){
// this.rows = 20;//20行
// this.cols = 10;//10列
this(20,10);
}
//有參構(gòu)造,自定義大小墻體文兑,默認(rèn)cell位置
public Wall(int rows, int cols){
this.rows = rows;
this.cols = cols;
newTetramino = randomTetramino();
}
//有參構(gòu)造,默認(rèn)大小墻體腺劣,自定義T位置
public Wall(Cell cell){
this(20,10);
this.newTetramino = randomTetramino(cell);
}
//有參構(gòu)造,自定義大小墻體籍铁,自定義t位置
public Wall(int rows, int cols, Cell cell){
this.rows = rows;
this.cols = cols;
this.newTetramino = randomTetramino(cell);
}
//方法:
//墻體的打印拒名。
public void printWall(){
//開關(guān)
/*boolean flag;
for(int i=0; i<rows; i++){//行:rows
for(int j=0; j<cols; j++){//列:cols
flag = true;//開關(guān)開啟
//對T型進(jìn)行打印
for(int k=0; k<t.cells.length; k++){
if(i == t.cells[k].row &&
j == t.cells[k].col){
System.out.print(t.cells[k].CHARACTER);
flag = false;//開關(guān)關(guān)閉
break;
}
}
if(flag){
System.out.print(this.CHARACTER);
}
}
System.out.println();//換行
}*/
for(int i=0; i<rows; i++){//行:rows
second:for(int j=0; j<cols; j++){//列:cols
//對T型進(jìn)行打印
for(int k=0; k<newTetramino.cells.length; k++){
if(i == newTetramino.cells[k].row &&
j == newTetramino.cells[k].col){
System.out.print(newTetramino.cells[k].CHARACTER);
continue second;
}
}
System.out.print(this.CHARACTER);
}
System.out.println();//換行
}
}
/**
* 隨機(jī)產(chǎn)生具體俄羅斯方塊
*/
private Tetramino randomTetramino(){
Random ran = new Random();
int number = ran.nextInt(3);
//返回隨機(jī)的俄羅斯方塊
//方式一:
// Tetramino[] tetris = {new T(),new J(),new L()};
// return tetris[number];
Tetramino tetramino = null;
//方式二
/*if(number == 0){
tetramino = new T();
}else if(number == 1){
tetramino = new J();
}else if(number == 2){
tetramino = new L();
}else{
tetramino = new L();
}*/
//方式三
switch(number){
case 0:
tetramino = new T();break;
case 1:
tetramino = new L();break;
case 2:
tetramino = new J();break;
default:
tetramino = new T();break;
}
return tetramino;
}
private Tetramino randomTetramino(Cell cell){
Random ran = new Random();
int number = ran.nextInt(3);
Tetramino tetramino = null;
switch(number){
case 0:
tetramino = new T(cell);break;
case 1:
tetramino = new L(cell);break;
case 2:
tetramino = new J(cell);break;
default:
tetramino = new T(cell);break;
}
return tetramino;
}
}