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 require 'shogi_server/piece'
22 module ShogiServer # for a namespace
26 @sente_hands = Array::new
27 @gote_hands = Array::new
28 @history = Hash::new(0)
29 @sente_history = Hash::new(0)
30 @gote_history = Hash::new(0)
31 @array = [[], [], [], [], [], [], [], [], [], []]
33 @teban = nil # black => true, white => false
35 attr_accessor :array, :sente_hands, :gote_hands, :history, :sente_history, :gote_history
36 attr_reader :move_count
39 PieceKY::new(self, 1, 1, false)
40 PieceKE::new(self, 2, 1, false)
41 PieceGI::new(self, 3, 1, false)
42 PieceKI::new(self, 4, 1, false)
43 PieceOU::new(self, 5, 1, false)
44 PieceKI::new(self, 6, 1, false)
45 PieceGI::new(self, 7, 1, false)
46 PieceKE::new(self, 8, 1, false)
47 PieceKY::new(self, 9, 1, false)
48 PieceKA::new(self, 2, 2, false)
49 PieceHI::new(self, 8, 2, false)
51 PieceFU::new(self, i, 3, false)
54 PieceKY::new(self, 1, 9, true)
55 PieceKE::new(self, 2, 9, true)
56 PieceGI::new(self, 3, 9, true)
57 PieceKI::new(self, 4, 9, true)
58 PieceOU::new(self, 5, 9, true)
59 PieceKI::new(self, 6, 9, true)
60 PieceGI::new(self, 7, 9, true)
61 PieceKE::new(self, 8, 9, true)
62 PieceKY::new(self, 9, 9, true)
63 PieceKA::new(self, 8, 8, true)
64 PieceHI::new(self, 2, 8, true)
66 PieceFU::new(self, i, 7, true)
71 def have_piece?(hands, name)
72 piece = hands.find { |i|
78 def move_to(x0, y0, x1, y1, name, sente)
85 if ((x0 == 0) || (y0 == 0))
86 piece = have_piece?(hands, name)
87 return :illegal if (! piece.move_to?(x1, y1, name)) # TODO null check for the piece?
90 return :illegal if (! @array[x0][y0].move_to?(x1, y1, name)) # TODO null check?
91 if (@array[x0][y0].name != name) # promoted ?
92 @array[x0][y0].promoted = true
94 if (@array[x1][y1]) # capture
95 if (@array[x1][y1].name == "OU")
96 return :outori # return board update
98 @array[x1][y1].sente = @array[x0][y0].sente
99 @array[x1][y1].move_to(0, 0)
100 hands.sort! {|a, b| # TODO refactor. Move to Piece class
104 @array[x0][y0].move_to(x1, y1)
107 @teban = @teban ? false : true
111 def look_for_ou(sente)
117 (@array[x][y].name == "OU") &&
118 (@array[x][y].sente == sente))
125 raise "can't find ou"
128 # note checkmate, but check. sente is checked.
129 def checkmated?(sente) # sente is loosing
130 ou = look_for_ou(sente)
136 (@array[x][y].sente != sente))
137 if (@array[x][y].movable_grids.include?([ou.x, ou.y]))
148 def uchifuzume?(sente)
149 rival_ou = look_for_ou(! sente) # rival's ou
150 if (sente) # rival is gote
151 if ((rival_ou.y != 9) &&
152 (@array[rival_ou.x][rival_ou.y + 1]) &&
153 (@array[rival_ou.x][rival_ou.y + 1].name == "FU") &&
154 (@array[rival_ou.x][rival_ou.y + 1].sente == sente)) # uchifu true
156 fu_y = rival_ou.y + 1
161 if ((rival_ou.y != 1) &&
162 (@array[rival_ou.x][rival_ou.y - 1]) &&
163 (@array[rival_ou.x][rival_ou.y - 1].name == "FU") &&
164 (@array[rival_ou.x][rival_ou.y - 1].sente == sente)) # uchifu true
166 fu_y = rival_ou.y - 1
172 ## case: rival_ou is moving
173 rival_ou.movable_grids.each do |(cand_x, cand_y)|
174 tmp_board = Marshal.load(Marshal.dump(self))
175 s = tmp_board.move_to(rival_ou.x, rival_ou.y, cand_x, cand_y, "OU", ! sente)
176 raise "internal error" if (s != true)
177 if (! tmp_board.checkmated?(! sente)) # good move
182 ## case: rival is capturing fu
188 (@array[x][y].sente != sente) &&
189 @array[x][y].movable_grids.include?([fu_x, fu_y])) # capturable
192 if (@array[x][y].promoted)
193 names << @array[x][y].promoted_name
195 names << @array[x][y].name
196 if @array[x][y].promoted_name &&
197 @array[x][y].move_to?(fu_x, fu_y, @array[x][y].promoted_name)
198 names << @array[x][y].promoted_name
202 tmp_board = Marshal.load(Marshal.dump(self))
203 s = tmp_board.move_to(x, y, fu_x, fu_y, name, ! sente)
207 tmp_board.checkmated?(! sente) # result
210 all_illegal = names.find {|a| a != :illegal}
211 raise "internal error: legal move not found" if all_illegal == nil
212 r = names.find {|a| a == false} # good move
213 return false if r == false # found good move
222 # @[sente|gote]_history has at least one item while the player is checking the other or
224 def update_sennichite(player)
227 if checkmated?(!player)
229 @sente_history["dummy"] = 1 # flag to see Sente player is checking Gote player
231 @gote_history["dummy"] = 1 # flag to see Gote player is checking Sente player
235 @sente_history.clear # no more continuous check
237 @gote_history.clear # no more continuous check
240 if @sente_history.size > 0 # possible for Sente's or Gote's turn
241 @sente_history[str] += 1
243 if @gote_history.size > 0 # possible for Sente's or Gote's turn
244 @gote_history[str] += 1
248 def oute_sennichite?(player)
249 if (@sente_history[to_s] >= 4)
250 return :oute_sennichite_sente_lose
251 elsif (@gote_history[to_s] >= 4)
252 return :oute_sennichite_gote_lose
258 def sennichite?(sente)
259 if (@history[to_s] >= 4) # already 3 times
265 def good_kachi?(sente)
266 if (checkmated?(sente))
267 puts "'NG: Checkmating." if $DEBUG
271 ou = look_for_ou(sente)
272 if (sente && (ou.y >= 4))
273 puts "'NG: Black's OU does not enter yet." if $DEBUG
276 if (! sente && (ou.y <= 6))
277 puts "'NG: White's OU does not enter yet." if $DEBUG
295 (@array[x][y].sente == sente) &&
296 (@array[x][y].point > 0))
297 point = point + @array[x][y].point
303 hands.each do |piece|
304 point = point + piece.point
308 puts "'NG: Piece#[%d] is too small." % [number] if $DEBUG
313 puts "'NG: Black's point#[%d] is too small." % [point] if $DEBUG
318 puts "'NG: White's point#[%d] is too small." % [point] if $DEBUG
323 puts "'Good: Piece#[%d], Point[%d]." % [number, point] if $DEBUG
327 # sente is nil only if tests in test_board run
328 def handle_one_move(str, sente=nil)
329 if (str =~ /^([\+\-])(\d)(\d)(\d)(\d)([A-Z]{2})/)
336 elsif (str =~ /^%KACHI/)
337 raise ArgumentError, "sente is null", caller if sente == nil
338 if (good_kachi?(sente))
343 elsif (str =~ /^%TORYO/)
349 if (((x0 == 0) || (y0 == 0)) && # source is not from hand
350 ((x0 != 0) || (y0 != 0)))
352 elsif ((x1 == 0) || (y1 == 0)) # destination is out of board
357 sente = true if sente == nil # deprecated
358 return :illegal unless sente == true # black player's move must be black
361 sente = false if sente == nil # deprecated
362 return :illegal unless sente == false # white player's move must be white
367 if ((x0 == 0) && (y0 == 0))
368 return :illegal if (! have_piece?(hands, name))
369 elsif (! @array[x0][y0])
370 return :illegal # no piece
371 elsif (@array[x0][y0].sente != sente)
372 return :illegal # this is not mine
373 elsif (@array[x0][y0].name != name)
374 return :illegal if (@array[x0][y0].promoted_name != name) # can't promote
378 if (@array[x1][y1] &&
379 (@array[x1][y1].sente == sente)) # can't capture mine
381 elsif ((x0 == 0) && (y0 == 0) && @array[x1][y1])
382 return :illegal # can't put on existing piece
385 tmp_board = Marshal.load(Marshal.dump(self))
386 return :illegal if (tmp_board.move_to(x0, y0, x1, y1, name, sente) == :illegal)
387 return :oute_kaihimore if (tmp_board.checkmated?(sente))
388 tmp_board.update_sennichite(sente)
389 os_result = tmp_board.oute_sennichite?(sente)
390 return os_result if os_result # :oute_sennichite_sente_lose or :oute_sennichite_gote_lose
391 return :sennichite if tmp_board.sennichite?(sente)
393 if ((x0 == 0) && (y0 == 0) && (name == "FU") && tmp_board.uchifuzume?(sente))
397 move_to(x0, y0, x1, y1, name, sente)
399 update_sennichite(sente)
407 a.push(sprintf("P%d", y))
419 a.push(sprintf("\n"))
422 if (! sente_hands.empty?)
424 sente_hands.each do |p|
425 a.push("00" + p.name)
429 if (! gote_hands.empty?)
431 gote_hands.each do |p|
432 a.push("00" + p.name)
436 a.push("%s\n" % [@teban ? "+" : "-"])