OSDN Git Service

Debugged the daemon mode
[shogi-server/shogi-server.git] / shogi_server / piece.rb
1 ## $Id$
2
3 ## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
4 ## Copyright (C) 2007-2008 Daigo Moriwaki (daigo at debian dot org)
5 ##
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
10 ##
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ## GNU General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, write to the Free Software
18 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 module ShogiServer # for a namespace
21
22 class Piece
23   PROMOTE = {"FU" => "TO", "KY" => "NY", "KE" => "NK", 
24              "GI" => "NG", "KA" => "UM", "HI" => "RY"}
25   def initialize(board, x, y, sente, promoted=false)
26     @board = board
27     @x = x
28     @y = y
29     @sente = sente
30     @promoted = promoted
31
32     if ((x == 0) || (y == 0))
33       if (sente)
34         hands = board.sente_hands
35       else
36         hands = board.gote_hands
37       end
38       hands.push(self)
39       hands.sort! {|a, b|
40         a.name <=> b.name
41       }
42     else
43       @board.array[x][y] = self
44     end
45   end
46   attr_accessor :promoted, :sente, :x, :y, :board
47
48   def room_of_head?(x, y, name)
49     true
50   end
51
52   def movable_grids
53     return adjacent_movable_grids + far_movable_grids
54   end
55
56   def far_movable_grids
57     return []
58   end
59
60   def jump_to?(x, y)
61     if ((1 <= x) && (x <= 9) && (1 <= y) && (y <= 9))
62       if ((@board.array[x][y] == nil) || # dst is empty
63           (@board.array[x][y].sente != @sente)) # dst is enemy
64         return true
65       end
66     end
67     return false
68   end
69
70   def put_to?(x, y)
71     if ((1 <= x) && (x <= 9) && (1 <= y) && (y <= 9))
72       if (@board.array[x][y] == nil) # dst is empty?
73         return true
74       end
75     end
76     return false
77   end
78
79   def adjacent_movable_grids
80     grids = Array::new
81     if (@promoted)
82       moves = @promoted_moves
83     else
84       moves = @normal_moves
85     end
86     moves.each do |(dx, dy)|
87       if (@sente)
88         cand_y = @y - dy
89       else
90         cand_y = @y + dy
91       end
92       cand_x = @x + dx
93       if (jump_to?(cand_x, cand_y))
94         grids.push([cand_x, cand_y])
95       end
96     end
97     return grids
98   end
99
100   def move_to?(x, y, name)
101     return false if (! room_of_head?(x, y, name))
102     return false if ((name != @name) && (name != @promoted_name))
103     return false if (@promoted && (name != @promoted_name)) # can't un-promote
104
105     if (! @promoted)
106       return false if (((@x == 0) || (@y == 0)) && (name != @name)) # can't put promoted piece
107       if (@sente)
108         return false if ((4 <= @y) && (4 <= y) && (name != @name)) # can't promote
109       else
110         return false if ((6 >= @y) && (6 >= y) && (name != @name))
111       end
112     end
113
114     if ((@x == 0) || (@y == 0))
115       return jump_to?(x, y)
116     else
117       return movable_grids.include?([x, y])
118     end
119   end
120
121   def move_to(x, y)
122     if ((@x == 0) || (@y == 0))
123       if (@sente)
124         @board.sente_hands.delete(self)
125       else
126         @board.gote_hands.delete(self)
127       end
128       @board.array[x][y] = self
129     elsif ((x == 0) || (y == 0))
130       @promoted = false         # clear promoted flag before moving to hands
131       if (@sente)
132         @board.sente_hands.push(self)
133       else
134         @board.gote_hands.push(self)
135       end
136       @board.array[@x][@y] = nil
137     else
138       @board.array[@x][@y] = nil
139       @board.array[x][y] = self
140     end
141     @x = x
142     @y = y
143   end
144
145   def point
146     @point
147   end
148
149   def name
150     @name
151   end
152
153   def promoted_name
154     @promoted_name
155   end
156
157   def to_s
158     if (@sente)
159       sg = "+"
160     else
161       sg = "-"
162     end
163     if (@promoted)
164       n = @promoted_name
165     else
166       n = @name
167     end
168     return sg + n
169   end
170 end
171
172 class PieceFU < Piece
173   def initialize(*arg)
174     @point = 1
175     @normal_moves = [[0, +1]]
176     @promoted_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1]]
177     @name = "FU"
178     @promoted_name = "TO"
179     super
180   end
181   def room_of_head?(x, y, name)
182     if (name == "FU")
183       if (@sente)
184         return false if (y == 1)
185       else
186         return false if (y == 9)
187       end
188       ## 2fu check
189       c = 0
190       iy = 1
191       while (iy <= 9)
192         if ((iy  != @y) &&      # not source position
193             @board.array[x][iy] &&
194             (@board.array[x][iy].sente == @sente) && # mine
195             (@board.array[x][iy].name == "FU") &&
196             (@board.array[x][iy].promoted == false))
197           return false
198         end
199         iy = iy + 1
200       end
201     end
202     return true
203   end
204 end
205
206 class PieceKE  < Piece
207   def initialize(*arg)
208     @point = 1
209     @normal_moves = [[+1, +2], [-1, +2]]
210     @promoted_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1]]
211     @name = "KE"
212     @promoted_name = "NK"
213     super
214   end
215   def room_of_head?(x, y, name)
216     if (name == "KE")
217       if (@sente)
218         return false if ((y == 1) || (y == 2))
219       else
220         return false if ((y == 9) || (y == 8))
221       end
222     end
223     return true
224   end
225 end
226 class PieceGI  < Piece
227   def initialize(*arg)
228     @point = 1
229     @normal_moves = [[0, +1], [+1, +1], [-1, +1], [+1, -1], [-1, -1]]
230     @promoted_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1]]
231     @name = "GI"
232     @promoted_name = "NG"
233     super
234   end
235 end
236 class PieceKI  < Piece
237   def initialize(*arg)
238     @point = 1
239     @normal_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1]]
240     @promoted_moves = []
241     @name = "KI"
242     @promoted_name = nil
243     super
244   end
245 end
246 class PieceKA  < Piece
247   def initialize(*arg)
248     @point = 5
249     @normal_moves = []
250     @promoted_moves = [[0, +1], [+1, 0], [-1, 0], [0, -1]]
251     @name = "KA"
252     @promoted_name = "UM"
253     super
254   end
255   def far_movable_grids
256     grids = Array::new
257     ## up right
258     cand_x = @x - 1
259     cand_y = @y - 1
260     while (jump_to?(cand_x, cand_y))
261       grids.push([cand_x, cand_y])
262       break if (! put_to?(cand_x, cand_y))
263       cand_x = cand_x - 1
264       cand_y = cand_y - 1
265     end
266     ## down right
267     cand_x = @x - 1
268     cand_y = @y + 1
269     while (jump_to?(cand_x, cand_y))
270       grids.push([cand_x, cand_y])
271       break if (! put_to?(cand_x, cand_y))
272       cand_x = cand_x - 1
273       cand_y = cand_y + 1
274     end
275     ## up left
276     cand_x = @x + 1
277     cand_y = @y - 1
278     while (jump_to?(cand_x, cand_y))
279       grids.push([cand_x, cand_y])
280       break if (! put_to?(cand_x, cand_y))
281       cand_x = cand_x + 1
282       cand_y = cand_y - 1
283     end
284     ## down left
285     cand_x = @x + 1
286     cand_y = @y + 1
287     while (jump_to?(cand_x, cand_y))
288       grids.push([cand_x, cand_y])
289       break if (! put_to?(cand_x, cand_y))
290       cand_x = cand_x + 1
291       cand_y = cand_y + 1
292     end
293     return grids
294   end
295 end
296 class PieceHI  < Piece
297   def initialize(*arg)
298     @point = 5
299     @normal_moves = []
300     @promoted_moves = [[+1, +1], [-1, +1], [+1, -1], [-1, -1]]
301     @name = "HI"
302     @promoted_name = "RY"
303     super
304   end
305   def far_movable_grids
306     grids = Array::new
307     ## up
308     cand_x = @x
309     cand_y = @y - 1
310     while (jump_to?(cand_x, cand_y))
311       grids.push([cand_x, cand_y])
312       break if (! put_to?(cand_x, cand_y))
313       cand_y = cand_y - 1
314     end
315     ## down
316     cand_x = @x
317     cand_y = @y + 1
318     while (jump_to?(cand_x, cand_y))
319       grids.push([cand_x, cand_y])
320       break if (! put_to?(cand_x, cand_y))
321       cand_y = cand_y + 1
322     end
323     ## right
324     cand_x = @x - 1
325     cand_y = @y
326     while (jump_to?(cand_x, cand_y))
327       grids.push([cand_x, cand_y])
328       break if (! put_to?(cand_x, cand_y))
329       cand_x = cand_x - 1
330     end
331     ## down
332     cand_x = @x + 1
333     cand_y = @y
334     while (jump_to?(cand_x, cand_y))
335       grids.push([cand_x, cand_y])
336       break if (! put_to?(cand_x, cand_y))
337       cand_x = cand_x + 1
338     end
339     return grids
340   end
341 end
342 class PieceOU < Piece
343   def initialize(*arg)
344     @point = 0
345     @normal_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1], [+1, -1], [-1, -1]]
346     @promoted_moves = []
347     @name = "OU"
348     @promoted_name = nil
349     super
350   end
351 end
352
353 end # ShogiServer