Skip to content

Instantly share code, notes, and snippets.

@umezo
Created November 8, 2012 10:16
Show Gist options
  • Save umezo/4037953 to your computer and use it in GitHub Desktop.
Save umezo/4037953 to your computer and use it in GitHub Desktop.
Generate amida, a traditional Japanese randomizer ;
function Amida( col , row ){
this.amidaModel = new AmidaModel( col , row );
}
Amida.prototype.print = function( ){
var chars = ["├" , "┤" , "│" ];
var prev = 0 ;
var grid = this.amidaModel.getArray();
var line = [] ;
for( var i = 0 , n = grid.length ; i < n ; i++ ){
var row = grid[i];
row.push( 0 );//番兵としてつっこむ
for( var j = 0 , m = row.length ; j < m ; j++ ){
var current = row[j];
var charIndex ;
if( prev ){
charIndex = 1 ;
}else if( current ){
charIndex = 0 ;
}else{
charIndex = 2 ;
}
prev = current ;
line.push( chars[ charIndex ] );
}
line.push( "\n" );
}
console.log( "",line.join("") );
}
function AmidaModel( col , row ){
this.col = col ;
this.row = row ;
}
AmidaModel.prototype.getArray = function(){
var grid = [] ;
var row = this.row , col = this.col ;
for( var i = 0 ; i < row; i++ ){
var hasLine = false;
grid[i] = [] ;
for( var j = 0 ; j < col ; j++ ){
if( hasLine ){
hasLine = false;
}else{
hasLine = Math.random() > 0.5 ;
}
grid[i][j] = hasLine > 0.5 ;
}
}
return grid ;
};
// ( new Amida( 5 , 10 ) ).print() ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment