ランダムなんだけど固定な迷路生成
ランダムに生成されるんだけど初期値(シード値)が同じであれば同じ迷路が生成される
参照(ほぼマルパクさせていただきました
http://www.mztm.jp/2009/06/08/random、線形合同法/
http://www40.atwiki.jp/spellbound/pages/282.html
/**
* Copyright fumix ( http://wonderfl.net/user/fumix )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/tMMV
*/
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextFieldType;
public class Index extends Sprite {
private var _base : BitmapData;
private var _seed : Label;
private var _button : Button;
// ----------------------------------------
// CLASS CONSTANTS
// ----------------------------------------
// VARIABLES
// ----------------------------------------
// METHODS
public function Index() : void {
if (stage) _initialize();
else addEventListener(Event.ADDED_TO_STAGE, _initialize);
}
/**
* _initialize
*/
private function _initialize(e : Event = null) : void {
removeEventListener(Event.ADDED_TO_STAGE, _initialize);
//迷路ベース
_base = new BitmapData(9, 9, false);
var bitmap : Bitmap = new Bitmap(_base);
bitmap.scaleX = bitmap.scaleY = 40;
//UI
_seed = new Label('123456',TextFieldType.INPUT,100,true);
_button = new Button('CREATE',50);
var layout1:LayoutWidth = new LayoutWidth();
layout1.addChild(_seed);
layout1.addChild(_button);
var layout2:LayoutHeight = new LayoutHeight(10);
layout2.addChild(layout1);
layout2.addChild(bitmap);
layout2.x = Math.floor(stage.stageWidth /2 - layout2.width / 2);
layout2.y = Math.floor(stage.stageHeight /2 - layout2.height / 2);
addChild(layout2);
_button.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
boutaoshi();
}
private function onMouseUp(event : MouseEvent) : void {
boutaoshi();
}
/**
* 棒倒し法で迷路作成
*/
private function boutaoshi() : void {
//シード値をいれて乱数を固定?
Mas.randomSeed(Number(_seed.text));
// 格子状のベースを作る
for (var y : int = 0; y < _base.height; y++) {
for (var x : int = 0; x < _base.width; x++) {
if (y == 0 || x == 00 || y == _base.height - 1 || x == _base.width - 1 || y % 2 == 0 && x % 2 == 0) {
_base.setPixel(x, y, 0x000000);
}else{
_base.setPixel(x, y, 0xFFFFFF);
}
}
}
// 棒倒し法でランダムに壁を作る
var dx : int;
var dy : int;
// 1列目
for (y = 2; y < _base.height - 1 ; y += 2) {
dx = 2;
dy = y;
// 基準点から上、下、左、右のいずれかに棒倒し
switch(int(Mas.random() * 4)) {
case 0:
dy++;
break;
case 1:
dy--;
break;
case 2:
dx++;
break;
case 3:
dx--;
break;
}
// 壁が既に設置されていない場合は追加。それ以外はやり直し
if (_base.getPixel(dx, dy) != 0x000000) {
_base.setPixel(dx, dy, 0x000000);
} else {
y -= 2;
}
}
// 2列目以降
for (x = 4; x < _base.width - 1; x += 2) {
for (y = 2; y < _base.height - 1 ; y += 2) {
dx = x;
dy = y;
// 基準点から上、下、右のいずれかに棒倒し
switch(int(Mas.random() * 3)) {
case 0:
dy++;
break;
case 1:
dy--;
break;
case 2:
dx++;
break;
}
// 壁が既に設置されていない場合は追加。それ以外はやり直し
if (_base.getPixel(dx, dy) != 0x000000) {
_base.setPixel(dx, dy, 0x000000);
} else {
y -= 2;
}
}
}
}
}
}
import flash.display.DisplayObject;
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFieldType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
/**
*線形合同法による乱数生成
**/
class Mas {
private static var num : Number = 1;
public static function randomSeed(s : Number) : void {
num = s;
for (var i : int = 0; i < 100; i++) {
random();
}
}
public static function random() : Number {
num = (num * 1664525 + 1013904223) % 0x100000000;
return num / 0x100000000;
}
}
/**
*以下UIまわり
**/
class Button extends SimpleButton {
public function Button(label : String, width : int = 0) : void {
var up : Sprite = _buildImage(label, 0x0, width);
var over : Sprite = _buildImage(label, 0x333333, width);
var down : Sprite = _buildImage(label, 0x333333, width);
down.y = 1;
super(up, over, down, up);
}
private static function _buildImage(label : String, color : int, width : int = 0) : Sprite {
var text : TextField = new TextField();
text.defaultTextFormat = new TextFormat('Verdana', 10, 0xffffff, true, null, null, null, null, TextFormatAlign.CENTER);
text.autoSize = TextFieldAutoSize.LEFT;
text.selectable = false;
text.text = label;
text.x = (width - text.width) >> 1;
text.y = 5;
var base : Shape = new Shape();
var g : Graphics = base.graphics;
g.beginFill(color);
g.drawRect(0, 0, width, text.height + 10);
g.endFill();
var sp : Sprite = new Sprite();
sp.addChild(base);
sp.addChild(text);
return sp;
}
}
class Label extends Sprite {
private var _text : TextField;
public function Label(label : String,type:String = TextFieldType.DYNAMIC, width : int = 0,border:Boolean = false,center:Boolean = false ) : void {
var up : Sprite = _buildImage(label, type,0x0, width,border,center);
addChild(up);
}
private function _buildImage(label : String, type:String,color : int, width : int = 0,border:Boolean = false,center:Boolean = false ) : Sprite {
_text = new TextField();
_text.defaultTextFormat = new TextFormat('Verdana', 14, 0x000000, true, null, null, null, null, TextFormatAlign.LEFT);
_text.type = type;
_text.autoSize = TextFieldAutoSize.LEFT;
if(center) _text.autoSize = TextFieldAutoSize.CENTER;
if(type == TextFieldType.DYNAMIC) _text.selectable = false;
_text.text = label;
if(type == TextFieldType.INPUT) {
var h:int = _text.height;
_text.wordWrap = false;
_text.autoSize = TextFieldAutoSize.NONE;
_text.width = width;
_text.height = h;
}
if(center) _text.x = (width - _text.width) >> 1;
_text.y = 3;
var base : Shape = new Shape();
var g : Graphics = base.graphics;
if(border) g.lineStyle(1,0x0);
g.beginFill(color,0);
g.drawRect(0, 0, width, _text.height + 6);
g.endFill();
var sp : Sprite = new Sprite();
sp.addChild(base);
sp.addChild(_text);
return sp;
}
public function get text() : String {
return _text.text;
}
public function set text(text : String) : void {
_text.text = text;
}
}
class LayoutHeight extends Sprite {
private var _interval : int;
private var _nextY : int;
public function LayoutHeight(interval : int = 5,margin:int = 0) {
_interval = interval;
_nextY = margin;
}
override public function addChild(child : DisplayObject) : DisplayObject {
child.y = _nextY;
_nextY += child.height+_interval;
return super.addChild(child);
}
}
class LayoutWidth extends Sprite {
private var _interval : int;
private var _nextX : int;
public function LayoutWidth(interval:int = 5,margin:int = 0) {
_interval = interval;
_nextX = margin;
}
override public function addChild(child : DisplayObject) : DisplayObject {
child.x = _nextX;
_nextX += child.width+_interval;
return super.addChild(child);
}
}