「Extend」を含む日記 RSS

はてなキーワード: Extendとは

2007-07-11

めいろさくせーぷろぐらむつくってみたー

http://anond.hatelabo.jp/20070711013155

人待ちの間暇だったから作ってみたよー。

さっくり作ったから無駄が多い気がするけど、とりあえず動いた。

全体が埋まったかどうかの判定をしないでひたすらまわしまくってるし、なんか綺麗な形じゃないけど、とりあえず投稿しちゃうよ!

これ綺麗な形にするの結構難しいんじゃないかな。面白いね。

class Maze
	class Box
		WALL = 0
		LOAD = 1
		def initialize(state = LOAD)
			@state = state
		end
		def is_wall?
			return true if @state == WALL
			false
		end
		def is_load?
			return true if @state == LOAD
			false
		end
		def set_wall
			@state = WALL
		end
		def set_load
			@state = LOAD
		end
	end
	TOP = 10
	RIGHT = 11
	BOTTOM = 12
	LEFT = 13
	def initialize(height = 100, width = 60)
		@height = height
		@width  = width
		@data = Array.new
		self.fillbox
		@counter = 1000
		@root = Array.new
	end
	def choose_random
		return rand(@width-3)+1, rand(@height-3)+1
	end
	def choose_direct
		t = rand(4)+10
		return t
	end
	def move(x, y, t)
		new_x = x
		new_y = y
		case t
		when TOP
			new_y -= 1
		when RIGHT
			new_x += 1
		when BOTTOM
			new_y += 1
		when LEFT
			new_x -= 1
		end
		return new_x, new_y
	end
	def make_load?(x, y)
		return false if x < 0 or y < 0 or x > @width-1 or y > @height-1
		return false if @data[x][y].is_load?
		true
	end
	def dead_end?(x, y, t)
		case t
		when TOP
			if make_load?(x-1, y) and \
				 make_load?(x-1, y-1) and\
				 make_load?(x, y-1) and\
				 make_load?(x+1, y-1) and\
				 make_load?(x+1, y) then
				 return false
			end
		when RIGHT
			if make_load?(x, y-1) and \
				 make_load?(x+1, y-1) and\
				 make_load?(x+1, y) and\
				 make_load?(x+1, y+1) and\
				 make_load?(x, y+1) then
				 return false
			end
		when BOTTOM
			if make_load?(x-1, y) and \
				 make_load?(x-1, y+1) and\
				 make_load?(x, y+1) and\
				 make_load?(x+1, y+1) and\
				 make_load?(x+1, y) then
				 return false
			end
		when LEFT
			if make_load?(x, y+1) and \
				 make_load?(x-1, y+1) and\
				 make_load?(x-1, y) and\
				 make_load?(x-1, y-1) and\
				 make_load?(x, y-1) then
				 return false
			end
		end
		true
	end
	def check_all?(stack)
		for i in TOP..LEFT do
			return false if !stack.include?(i)
		end
		true
	end
	def extend(x, y)
		@data[x][y].set_load
		check_stack = Array.new
		loop do
			t = choose_direct
			check_stack << t
			new_x, new_y = move(x, y, t)
			if enable_new_point?(new_x, new_y)
				if !dead_end?(new_x, new_y, t) then
					@root << ["ex", new_x, new_y, t]
					extend(new_x, new_y)
					break
				end
			end
			break if check_all?(check_stack)
		end
	end
	def enable_new_point?(x, y)
		return false if x<=0 or y <= 0 or x > @width-1 or y > @height-1
		return false if @data[x][y].is_load?
		true
	end
	def _make
		x = y = 0
		loop do
			x, y = choose_random
			break if @data[x][y].is_load?
		end
		@root << [x,y]
		extend(x, y)
	end
	def make_goal
		x = @width - 2
		y = @height - 1
		for i in 1..@width-3
			if @data[x-i][y-1].is_load? then
				@data[x-i][y].set_load
				break
			end
		end
	end
	def make_start
		for i in 1..@width-3
			if @data[i][1].is_load? then
				@data[i][0].set_load
				break
			end
		end
	end
	def make
		self.fill_all
		x, y = choose_random
		@root << [x,y]
		@data[x][y].set_load
		extend(x,y)
		while(!is_fill?) do
			_make
		end
		make_goal
		make_start
		self
	end
	def is_fill?
		@counter -= 1
		return true if @counter < 0
		false
	end
	def fillbox
		for i in 0...@width do
			@data[i] = Array.new
			for j in 0...@height do
				@data[i][j] = Box.new
			end
		end
	end
	def fill_all
		for i in 0...@width do
			for j in 0...@height do
				@data[i][j].set_wall
			end
		end
	end
	def output
		for i in 0...@height do
			for j in 0...@width do
				if @data[j][i].is_wall? then
					print "#" 
				else
					print " "
				end
			end
			print "\n"
		end
	end
end

Maze.new(30, 30).make.output

## output

# ############################
#  ##    # #     ##    #     #
##    ##   ## ## #  ##   ### #
### #  ###  #  #   #### ###  #
#   # ## ## ## ### #  #  #  ##
### #  #  #  #   #### ## # ###
##  ## ## ## ###   #  ## #   #
#  ### #  #   #### # ##  ## ##
# ## #   ####    #    # ###  #
#  # ###  # #### # ## # #   ##
##     ## #      #  ### ######
######  ### #######   ###    #
#    ##   ###   # # #   ## # #
# #######  #  # # # ###  ### #
#   # # ##   ##   #   ##   # #
### #    ######## ########   #
#   # ## ##     #    #     ###
# ### ##  ## ##   ##   ##### #
#      ##  # #  #  ###  #    #
# ######## # #####   ## # # ##
#       ## #  #  #####  # #  #
####### #  ## # ## #   ## ## #
##    ### ###   ##   ####### #
#  ##  ##   ## ##  ###    ## #
# ####  ###  # #  ## # ##    #
#  # ##   ## # # ###    ######
## #  # #  # # #   ## #    ###
#    ## ## #   # #  # # ##  ##
# # ##  ##   # # ##   #  ##  #
########################### ##

ログイン ユーザー登録
ようこそ ゲスト さん