+ def initialize
+ @sente_hands = Array::new
+ @gote_hands = Array::new
+ @history = Hash::new
+ @sente_history = Hash::new
+ @gote_history = Hash::new
+ @array = [[], [], [], [], [], [], [], [], [], []]
+ end
+ attr_accessor :array, :sente_hands, :gote_hands, :history, :sente_history, :gote_history
+
+ def initial
+ PieceKY::new(self, 1, 1, false)
+ PieceKE::new(self, 2, 1, false)
+ PieceGI::new(self, 3, 1, false)
+ PieceKI::new(self, 4, 1, false)
+ PieceOU::new(self, 5, 1, false)
+ PieceKI::new(self, 6, 1, false)
+ PieceGI::new(self, 7, 1, false)
+ PieceKE::new(self, 8, 1, false)
+ PieceKY::new(self, 9, 1, false)
+ PieceKA::new(self, 2, 2, false)
+ PieceHI::new(self, 8, 2, false)
+ PieceFU::new(self, 1, 3, false)
+ PieceFU::new(self, 2, 3, false)
+ PieceFU::new(self, 3, 3, false)
+ PieceFU::new(self, 4, 3, false)
+ PieceFU::new(self, 5, 3, false)
+ PieceFU::new(self, 6, 3, false)
+ PieceFU::new(self, 7, 3, false)
+ PieceFU::new(self, 8, 3, false)
+ PieceFU::new(self, 9, 3, false)
+
+ PieceKY::new(self, 1, 9, true)
+ PieceKE::new(self, 2, 9, true)
+ PieceGI::new(self, 3, 9, true)
+ PieceKI::new(self, 4, 9, true)
+ PieceOU::new(self, 5, 9, true)
+ PieceKI::new(self, 6, 9, true)
+ PieceGI::new(self, 7, 9, true)
+ PieceKE::new(self, 8, 9, true)
+ PieceKY::new(self, 9, 9, true)
+ PieceKA::new(self, 8, 8, true)
+ PieceHI::new(self, 2, 8, true)
+ PieceFU::new(self, 1, 7, true)
+ PieceFU::new(self, 2, 7, true)
+ PieceFU::new(self, 3, 7, true)
+ PieceFU::new(self, 4, 7, true)
+ PieceFU::new(self, 5, 7, true)
+ PieceFU::new(self, 6, 7, true)
+ PieceFU::new(self, 7, 7, true)
+ PieceFU::new(self, 8, 7, true)
+ PieceFU::new(self, 9, 7, true)
+ end
+
+ def have_piece?(hands, name)
+ piece = hands.find { |i|
+ i.name == name
+ }
+ return piece
+ end
+
+ def move_to(x0, y0, x1, y1, name, sente)
+ if (sente)
+ hands = @sente_hands
+ else
+ hands = @gote_hands
+ end
+
+ if ((x0 == 0) || (y0 == 0))
+ piece = have_piece?(hands, name)
+ return :illegal if (! piece.move_to?(x1, y1, name))
+ piece.move_to(x1, y1)
+ else
+ return :illegal if (! @array[x0][y0].move_to?(x1, y1, name))
+ if (@array[x0][y0].name != name) # promoted ?
+ @array[x0][y0].promoted = true
+ end
+ if (@array[x1][y1])
+ if (@array[x1][y1].name == "OU")
+ return :outori # return board update
+ end
+ @array[x1][y1].sente = @array[x0][y0].sente
+ @array[x1][y1].move_to(0, 0)
+ hands.sort! {|a, b|
+ a.name <=> b.name
+ }
+ end
+ @array[x0][y0].move_to(x1, y1)
+ end
+ return true
+ end
+
+ def look_for_ou(sente)
+ x = 1
+ while (x <= 9)
+ y = 1
+ while (y <= 9)
+ if (@array[x][y] &&
+ (@array[x][y].name == "OU") &&
+ (@array[x][y].sente == sente))
+ return @array[x][y]
+ end
+ y = y + 1
+ end
+ x = x + 1
+ end
+ raise "can't find ou"
+ end
+
+ def checkmated?(sente) # sente is loosing
+ ou = look_for_ou(sente)
+ x = 1
+ while (x <= 9)
+ y = 1
+ while (y <= 9)
+ if (@array[x][y] &&
+ (@array[x][y].sente != sente))
+ if (@array[x][y].movable_grids.include?([ou.x, ou.y]))
+ return true
+ end
+ end
+ y = y + 1
+ end
+ x = x + 1
+ end
+ return false
+ end
+
+ def uchifuzume?(sente)
+ rival_ou = look_for_ou(! sente) # rival's ou
+ if (sente) # rival is gote
+ if ((rival_ou.y != 9) &&
+ (@array[rival_ou.x][rival_ou.y + 1]) &&
+ (@array[rival_ou.x][rival_ou.y + 1].name == "FU") &&
+ (@array[rival_ou.x][rival_ou.y + 1].sente == sente)) # uchifu true
+ fu_x = rival_ou.x
+ fu_y = rival_ou.y + 1
+ else
+ return false
+ end
+ else # gote
+ if ((rival_ou.y != 0) &&
+ (@array[rival_ou.x][rival_ou.y - 1]) &&
+ (@array[rival_ou.x][rival_ou.y - 1].name == "FU") &&
+ (@array[rival_ou.x][rival_ou.y - 1].sente == sente)) # uchifu true
+ fu_x = rival_ou.x
+ fu_y = rival_ou.y - 1
+ else
+ return false
+ end
+ end
+
+ ## case: rival_ou is moving
+ escaped = false
+ rival_ou.movable_grids.each do |(cand_x, cand_y)|
+ tmp_board = Marshal.load(Marshal.dump(self))
+ s = tmp_board.move_to(rival_ou.x, rival_ou.y, cand_x, cand_y, "OU", ! sente)
+ raise "internal error" if (s != true)
+ if (! tmp_board.checkmated?(! sente)) # good move
+ return false
+ end
+ end
+
+ ## case: rival is capturing fu
+ x = 1
+ while (x <= 9)
+ y = 1
+ while (y <= 9)
+ if (@array[x][y] &&
+ (@array[x][y].sente != sente) &&
+ @array[x][y].movable_grids.include?([fu_x, fu_y])) # capturable
+ if (@array[x][y].promoted)
+ name = @array[x][y].promoted_name
+ else
+ name = @array[x][y].name
+ end
+ tmp_board = Marshal.load(Marshal.dump(self))
+ s = tmp_board.move_to(x, y, fu_x, fu_y, name, ! sente)
+ raise "internal error" if (s != true)
+ if (! tmp_board.checkmated?(! sente)) # good move
+ return false
+ end
+ end
+ y = y + 1
+ end
+ x = x + 1
+ end
+ return true
+ end
+
+ def oute_sennichite?(sente)
+ if (checkmated?(! sente))
+ str = to_s
+ if (sente)
+ if (@sente_history[str] && (@sente_history[str] >= 3)) # already 3 times
+ return true
+ end
+ else
+ if (@gote_history[str] && (@gote_history[str] >= 3)) # already 3 times
+ return true
+ end
+ end
+ end
+ return false
+ end
+
+ def sennichite?(sente)
+ str = to_s
+ if (@history[str] && (@history[str] >= 3)) # already 3 times
+ return true
+ end
+ return false
+ end
+
+ def good_kachi?(sente)
+ return false if (checkmated?(sente))
+ ou = look_for_ou(sente)
+ return false if (sente && (ou.y >= 4))
+ return false if (! sente && (ou.y <= 6))
+
+ number = 0
+ point = 0
+
+ if (sente)
+ hands = @sente_hands
+ r = [1, 2, 3]
+ else
+ hands = @gote_hands
+ r = [7, 8, 9]
+ end
+ r.each do |y|
+ x = 1
+ while (x <= 9)
+ if (@array[x][y] &&
+ (@array[x][y].sente == sente) &&
+ (@array[x][y].point > 0))
+ point = point + @array[x][y].point
+ number = number + 1
+ end
+ x = x + 1
+ end
+ end
+ hands.each do |piece|
+ point = point + piece.point
+ end
+
+ return false if (number < 10)
+ if (sente)
+ return false if (point < 28)
+ else
+ return false if (point < 27)
+ end
+ return true
+ end
+
+ def handle_one_move(str)
+ if (str =~ /^([\+\-])(\d)(\d)(\d)(\d)([A-Z]{2})/)
+ sg = $1
+ x0 = $2.to_i
+ y0 = $3.to_i
+ x1 = $4.to_i
+ y1 = $5.to_i
+ name = $6
+ elsif (str =~ /^%KACHI/)
+ if (@sente == @current_player)
+ sente = true
+ else
+ sente = false
+ end
+ if (good_kachi?(sente))
+ return :kachi_win
+ else
+ return :kachi_lose
+ end
+ elsif (str =~ /^%TORYO/)
+ return :toryo
+ else
+ return :illegal
+ end
+
+ if (((x0 == 0) || (y0 == 0)) && # source is not from hand
+ ((x0 != 0) || (y0 != 0)))
+ return :illegal
+ elsif ((x1 == 0) || (y1 == 0)) # destination is out of board
+ return :illegal
+ end
+
+ if (sg == "+")
+ sente = true
+ hands = @sente_hands
+ else
+ sente = false
+ hands = @gote_hands
+ end
+
+ ## source check
+ if ((x0 == 0) && (y0 == 0))
+ return :illegal if (! have_piece?(hands, name))
+ elsif (! @array[x0][y0])
+ return :illegal # no piece
+ elsif (@array[x0][y0].sente != sente)
+ return :illegal # this is not mine
+ elsif (@array[x0][y0].name != name)
+ return :illegal if (@array[x0][y0].promoted_name != name) # can't promote
+ end
+
+ ## destination check
+ if (@array[x1][y1] &&
+ (@array[x1][y1].sente == sente)) # can't capture mine
+ return :illegal
+ elsif ((x0 == 0) && (y0 == 0) && @array[x1][y1])
+ return :illegal # can't put on existing piece
+ end
+
+ tmp_board = Marshal.load(Marshal.dump(self))
+ return :illegal if (tmp_board.move_to(x0, y0, x1, y1, name, sente) == :illegal)
+ return :oute_kaihimore if (tmp_board.checkmated?(sente))
+ return :oute_sennichite if tmp_board.oute_sennichite?(sente)
+ return :sennichite if tmp_board.sennichite?(sente)
+
+ if ((x0 == 0) && (y0 == 0) && (name == "FU") && tmp_board.uchifuzume?(sente))
+ return :uchifuzume
+ end
+
+ move_to(x0, y0, x1, y1, name, sente)
+ str = to_s
+
+ if (checkmated?(! sente))
+ if (sente)
+ @sente_history[str] = (@sente_history[str] || 0) + 1
+ else
+ @gote_history[str] = (@gote_history[str] || 0) + 1
+ end
+ else
+ if (sente)
+ @sente_history.clear
+ else
+ @gote_history.clear
+ end
+ end
+ @history[str] = (@history[str] || 0) + 1
+ return :normal
+ end
+
+ def to_s
+ a = Array::new
+ y = 1
+ while (y <= 9)
+ a.push(sprintf("P%d", y))
+ x = 9
+ while (x >= 1)
+ piece = @array[x][y]
+ if (piece)
+ s = piece.to_s
+ else
+ s = " * "
+ end
+ a.push(s)
+ x = x - 1
+ end
+ a.push(sprintf("\n"))
+ y = y + 1
+ end
+ if (! sente_hands.empty?)
+ a.push("P+")
+ sente_hands.each do |p|
+ a.push("00" + p.name)
+ end
+ a.push("\n")
+ end
+ if (! gote_hands.empty?)
+ a.push("P-")
+ gote_hands.each do |p|
+ a.push("00" + p.name)
+ end
+ a.push("\n")
+ end
+ a.push("+\n")
+ return a.join
+ end