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 def initialize(move_count=0)
24 @sente_hands = Array::new
25 @gote_hands = Array::new
26 @history = Hash::new(0)
27 @sente_history = Hash::new(0)
28 @gote_history = Hash::new(0)
29 @array = [[], [], [], [], [], [], [], [], [], []]
30 @move_count = move_count
31 @teban = nil # black => true, white => false
33 attr_accessor :array, :sente_hands, :gote_hands, :history, :sente_history, :gote_history, :teban
34 attr_reader :move_count
37 return Marshal.load(Marshal.dump(self))
41 PieceKY::new(self, 1, 1, false)
42 PieceKE::new(self, 2, 1, false)
43 PieceGI::new(self, 3, 1, false)
44 PieceKI::new(self, 4, 1, false)
45 PieceOU::new(self, 5, 1, false)
46 PieceKI::new(self, 6, 1, false)
47 PieceGI::new(self, 7, 1, false)
48 PieceKE::new(self, 8, 1, false)
49 PieceKY::new(self, 9, 1, false)
50 PieceKA::new(self, 2, 2, false)
51 PieceHI::new(self, 8, 2, false)
53 PieceFU::new(self, i, 3, false)
56 PieceKY::new(self, 1, 9, true)
57 PieceKE::new(self, 2, 9, true)
58 PieceGI::new(self, 3, 9, true)
59 PieceKI::new(self, 4, 9, true)
60 PieceOU::new(self, 5, 9, true)
61 PieceKI::new(self, 6, 9, true)
62 PieceGI::new(self, 7, 9, true)
63 PieceKE::new(self, 8, 9, true)
64 PieceKY::new(self, 9, 9, true)
65 PieceKA::new(self, 8, 8, true)
66 PieceHI::new(self, 2, 8, true)
68 PieceFU::new(self, i, 7, true)
73 def set_from_str(strs)
74 strs.split(/\n/).each do |str|
79 while (str.length > 2)
80 str.sub!(/^(...?)/, '')
82 if (one =~ /^([\+\-])(..)/)
90 if ((x < 1) || (9 < x) || (y < 1) || (9 < y))
91 raise "bad position #{x} #{y}"
95 PieceFU::new(self, x, y, sente)
97 PieceKY::new(self, x, y, sente)
99 PieceKE::new(self, x, y, sente)
101 PieceGI::new(self, x, y, sente)
103 PieceKI::new(self, x, y, sente)
105 PieceOU::new(self, x, y, sente)
107 PieceKA::new(self, x, y, sente)
109 PieceHI::new(self, x, y, sente)
111 PieceFU::new(self, x, y, sente, true)
113 PieceKY::new(self, x, y, sente, true)
115 PieceKE::new(self, x, y, sente, true)
117 PieceGI::new(self, x, y, sente, true)
119 PieceKA::new(self, x, y, sente, true)
121 PieceHI::new(self, x, y, sente, true)
123 raise "unkown piece #{name}"
128 elsif (str =~ /^P([\+\-])/)
136 while (str.length > 3)
137 str.sub!(/^..(..)/, '')
141 PieceFU::new(self, 0, 0, sente)
143 PieceKY::new(self, 0, 0, sente)
145 PieceKE::new(self, 0, 0, sente)
147 PieceGI::new(self, 0, 0, sente)
149 PieceKI::new(self, 0, 0, sente)
151 PieceKA::new(self, 0, 0, sente)
153 PieceHI::new(self, 0, 0, sente)
155 raise "unkown piece #{name}"
162 def have_piece?(hands, name)
163 piece = hands.find { |i|
169 def move_to(x0, y0, x1, y1, name, sente)
176 if ((x0 == 0) || (y0 == 0))
177 piece = have_piece?(hands, name)
178 return :illegal if (piece == nil || ! piece.move_to?(x1, y1, name))
179 piece.move_to(x1, y1)
181 if (@array[x0][y0] == nil || !@array[x0][y0].move_to?(x1, y1, name))
184 if (@array[x0][y0].name != name) # promoted ?
185 @array[x0][y0].promoted = true
187 if (@array[x1][y1]) # capture
188 if (@array[x1][y1].name == "OU")
189 return :outori # return board update
191 @array[x1][y1].sente = @array[x0][y0].sente
192 @array[x1][y1].move_to(0, 0)
193 hands.sort! {|a, b| # TODO refactor. Move to Piece class
197 @array[x0][y0].move_to(x1, y1)
200 @teban = @teban ? false : true
204 def look_for_ou(sente)
210 (@array[x][y].name == "OU") &&
211 (@array[x][y].sente == sente))
218 raise "can't find ou"
221 # not checkmate, but check. sente is checked.
222 def checkmated?(sente) # sente is loosing
223 ou = look_for_ou(sente)
229 (@array[x][y].sente != sente))
230 if (@array[x][y].movable_grids.include?([ou.x, ou.y]))
241 def uchifuzume?(sente)
242 rival_ou = look_for_ou(! sente) # rival's ou
243 if (sente) # rival is gote
244 if ((rival_ou.y != 9) &&
245 (@array[rival_ou.x][rival_ou.y + 1]) &&
246 (@array[rival_ou.x][rival_ou.y + 1].name == "FU") &&
247 (@array[rival_ou.x][rival_ou.y + 1].sente == sente)) # uchifu true
249 fu_y = rival_ou.y + 1
254 if ((rival_ou.y != 1) &&
255 (@array[rival_ou.x][rival_ou.y - 1]) &&
256 (@array[rival_ou.x][rival_ou.y - 1].name == "FU") &&
257 (@array[rival_ou.x][rival_ou.y - 1].sente == sente)) # uchifu true
259 fu_y = rival_ou.y - 1
265 ## case: rival_ou is moving
266 rival_ou.movable_grids.each do |(cand_x, cand_y)|
267 tmp_board = deep_copy
268 s = tmp_board.move_to(rival_ou.x, rival_ou.y, cand_x, cand_y, "OU", ! sente)
269 raise "internal error" if (s != true)
270 if (! tmp_board.checkmated?(! sente)) # good move
275 ## case: rival is capturing fu
281 (@array[x][y].sente != sente) &&
282 @array[x][y].movable_grids.include?([fu_x, fu_y])) # capturable
285 if (@array[x][y].promoted)
286 names << @array[x][y].promoted_name
288 names << @array[x][y].name
289 if @array[x][y].promoted_name &&
290 @array[x][y].move_to?(fu_x, fu_y, @array[x][y].promoted_name)
291 names << @array[x][y].promoted_name
295 tmp_board = deep_copy
296 s = tmp_board.move_to(x, y, fu_x, fu_y, name, ! sente)
300 tmp_board.checkmated?(! sente) # result
303 all_illegal = names.find {|a| a != :illegal}
304 raise "internal error: legal move not found" if all_illegal == nil
305 r = names.find {|a| a == false} # good move
306 return false if r == false # found good move
315 # @[sente|gote]_history has at least one item while the player is checking the other or
317 def update_sennichite(player)
320 if checkmated?(!player)
322 @sente_history["dummy"] = 1 # flag to see Sente player is checking Gote player
324 @gote_history["dummy"] = 1 # flag to see Gote player is checking Sente player
328 @sente_history.clear # no more continuous check
330 @gote_history.clear # no more continuous check
333 if @sente_history.size > 0 # possible for Sente's or Gote's turn
334 @sente_history[str] += 1
336 if @gote_history.size > 0 # possible for Sente's or Gote's turn
337 @gote_history[str] += 1
341 def oute_sennichite?(player)
342 return nil unless sennichite?
346 if (@sente_history[to_s] >= 4) # sente is checking gote
347 return :oute_sennichite_sente_lose
348 elsif (@gote_history[to_s] >= 3) # sente is escaping
349 return :oute_sennichite_gote_lose
351 return nil # Not oute_sennichite, but sennichite
355 if (@gote_history[to_s] >= 4) # gote is checking sente
356 return :oute_sennichite_gote_lose
357 elsif (@sente_history[to_s] >= 3) # gote is escaping
358 return :oute_sennichite_sente_lose
360 return nil # Not oute_sennichite, but sennichite
366 if (@history[to_s] >= 4) # already 3 times
372 def good_kachi?(sente)
373 if (checkmated?(sente))
374 puts "'NG: Checkmating." if $DEBUG
378 ou = look_for_ou(sente)
379 if (sente && (ou.y >= 4))
380 puts "'NG: Black's OU does not enter yet." if $DEBUG
383 if (! sente && (ou.y <= 6))
384 puts "'NG: White's OU does not enter yet." if $DEBUG
402 (@array[x][y].sente == sente) &&
403 (@array[x][y].point > 0))
404 point = point + @array[x][y].point
410 hands.each do |piece|
411 point = point + piece.point
415 puts "'NG: Piece#[%d] is too small." % [number] if $DEBUG
420 puts "'NG: Black's point#[%d] is too small." % [point] if $DEBUG
425 puts "'NG: White's point#[%d] is too small." % [point] if $DEBUG
430 puts "'Good: Piece#[%d], Point[%d]." % [number, point] if $DEBUG
434 # sente is nil only if tests in test_board run
435 def handle_one_move(str, sente=nil)
436 if (str =~ /^([\+\-])(\d)(\d)(\d)(\d)([A-Z]{2})/)
443 elsif (str =~ /^%KACHI/)
444 raise ArgumentError, "sente is null", caller if sente == nil
445 if (good_kachi?(sente))
450 elsif (str =~ /^%TORYO/)
456 if (((x0 == 0) || (y0 == 0)) && # source is not from hand
457 ((x0 != 0) || (y0 != 0)))
459 elsif ((x1 == 0) || (y1 == 0)) # destination is out of board
464 sente = true if sente == nil # deprecated
465 return :illegal unless sente == true # black player's move must be black
468 sente = false if sente == nil # deprecated
469 return :illegal unless sente == false # white player's move must be white
474 if ((x0 == 0) && (y0 == 0))
475 return :illegal if (! have_piece?(hands, name))
476 elsif (! @array[x0][y0])
477 return :illegal # no piece
478 elsif (@array[x0][y0].sente != sente)
479 return :illegal # this is not mine
480 elsif (@array[x0][y0].name != name)
481 return :illegal if (@array[x0][y0].promoted_name != name) # can't promote
485 if (@array[x1][y1] &&
486 (@array[x1][y1].sente == sente)) # can't capture mine
488 elsif ((x0 == 0) && (y0 == 0) && @array[x1][y1])
489 return :illegal # can't put on existing piece
492 tmp_board = deep_copy
493 return :illegal if (tmp_board.move_to(x0, y0, x1, y1, name, sente) == :illegal)
494 return :oute_kaihimore if (tmp_board.checkmated?(sente))
495 tmp_board.update_sennichite(sente)
496 os_result = tmp_board.oute_sennichite?(sente)
497 return os_result if os_result # :oute_sennichite_sente_lose or :oute_sennichite_gote_lose
498 return :sennichite if tmp_board.sennichite?
500 if ((x0 == 0) && (y0 == 0) && (name == "FU") && tmp_board.uchifuzume?(sente))
504 move_to(x0, y0, x1, y1, name, sente)
506 update_sennichite(sente)
514 a.push(sprintf("P%d", y))
526 a.push(sprintf("\n"))
529 if (! sente_hands.empty?)
531 sente_hands.each do |p|
532 a.push("00" + p.name)
536 if (! gote_hands.empty?)
538 gote_hands.each do |p|
539 a.push("00" + p.name)
543 a.push("%s\n" % [@teban ? "+" : "-"])