Created
October 11, 2009 07:51
-
-
Save mizchi/207527 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//マップ定義 | |
const RIGHT=0; | |
const LEFT=1; | |
const UP=2; | |
const DOWN=3; | |
const START_POS=-1; | |
const GOAL_POS=-2; | |
function map_generater(_x,_y,start_x,start_y){ | |
var _map=[]; | |
var c_x;//xカーソル | |
var c_y;//yカーソル | |
var dig_direction; | |
var randnum; | |
//初期化 | |
for(var i=0;i<_x;i++){ | |
_map[i]=new Array(); | |
for(var j=0;j<_y;j++) | |
_map[i][j]=1; | |
} | |
c_x= start_x; | |
c_y= start_y; | |
resetDigPos(c_x,c_y); | |
for(i=0;i<50000;i++){ //暫定 軌跡をスタックにいれて再帰処理させる | |
c_x=(randInt(_x/2-1)+1)*2; | |
c_y=(randInt(_y/2-1)+1)*2; | |
if(! _map[c_x][c_y]) | |
resetDigPos(c_x,c_y); | |
} | |
removeIsolateSell(); //孤立したブロックを除去 | |
//スタートを設定 | |
_map[start_x][start_y]=START_POS; | |
return _map; | |
/*----- Class Method -----*/ | |
function resetDigPos(_xx,_yy){ | |
c_x=_xx; | |
c_y=_yy; | |
_map[c_x][c_y]=0; | |
var flag=1; | |
var last_dir=-1; | |
while(flag){ | |
//掘る方角を設定 | |
randnum=Math.floor(Math.random()*4); | |
flag=0; | |
//逆戻り禁止 | |
//進んできた方向へ戻ろうとしたら方向をリロール | |
if( (last_dir==LEFT)&&(randnum==RIGHT) ){ | |
flag=1;continue;} | |
if( (last_dir==RIGHT)&&(randnum==LEFT) ){ | |
flag=1;continue;} | |
if( (last_dir==UP)&&(randnum==DOWN) ){ | |
flag=1;continue;} | |
if( (last_dir==DOWN)&&(randnum==UP) ){ | |
flag=1;continue;} | |
//設定した方角の2マス先をチェックし | |
//壁だったらそこまで掘る | |
switch(randnum){ | |
case RIGHT: | |
if( (c_x+2 < _map.length) && _map[c_x+2][c_y] ) | |
flag=1; | |
else flag=0; | |
break; | |
case LEFT: | |
if((c_x-2 > 0) && _map[c_x-2][c_y]) | |
flag=1; | |
else flag=0; | |
break; | |
case UP: | |
if((c_y-2 > 0) && _map[c_x][c_y-2]) | |
flag=1; | |
else flag=0; | |
case DOWN: | |
if((c_y+2 < _map[0].length) && _map[c_x][c_y+2] ) | |
flag=1; | |
else flag=0; | |
} | |
if(flag) dig(randnum); | |
} | |
} | |
function dig(direction){ | |
if(direction==RIGHT){ //右へ | |
for(var i=0;i<2;i++) _map[++c_x][c_y]=0; | |
}else if(direction==LEFT){//左へ | |
for(var i=0;i<2;i++) _map[--c_x][c_y]=0; | |
}else if(direction==UP){//上へ | |
if(c_y-2!=0)//あとで精査 | |
for(var i=0;i<2;i++) _map[c_x][--c_y]=0; | |
}else if(direction==DOWN){//下へ | |
for(var i=0;i<2;i++) _map[c_x][++c_y]=0; | |
} | |
last_dir=randnum; | |
} | |
function removeIsolateSell(){ //ひどい | |
for(var i=1;i< _map.length-1 ;i++) | |
for(var j=1;j< _map[0].length-1 ;j++) | |
if(_map[i][j]==1) | |
if(_map[i-1][j-1]==0) | |
if(_map[i][j-1]==0) | |
if(_map[i+1][j-1]==0) | |
if(_map[i-1][j]==0) | |
if(_map[i+1][j]==0) | |
if(_map[i-1][j+1]==0) | |
if(_map[i][j+1]==0) | |
if(_map[i+1][j+1]==0) | |
_map[i][j]=0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment