比如連連看的數據生成。
比較簡單,這裡不包括判斷是否一定有解的部分,只是一個隨意生成地圖(給定行,列,每種圖片的生成個數),沒有思路的可以參考一下,高手也可以指正。
//生成地圖的類
package src.ww.llk.map
{
import mx.collections.ArrayCollection;
public class MakeMap
{
private var mapParam:MapParam = null;
private var types:ArrayCollection = null;
//保證取得的數據不被重取
private var flags:ArrayCollection = null;
public function MakeMap(param:MapParam):void {
mapParam = param;
initData();
}
// 初始化必要的數據
private function initData():void {
types = new ArrayCollection();
var typeNum:int = Math.floor(mapParam.cols*mapParam.rows/mapParam.numberPerType);
for(var i:int=1;i<=typeNum;i++) {
for (var j:int=0;j<mapParam.numberPerType;j++) {
types.addItem(i);
}
}
var yushu:int = mapParam.cols*mapParam.rows - typeNum*mapParam.numberPerType;
for (var yI:int = 0; yI<yushu;yI++) {
types.addItem(1);
}
}
//隨機生成地圖
public function make():Array {
flags = new ArrayCollection();
for(var i:int=0;i<types.length;i++){
flags.addItem(i);
}
var mapData:Array = new Array();
var rowData:Array = null;
var col:int = 0;
rowData = new Array();
rowData.push(0);
for(col = 0;col<mapParam.cols;col++){
rowData.push(0);
}
rowData.push(0);
mapData.push(rowData);
for(var row:int = 0;row<mapParam.rows;row++){
rowData = new Array();
rowData.push(0);
for(col = 0;col<mapParam.cols;col++){
rowData.push(getType());
}
rowData.push(0);
mapData.push(rowData);
}
rowData = new Array();
rowData.push(0);
for(col = 0;col<mapParam.cols;col++){
rowData.push(0);
}
rowData.push(0);
mapData.push(rowData);
return mapData;
}
// 隨機取得單個數據
private function getType():int {
var ran:int=-1;
var ret:int = 0;
ran = randomIndex();
var index:int = int(flags.getItemAt(ran));
flags.removeItemAt(ran);
ret = int(types.getItemAt(index));
return ret;
}
private function randomInde