3 ## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
4 ## Copyright (C) 2007-2012 Daigo Moriwaki (daigo at debian dot org)
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.
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.
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
20 module ShogiServer # for a namespace
23 PROMOTE = {"FU" => "TO", "KY" => "NY", "KE" => "NK",
24 "GI" => "NG", "KA" => "UM", "HI" => "RY"}
25 def initialize(board, x, y, sente, promoted=false)
32 if ((x == 0) || (y == 0))
34 hands = board.sente_hands
36 hands = board.gote_hands
43 @board.array[x][y] = self
46 attr_accessor :promoted, :sente, :x, :y, :board
48 def room_of_head?(x, y, name)
53 return adjacent_movable_grids + far_movable_grids
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
71 if ((1 <= x) && (x <= 9) && (1 <= y) && (y <= 9))
72 if (@board.array[x][y] == nil) # dst is empty?
79 def adjacent_movable_grids
82 moves = @promoted_moves
86 moves.each do |(dx, dy)|
93 if (jump_to?(cand_x, cand_y))
94 grids.push([cand_x, cand_y])
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
106 return false if (((@x == 0) || (@y == 0)) && (name != @name)) # can't put promoted piece
108 return false if ((4 <= @y) && (4 <= y) && (name != @name)) # can't promote
110 return false if ((6 >= @y) && (6 >= y) && (name != @name))
114 if ((@x == 0) || (@y == 0))
115 return jump_to?(x, y)
117 return movable_grids.include?([x, y])
122 if ((@x == 0) || (@y == 0))
124 @board.sente_hands.delete(self)
126 @board.gote_hands.delete(self)
128 @board.array[x][y] = self
129 elsif ((x == 0) || (y == 0))
130 @promoted = false # clear promoted flag before moving to hands
132 @board.sente_hands.push(self)
134 @board.gote_hands.push(self)
136 @board.array[@x][@y] = nil
138 @board.array[@x][@y] = nil
139 @board.array[x][y] = self
172 class PieceFU < Piece
175 @normal_moves = [[0, +1]]
176 @promoted_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1]]
178 @promoted_name = "TO"
181 def room_of_head?(x, y, name)
184 return false if (y == 1)
186 return false if (y == 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))
206 class PieceKY < Piece
210 @promoted_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1]]
212 @promoted_name = "NY"
215 def room_of_head?(x, y, name)
218 return false if (y == 1)
220 return false if (y == 9)
225 def far_movable_grids
233 while (jump_to?(cand_x, cand_y))
234 grids.push([cand_x, cand_y])
235 break if (! put_to?(cand_x, cand_y))
241 while (jump_to?(cand_x, cand_y))
242 grids.push([cand_x, cand_y])
243 break if (! put_to?(cand_x, cand_y))
252 class PieceKE < Piece
255 @normal_moves = [[+1, +2], [-1, +2]]
256 @promoted_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1]]
258 @promoted_name = "NK"
261 def room_of_head?(x, y, name)
264 return false if ((y == 1) || (y == 2))
266 return false if ((y == 9) || (y == 8))
272 class PieceGI < Piece
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]]
278 @promoted_name = "NG"
282 class PieceKI < Piece
285 @normal_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1]]
292 class PieceKA < Piece
296 @promoted_moves = [[0, +1], [+1, 0], [-1, 0], [0, -1]]
298 @promoted_name = "UM"
301 def far_movable_grids
306 while (jump_to?(cand_x, cand_y))
307 grids.push([cand_x, cand_y])
308 break if (! put_to?(cand_x, cand_y))
315 while (jump_to?(cand_x, cand_y))
316 grids.push([cand_x, cand_y])
317 break if (! put_to?(cand_x, cand_y))
324 while (jump_to?(cand_x, cand_y))
325 grids.push([cand_x, cand_y])
326 break if (! put_to?(cand_x, cand_y))
333 while (jump_to?(cand_x, cand_y))
334 grids.push([cand_x, cand_y])
335 break if (! put_to?(cand_x, cand_y))
342 class PieceHI < Piece
346 @promoted_moves = [[+1, +1], [-1, +1], [+1, -1], [-1, -1]]
348 @promoted_name = "RY"
351 def far_movable_grids
356 while (jump_to?(cand_x, cand_y))
357 grids.push([cand_x, cand_y])
358 break if (! put_to?(cand_x, cand_y))
364 while (jump_to?(cand_x, cand_y))
365 grids.push([cand_x, cand_y])
366 break if (! put_to?(cand_x, cand_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))
380 while (jump_to?(cand_x, cand_y))
381 grids.push([cand_x, cand_y])
382 break if (! put_to?(cand_x, cand_y))
388 class PieceOU < Piece
391 @normal_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1], [+1, -1], [-1, -1]]