1module Hako
2
3  class House
4    def initialize(widget)
5      @w = 4
6      @h = 5
7      @widget = widget
8      @chips = []
9      self
10    end
11    attr :widget
12    attr :unit_size
13    attr :chips
14
15    def enter(chip)
16      @chips.push(chip)
17    end
18
19    def wall
20      hash = {}
21      (-1..@w).each do |i|
22	hash[[i, -1]] = true
23	hash[[i, @h]] = true
24      end
25      (-1..@h).each do |i|
26	hash[[-1, i]] = true
27	hash[[@w, i]] = true
28      end
29      hash
30    end
31
32    def move?(chip, dx, dy)
33      field = self.wall
34      @chips.each do |c|
35	unless c == chip
36	  c.area.each do |a|
37	    field[a] = chip
38	  end
39	end
40      end
41
42      chip.area(dx, dy).each do |a|
43	return false if field[a]
44      end
45      return true
46    end
47  end
48
49  class Chip
50    def initialize(house, name, x, y, w=1, h=1)
51      @name = name
52      @w = w
53      @h = h
54
55      @house = house
56      
57      house.enter(self)
58      house.widget.add_chip(self)
59      moveto(x, y)
60    end
61    attr_reader :name, :w, :h
62
63    def area(dx=0, dy=0)
64      v = []
65      for i in (1..@h)
66	for j in (1..@w)
67	  v.push([dx + @x + j - 1, dy + @y + i - 1])
68	end
69      end
70      v
71    end
72
73    def moveto(x, y)
74      @x = x
75      @y = y
76      @house.widget.moveto_chip(x, y, self)
77    end
78
79    def move(dx, dy)
80      x = @x + dx
81      y = @y + dy
82      moveto(x, y)
83    end
84  
85    def do_motion(x, y)
86      try_move(dx, dy)
87    end
88
89    def try_move(dx, dy)
90      if @house.move?(self, dx, dy)
91	move(dx, dy)
92      end
93    end
94  end
95
96  class Game
97
98    def initialize(widget, lang = nil)
99      init_label_dic(lang)
100      house = House.new(widget)
101      she = Chip.new(house, label_of(:Musume), 1, 0, 2, 2)
102      father = Chip.new(house, label_of(:Papa), 0, 0, 1, 2)
103      mother = Chip.new(house, label_of(:Mama), 3, 0, 1, 2)
104      kozou = []
105      (0..3).each do |i|
106	kozou.push Chip.new(house, label_of(:Kozou), i, 2)
107      end
108      genan = []
109      (0..1).each do |i|
110	genan.push Chip.new(house, label_of(:Genan), i * 3, 3, 1, 2)
111      end
112      bantou = Chip.new(house, label_of(:Bantou), 1, 3, 2, 1)
113    end
114
115    private
116
117    def init_label_dic(lang)
118      @labeldic = 
119	if lang == 'ja' then
120	  { :Musume => "̼", :Papa => "���", :Mama => "���",
121	  :Kozou => "����", :Genan => "����", :Bantou => "��Ƭ" }
122	else
123	  { :Musume => "Daughter", :Papa => "Father", :Mama => "Mother",
124	  :Kozou => "Kozou", :Genan => "Genan", :Bantou => "Clerk" }
125	end
126    end
127
128    def label_of(key)
129      name = @labeldic[key]
130      name = key.to_s unless name
131      return name
132    end
133
134  end
135end
136