Look at the small game map data simply generated as code

Source: Internet
Author: User

Core tip:Flash tutorial, see the small game map data simply generated as code.

such as the data generation of repeatedly looking.

Relatively simple, here does not include the judge whether there is a certain part of the solution, just a random generation map (given rows, columns, the number of each image generated), there is no way to refer to the idea, the master can also be corrected.

Classes that generate maps

Package Src.ww.llk.map

{

Import mx.collections.ArrayCollection;

   

public class Makemap

{

private var mapparam:mapparam = null;

private var types:arraycollection = null;

Ensure that the data obtained is not removed

private var flags:arraycollection = null;

Public Function Makemap (param:mapparam): void {

Mapparam = param;

InitData ();

}

       

Initialize the necessary data

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);

}

}

       

Randomly generated maps

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;

}

       

Random access to a single data

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);

}

}

}

Parameter class, which is a bean,

Package Src.ww.llk.map

{

public class Mapparam

{

public Var rows:number=0;

public Var cols:number=0;

public Var numberpertype:int=4;

}

}

Test class:

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);

}

}

Run Result:

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







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.