OSDN Git Service

- shogi_server/command.rb: Commands specific to 81Dojo, startig with '%%%', are just...
[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 PieceKY  < Piece
207   def initialize(*arg)
208     @point = 1
209     @normal_moves = []
210     @promoted_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1]]
211     @name = "KY"
212     @promoted_name = "NY"
213     super
214   end
215   def room_of_head?(x, y, name)
216     if (name == "KY")
217       if (@sente)
218         return false if (y == 1)
219       else
220         return false if (y == 9)
221       end
222     end
223     return true
224   end
225   def far_movable_grids
226     grids = Array::new
227     if (@promoted)
228       return []
229     else
230       if (@sente)                 # up
231         cand_x = @x
232         cand_y = @y - 1
233         while (jump_to?(cand_x, cand_y))
234           grids.push([cand_x, cand_y])
235           break if (! put_to?(cand_x, cand_y))
236           cand_y = cand_y - 1
237         end
238       else                        # down
239         cand_x = @x
240         cand_y = @y + 1
241         while (jump_to?(cand_x, cand_y))
242           grids.push([cand_x, cand_y])
243           break if (! put_to?(cand_x, cand_y))
244           cand_y = cand_y + 1
245         end
246       end
247       return grids
248     end
249   end
250 end
251
252 class PieceKE  < Piece
253   def initialize(*arg)
254     @point = 1
255     @normal_moves = [[+1, +2], [-1, +2]]
256     @promoted_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1]]
257     @name = "KE"
258     @promoted_name = "NK"
259     super
260   end
261   def room_of_head?(x, y, name)
262     if (name == "KE")
263       if (@sente)
264         return false if ((y == 1) || (y == 2))
265       else
266         return false if ((y == 9) || (y == 8))
267       end
268     end
269     return true
270   end
271 end
272 class PieceGI  < Piece
273   def initialize(*arg)
274     @point = 1
275     @normal_moves = [[0, +1], [+1, +1], [-1, +1], [+1, -1], [-1, -1]]
276     @promoted_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1]]
277     @name = "GI"
278     @promoted_name = "NG"
279     super
280   end
281 end
282 class PieceKI  < Piece
283   def initialize(*arg)
284     @point = 1
285     @normal_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1]]
286     @promoted_moves = []
287     @name = "KI"
288     @promoted_name = nil
289     super
290   end
291 end
292 class PieceKA  < Piece
293   def initialize(*arg)
294     @point = 5
295     @normal_moves = []
296     @promoted_moves = [[0, +1], [+1, 0], [-1, 0], [0, -1]]
297     @name = "KA"
298     @promoted_name = "UM"
299     super
300   end
301   def far_movable_grids
302     grids = Array::new
303     ## up right
304     cand_x = @x - 1
305     cand_y = @y - 1
306     while (jump_to?(cand_x, cand_y))
307       grids.push([cand_x, cand_y])
308       break if (! put_to?(cand_x, cand_y))
309       cand_x = cand_x - 1
310       cand_y = cand_y - 1
311     end
312     ## down right
313     cand_x = @x - 1
314     cand_y = @y + 1
315     while (jump_to?(cand_x, cand_y))
316       grids.push([cand_x, cand_y])
317       break if (! put_to?(cand_x, cand_y))
318       cand_x = cand_x - 1
319       cand_y = cand_y + 1
320     end
321     ## up left
322     cand_x = @x + 1
323     cand_y = @y - 1
324     while (jump_to?(cand_x, cand_y))
325       grids.push([cand_x, cand_y])
326       break if (! put_to?(cand_x, cand_y))
327       cand_x = cand_x + 1
328       cand_y = cand_y - 1
329     end
330     ## down left
331     cand_x = @x + 1
332     cand_y = @y + 1
333     while (jump_to?(cand_x, cand_y))
334       grids.push([cand_x, cand_y])
335       break if (! put_to?(cand_x, cand_y))
336       cand_x = cand_x + 1
337       cand_y = cand_y + 1
338     end
339     return grids
340   end
341 end
342 class PieceHI  < Piece
343   def initialize(*arg)
344     @point = 5
345     @normal_moves = []
346     @promoted_moves = [[+1, +1], [-1, +1], [+1, -1], [-1, -1]]
347     @name = "HI"
348     @promoted_name = "RY"
349     super
350   end
351   def far_movable_grids
352     grids = Array::new
353     ## up
354     cand_x = @x
355     cand_y = @y - 1
356     while (jump_to?(cand_x, cand_y))
357       grids.push([cand_x, cand_y])
358       break if (! put_to?(cand_x, cand_y))
359       cand_y = cand_y - 1
360     end
361     ## down
362     cand_x = @x
363     cand_y = @y + 1
364     while (jump_to?(cand_x, cand_y))
365       grids.push([cand_x, cand_y])
366       break if (! put_to?(cand_x, cand_y))
367       cand_y = cand_y + 1
368     end
369     ## right
370     cand_x = @x - 1
371     cand_y = @y
372     while (jump_to?(cand_x, cand_y))
373       grids.push([cand_x, cand_y])
374       break if (! put_to?(cand_x, cand_y))
375       cand_x = cand_x - 1
376     end
377     ## down
378     cand_x = @x + 1
379     cand_y = @y
380     while (jump_to?(cand_x, cand_y))
381       grids.push([cand_x, cand_y])
382       break if (! put_to?(cand_x, cand_y))
383       cand_x = cand_x + 1
384     end
385     return grids
386   end
387 end
388 class PieceOU < Piece
389   def initialize(*arg)
390     @point = 0
391     @normal_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1], [+1, -1], [-1, -1]]
392     @promoted_moves = []
393     @name = "OU"
394     @promoted_name = nil
395     super
396   end
397 end
398
399 end # ShogiServer