2D葫蘆娃v1
Main函數(shù)(上帝之手)
上帝先創(chuàng)造世界:一個20*20的方形舞臺竖配。
public static void main(String args[]){
int N = 20;
Stage stage = new Stage(N);
然后創(chuàng)造生物:7個葫蘆兄弟brothers
虾标、12只小妖怪們monsters
另绩、1個爺爺grandpa
和1只蛇精snake
爆班。
//Create Huluwa Brothers
HuLuWa[] brothers = new HuLuWa[7];
for (int i = 0; i<brothers.length; i++){
brothers[i] = new HuLuWa(RANK.values()[i], COLOR.values()[i]);
}
//Create Monsters
Creature[] monsters = new Creature[12];
for ( int i = 0; i < monsters.length; i++){
if ( i == 0 ) monsters[i] = new Scorpion();
else{
monsters[i] = new LouLuo();
}
}
//Create Grandpa
Grandpa grandpa = new Grandpa();
//Create Snake
Snake snake= new Snake();
上帝將世界舞臺的左半側(cè)劃分給葫蘆兄弟陣營甚颂,讓他們排成長蛇陣型蜜猾;然后,將世界右半側(cè)劃分給妖怪陣營振诬,讓小妖怪們以蝎子精為首排成箭陣蹭睡。
CreatureQueue hulu_q = new CreatureQueue(brothers, stage.get_space(), 1, N/2-1, 0, N);
CreatureQueue mst_q = new CreatureQueue(monsters, stage.get_space(), N-2,N/2, 0, N);
Pattern ls_p= new LongSnakePattern();
ls_p.Reform(hulu_q);
Pattern arrow_p = new ArrowPattern();
arrow_p.Reform(mst_q);
上帝把爺爺和蛇精放在各自勢力范圍的最后方,讓他們加油鼓勁贷揽。
grandpa.set_pos(stage.get_space()[N/2][0]);
snake.set_pos(stage.get_space()[N/2][N-1]);
展示舞臺棠笑,上帝覺得不是很滿意。
stage.printStage();
于是讓妖怪們又變換成了新月陣型禽绪,展示舞臺蓖救,上帝覺得很滿意。
Pattern crescent_p = new CrescentPattern();
crescent_p.Reform(mst_q);
stage.printStage();
位置相關(guān)類
1. Stage
舞臺類
- 成員變量
Position[][] space
二維數(shù)組用于存儲程序運行的二維空間印屁,space[y][x]存儲坐標為(x,y)的Position - 成員函數(shù)
Stage(int N);
初始化舞臺
public void printStage();
打印舞臺
2. Position
位置類
- 成員變量
private boolean avail;
private Creature holder;
private Coord coord;
Coord
類型有x循捺,y兩個成員,代表坐標
- 成員函數(shù)
public void put_in(Creature creature);
放入生物
public void clear_pos();
清空該位置
public Creature get_holder();
獲取當前坐標
public boolean get_avail();
獲取當前生物
public Coord get_coord();
獲取位置是否可用
生物相關(guān)類
1. 抽象類Creature
- 成員變量
protected Position pos
每個生物都有一個位置雄人,初始為空 - 成員函數(shù)
public boolean set_pos(Position p)
普通方法从橘,所有生物有關(guān)位置設(shè)置的方法都是相同的
public Position get_pos()
普通方法,獲得生物的位置
public abstract String show()
抽象方法础钠,顯示生物信息
public abstract String get_symbol()
抽象方法恰力,返回生物的代表符號,用于打印舞臺的顯示
2. 比較接口Comparable
public boolean BiggerThan(Comparable another);
對生物比較行為的抽象旗吁。
3. HuLuWa
葫蘆娃類
- 成員變量
private RANK rank;
private COLOR color;
在Creature
抽象類的基礎(chǔ)上踩萎,增加color
rank
屬性
- 成員函數(shù)
public boolean BiggerThan(Comparable another);
根據(jù)rank
比較大小
public String get_symbol();
代表符號為顏色,如紅
4. 其他生物子類
Scorpion
蝎子精類很钓,代表符號為蝎
Snake
蛇精類香府,代表符號為蛇
LouLuo
小嘍啰類,代表符號為嘍
Grandpa
爺爺類码倦,代表符號為爺
移動排隊類
1. CreatureQueue
生物隊列類
- 將一組生物對象集合起來企孩,主要作用使將生物類
Creature
與舞臺類Stage
聯(lián)結(jié)起來,是陣型變換的基本單位袁稽。 - 成員變量
private Position[][] space;
舞臺空間勿璃,初始化時將當前舞臺空間代入隊列之中
private Creature[] creatures;
一維數(shù)組,代表被操作生物隊列
private int bound_x1;
private int bound_x2;
private int bound_y1;
private int bound_y2;
規(guī)定生物隊列可活動的矩形范圍, bound_x1 < bound_x2
則代表隊列頭部朝向右側(cè)补疑,隊列在舞臺左半場活動闻葵;反之頭部朝向左側(cè),在舞臺右半場活動
- 成員函數(shù)
主要定義了有關(guān)獲取生物數(shù)組癣丧、舞臺空間槽畔、范圍等屬性的方法
public Creature[] get_creatures();
public Position[][] getSpace();
public int getBound_x1();
public int getBound_x2()
public int getBound_y1();
public int getBound_y2();
2. 抽象類Pattern
- 對于排列陣型行為的抽象
- 成員函數(shù)
public void Reform(CreatureQueue queue);
普通方法胁编,對一個生物隊列進行陣型排列厢钧;調(diào)用get_basic_pattern(int num)
獲得num
大小的用Coord
一維數(shù)組表示基本陣型,再根據(jù)queue
的活動范圍等嬉橙,在對應(yīng)的舞臺空間上移動生物早直,排列陣型。
public abstract Coord[] get_basic_pattern(int num)
抽象方法市框,根據(jù)num
大小生成對應(yīng)的基本陣型霞扬,返回值為坐標Coord
的一維數(shù)組,下標為0的位置對應(yīng)"將領(lǐng)(鞋子)"的坐標枫振。
2. 具體陣型子類
- 對于每一個陣型子類喻圃,需要定義不同的get_basic_pattern(int num)。
LongSnakePattern
長蛇陣
BalancePattern
衡軛陣
ArrowPattern
箭形陣
ScalePattern
魚鱗陣
SquarePattern
方陣
WingPattern
翼陣
EchelonPattern
雁形陣
CrescentPattern
偃月陣