3 ## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
4 ## Copyright (C) 2007-2008 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 PieceKE < Piece
209 @normal_moves = [[+1, +2], [-1, +2]]
210 @promoted_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1]]
212 @promoted_name = "NK"
215 def room_of_head?(x, y, name)
218 return false if ((y == 1) || (y == 2))
220 return false if ((y == 9) || (y == 8))
226 class PieceGI < Piece
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]]
232 @promoted_name = "NG"
236 class PieceKI < Piece
239 @normal_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1]]
246 class PieceKA < Piece
250 @promoted_moves = [[0, +1], [+1, 0], [-1, 0], [0, -1]]
252 @promoted_name = "UM"
255 def far_movable_grids
260 while (jump_to?(cand_x, cand_y))
261 grids.push([cand_x, cand_y])
262 break if (! put_to?(cand_x, cand_y))
269 while (jump_to?(cand_x, cand_y))
270 grids.push([cand_x, cand_y])
271 break if (! put_to?(cand_x, cand_y))
278 while (jump_to?(cand_x, cand_y))
279 grids.push([cand_x, cand_y])
280 break if (! put_to?(cand_x, cand_y))
287 while (jump_to?(cand_x, cand_y))
288 grids.push([cand_x, cand_y])
289 break if (! put_to?(cand_x, cand_y))
296 class PieceHI < Piece
300 @promoted_moves = [[+1, +1], [-1, +1], [+1, -1], [-1, -1]]
302 @promoted_name = "RY"
305 def far_movable_grids
310 while (jump_to?(cand_x, cand_y))
311 grids.push([cand_x, cand_y])
312 break if (! put_to?(cand_x, cand_y))
318 while (jump_to?(cand_x, cand_y))
319 grids.push([cand_x, cand_y])
320 break if (! put_to?(cand_x, cand_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))
334 while (jump_to?(cand_x, cand_y))
335 grids.push([cand_x, cand_y])
336 break if (! put_to?(cand_x, cand_y))
342 class PieceOU < Piece
345 @normal_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1], [+1, -1], [-1, -1]]