比如連連看的數(shù)據(jù)生成。
比較簡(jiǎn)單,這里不包括判斷是否一定有解的部分,只是一個(gè)隨意生成地圖(給定行,列,每種圖片的生成個(gè)數(shù)),沒(méi)有思路的可以參考一下,高手也可以指正。
//生成地圖的類
package src.ww.llk.map
{
import mx.collections.ArrayCollection;
public class MakeMap
{
private var mapParam:MapParam = null;
private var types:ArrayCollection = null;
//保證取得的數(shù)據(jù)不被重取
private var flags:ArrayCollection = null;
public function MakeMap(param:MapParam):void {
mapParam = param;
initData();
}
// 初始化必要的數(shù)據(jù)
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 types.addItem(i); } } var yushu:int = mapParam.cols*mapParam.rows - typeNum*mapParam.numberPerType; for (var yI:int = 0; yI types.addItem(1); } } //隨機(jī)生成地圖 public function make():Array { flags = new ArrayCollection(); for(var i:int=0;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 rowData.push(0); } rowData.push(0); mapData.push(rowData); for(var row:int = 0;row rowData = new Array(); rowData.push(0); for(col = 0;col rowData.push(getType()); } rowData.push(0); mapData.push(rowData); } rowData = new Array(); rowData.push(0); for(col = 0;col rowData.push(0); } rowData.push(0); mapData.push(rowData); return mapData; } // 隨機(jī)取得單個(gè)數(shù)據(jù) 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 randomIndex():int { return Math.random() * (flags.length - 1); } } } 參數(shù)類,就是一個(gè)bean, package src.ww.llk.map { public class MapParam { public var rows:Number=0; public var cols:Number=0; public var numberPerType:int=4; } } 測(cè)試類: public function test():void { var param:MapParam = new MapParam(); param.rows = 6; param.cols = 6; param.numberPerType = 4; var maker:MakeMap = new MakeMap(param); var map:Array = maker.make(); for each(var rowData:Array in map) { var rowStr:String = ""; for each(var cell:int in rowData) { rowStr += cell + " "; } trace(rowStr); } } 運(yùn)行結(jié)果: 0 0 0 0 0 0 0 0 0 3 3 1 1 7 6 0 0 6 8 9 7 9 5 0 0 2 4 4 4 1 7 0 0 5 8 4 9 5 8 0 0 2 6 2 3 1 7 0 0 6 2 5 3 8 9 0 0 0 0 0 0 0 0 0
2015年全國(guó)職稱計(jì)算機(jī)考試教材(2007模 .. 定價(jià):¥225 優(yōu)惠價(jià):¥213 更多書(shū)籍 | |
全國(guó)職稱計(jì)算機(jī)考試速成過(guò)關(guān)系列套裝:W .. 定價(jià):¥133 優(yōu)惠價(jià):¥133.0 更多書(shū)籍 |