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
24 def initialize(move_count=0)
25 @sente_hands = Array::new
26 @gote_hands = Array::new
27 @history = Hash::new(0)
28 @sente_history = Hash::new(0)
29 @gote_history = Hash::new(0)
30 @array = [[], [], [], [], [], [], [], [], [], []]
31 @move_count = move_count
32 @teban = nil # black => true, white => false
35 attr_accessor :array, :sente_hands, :gote_hands, :history, :sente_history, :gote_history, :teban
36 attr_reader :move_count
38 # Initial moves for a Buoy game. If it is an empty array, the game is
39 # normal with the initial setting; otherwise, the game is started after the
41 attr_reader :initial_moves
44 return Marshal.load(Marshal.dump(self))
48 PieceKY::new(self, 1, 1, false)
49 PieceKE::new(self, 2, 1, false)
50 PieceGI::new(self, 3, 1, false)
51 PieceKI::new(self, 4, 1, false)
52 PieceOU::new(self, 5, 1, false)
53 PieceKI::new(self, 6, 1, false)
54 PieceGI::new(self, 7, 1, false)
55 PieceKE::new(self, 8, 1, false)
56 PieceKY::new(self, 9, 1, false)
57 PieceKA::new(self, 2, 2, false)
58 PieceHI::new(self, 8, 2, false)
60 PieceFU::new(self, i, 3, false)
63 PieceKY::new(self, 1, 9, true)
64 PieceKE::new(self, 2, 9, true)
65 PieceGI::new(self, 3, 9, true)
66 PieceKI::new(self, 4, 9, true)
67 PieceOU::new(self, 5, 9, true)
68 PieceKI::new(self, 6, 9, true)
69 PieceGI::new(self, 7, 9, true)
70 PieceKE::new(self, 8, 9, true)
71 PieceKY::new(self, 9, 9, true)
72 PieceKA::new(self, 8, 8, true)
73 PieceHI::new(self, 2, 8, true)
75 PieceFU::new(self, i, 7, true)
80 # Set up a board with the strs.
81 # Failing to parse the moves raises an StandardError.
82 # @param strs a board text
84 def set_from_str(strs)
85 strs.each_line do |str|
91 while (str.length > 2)
92 str.sub!(/^(...?)/, '')
94 if (one =~ /^([\+\-])(..)/)
102 if ((x < 1) || (9 < x) || (y < 1) || (9 < y))
103 raise "bad position #{x} #{y}"
107 PieceFU::new(self, x, y, sente)
109 PieceKY::new(self, x, y, sente)
111 PieceKE::new(self, x, y, sente)
113 PieceGI::new(self, x, y, sente)
115 PieceKI::new(self, x, y, sente)
117 PieceOU::new(self, x, y, sente)
119 PieceKA::new(self, x, y, sente)
121 PieceHI::new(self, x, y, sente)
123 PieceFU::new(self, x, y, sente, true)
125 PieceKY::new(self, x, y, sente, true)
127 PieceKE::new(self, x, y, sente, true)
129 PieceGI::new(self, x, y, sente, true)
131 PieceKA::new(self, x, y, sente, true)
133 PieceHI::new(self, x, y, sente, true)
135 raise "unkown piece #{name}"
148 while (str.length > 3)
149 str.sub!(/^..(..)/, '')
153 PieceFU::new(self, 0, 0, sente)
155 PieceKY::new(self, 0, 0, sente)
157 PieceKE::new(self, 0, 0, sente)
159 PieceGI::new(self, 0, 0, sente)
161 PieceKI::new(self, 0, 0, sente)
163 PieceKA::new(self, 0, 0, sente)
165 PieceHI::new(self, 0, 0, sente)
167 raise "unkown piece #{name}"
175 raise "bad line: #{str}"
180 # Set up a board starting with a position after the moves.
181 # Failing to parse the moves raises an ArgumentError.
182 # @param moves an array of moves. ex. ["+7776FU", "-3334FU"]
184 def set_from_moves(moves)
186 return :normal if moves.empty?
189 rt = handle_one_move(move, @teban)
190 raise ArgumentError, "bad moves: #{moves}" unless rt == :normal
192 @initial_moves = moves.dup
195 def have_piece?(hands, name)
196 piece = hands.find { |i|
202 def move_to(x0, y0, x1, y1, name, sente)
209 if ((x0 == 0) || (y0 == 0))
210 piece = have_piece?(hands, name)
211 return :illegal if (piece == nil || ! piece.move_to?(x1, y1, name))
212 piece.move_to(x1, y1)
214 if (@array[x0][y0] == nil || !@array[x0][y0].move_to?(x1, y1, name))
217 if (@array[x0][y0].name != name) # promoted ?
218 @array[x0][y0].promoted = true
220 if (@array[x1][y1]) # capture
221 if (@array[x1][y1].name == "OU")
222 return :outori # return board update
224 @array[x1][y1].sente = @array[x0][y0].sente
225 @array[x1][y1].move_to(0, 0)
226 hands.sort! {|a, b| # TODO refactor. Move to Piece class
230 @array[x0][y0].move_to(x1, y1)
233 @teban = @teban ? false : true
237 def look_for_ou(sente)
243 (@array[x][y].name == "OU") &&
244 (@array[x][y].sente == sente))
251 raise "can't find ou"
254 # not checkmate, but check. sente is checked.
255 def checkmated?(sente) # sente is loosing
256 ou = look_for_ou(sente)
262 (@array[x][y].sente != sente))
263 if (@array[x][y].movable_grids.include?([ou.x, ou.y]))
274 def uchifuzume?(sente)
275 rival_ou = look_for_ou(! sente) # rival's ou
276 if (sente) # rival is gote
277 if ((rival_ou.y != 9) &&
278 (@array[rival_ou.x][rival_ou.y + 1]) &&
279 (@array[rival_ou.x][rival_ou.y + 1].name == "FU") &&
280 (@array[rival_ou.x][rival_ou.y + 1].sente == sente)) # uchifu true
282 fu_y = rival_ou.y + 1
287 if ((rival_ou.y != 1) &&
288 (@array[rival_ou.x][rival_ou.y - 1]) &&
289 (@array[rival_ou.x][rival_ou.y - 1].name == "FU") &&
290 (@array[rival_ou.x][rival_ou.y - 1].sente == sente)) # uchifu true
292 fu_y = rival_ou.y - 1
298 ## case: rival_ou is moving
299 rival_ou.movable_grids.each do |(cand_x, cand_y)|
300 tmp_board = deep_copy
301 s = tmp_board.move_to(rival_ou.x, rival_ou.y, cand_x, cand_y, "OU", ! sente)
302 raise "internal error" if (s != true)
303 if (! tmp_board.checkmated?(! sente)) # good move
308 ## case: rival is capturing fu
314 (@array[x][y].sente != sente) &&
315 @array[x][y].movable_grids.include?([fu_x, fu_y])) # capturable
318 if (@array[x][y].promoted)
319 names << @array[x][y].promoted_name
321 names << @array[x][y].name
322 if @array[x][y].promoted_name &&
323 @array[x][y].move_to?(fu_x, fu_y, @array[x][y].promoted_name)
324 names << @array[x][y].promoted_name
328 tmp_board = deep_copy
329 s = tmp_board.move_to(x, y, fu_x, fu_y, name, ! sente)
333 tmp_board.checkmated?(! sente) # result
336 all_illegal = names.find {|a| a != :illegal}
337 raise "internal error: legal move not found" if all_illegal == nil
338 r = names.find {|a| a == false} # good move
339 return false if r == false # found good move
348 # @[sente|gote]_history has at least one item while the player is checking the other or
350 def update_sennichite(player)
353 if checkmated?(!player)
355 @sente_history["dummy"] = 1 # flag to see Sente player is checking Gote player
357 @gote_history["dummy"] = 1 # flag to see Gote player is checking Sente player
361 @sente_history.clear # no more continuous check
363 @gote_history.clear # no more continuous check
366 if @sente_history.size > 0 # possible for Sente's or Gote's turn
367 @sente_history[str] += 1
369 if @gote_history.size > 0 # possible for Sente's or Gote's turn
370 @gote_history[str] += 1
374 def oute_sennichite?(player)
375 return nil unless sennichite?
379 if (@sente_history[to_s] >= 4) # sente is checking gote
380 return :oute_sennichite_sente_lose
381 elsif (@gote_history[to_s] >= 3) # sente is escaping
382 return :oute_sennichite_gote_lose
384 return nil # Not oute_sennichite, but sennichite
388 if (@gote_history[to_s] >= 4) # gote is checking sente
389 return :oute_sennichite_gote_lose
390 elsif (@sente_history[to_s] >= 3) # gote is escaping
391 return :oute_sennichite_sente_lose
393 return nil # Not oute_sennichite, but sennichite
399 if (@history[to_s] >= 4) # already 3 times
405 def good_kachi?(sente)
406 if (checkmated?(sente))
407 puts "'NG: Checkmating." if $DEBUG
411 ou = look_for_ou(sente)
412 if (sente && (ou.y >= 4))
413 puts "'NG: Black's OU does not enter yet." if $DEBUG
416 if (! sente && (ou.y <= 6))
417 puts "'NG: White's OU does not enter yet." if $DEBUG
435 (@array[x][y].sente == sente) &&
436 (@array[x][y].point > 0))
437 point = point + @array[x][y].point
443 hands.each do |piece|
444 point = point + piece.point
448 puts "'NG: Piece#[%d] is too small." % [number] if $DEBUG
453 puts "'NG: Black's point#[%d] is too small." % [point] if $DEBUG
458 puts "'NG: White's point#[%d] is too small." % [point] if $DEBUG
463 puts "'Good: Piece#[%d], Point[%d]." % [number, point] if $DEBUG
467 # sente is nil only if tests in test_board run
468 def handle_one_move(str, sente=nil)
469 if (str =~ /^([\+\-])(\d)(\d)(\d)(\d)([A-Z]{2})/)
476 elsif (str =~ /^%KACHI/)
477 raise ArgumentError, "sente is null", caller if sente == nil
478 if (good_kachi?(sente))
483 elsif (str =~ /^%TORYO/)
489 if (((x0 == 0) || (y0 == 0)) && # source is not from hand
490 ((x0 != 0) || (y0 != 0)))
492 elsif ((x1 == 0) || (y1 == 0)) # destination is out of board
497 sente = true if sente == nil # deprecated
498 return :illegal unless sente == true # black player's move must be black
501 sente = false if sente == nil # deprecated
502 return :illegal unless sente == false # white player's move must be white
507 if ((x0 == 0) && (y0 == 0))
508 return :illegal if (! have_piece?(hands, name))
509 elsif (! @array[x0][y0])
510 return :illegal # no piece
511 elsif (@array[x0][y0].sente != sente)
512 return :illegal # this is not mine
513 elsif (@array[x0][y0].name != name)
514 return :illegal if (@array[x0][y0].promoted_name != name) # can't promote
518 if (@array[x1][y1] &&
519 (@array[x1][y1].sente == sente)) # can't capture mine
521 elsif ((x0 == 0) && (y0 == 0) && @array[x1][y1])
522 return :illegal # can't put on existing piece
525 tmp_board = deep_copy
526 return :illegal if (tmp_board.move_to(x0, y0, x1, y1, name, sente) == :illegal)
527 return :oute_kaihimore if (tmp_board.checkmated?(sente))
528 tmp_board.update_sennichite(sente)
529 os_result = tmp_board.oute_sennichite?(sente)
530 return os_result if os_result # :oute_sennichite_sente_lose or :oute_sennichite_gote_lose
531 return :sennichite if tmp_board.sennichite?
533 if ((x0 == 0) && (y0 == 0) && (name == "FU") && tmp_board.uchifuzume?(sente))
537 move_to(x0, y0, x1, y1, name, sente)
539 update_sennichite(sente)
547 a.push(sprintf("P%d", y))
559 a.push(sprintf("\n"))
562 if (! sente_hands.empty?)
564 sente_hands.each do |p|
565 a.push("00" + p.name)
569 if (! gote_hands.empty?)
571 gote_hands.each do |p|
572 a.push("00" + p.name)
576 a.push("%s\n" % [@teban ? "+" : "-"])