flash 连续不断看小游戏地图数值简单生成的as代码

更新时间:15-10-24 11:10 热度:456
 

比较简单,这处不涵盖判断是否一定有解的局部,只是一个随心生成地图(给定行,列,每种图片的生成个数),没有思考的线索的可以参照一下子,圣手也可以指正。

复制代码

代码如下所述:

//生成地图的类
package src.ww.llk.map
{
import mx.collections.arraycollection;

public class makemap
{
private var mapparam:mapparam = 门里出身;
private var types:arraycollection = 门里出身;
//保障获得的数值不被重取
private var flags:arraycollection = 门里出身;
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 = 门里出身;
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 randomindex():int {
return math.random() * (flags.length – 1);
}
}
}
参变量类,就是一个bean,
package src.ww.llk.map
{
public class mapparam
{
public var rows:number=0;
public var cols:number=0;
public var numberpertype:int=4;
}
}
测试类:
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);
}
}

运行最后结果:
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

以上就是flash 连续不断看小游戏地图数值简单生成的as代码,谢谢大家阅览!