
module Game
  attr_reader  :game, :moves
  attr_reader :height, :width
  attr_reader :current_node, :move_nb, :w_pris, :b_pris

  def initialize
    @clocks = {'W' => -1, 'B' => -1 }
    @byos = {'W' => -1, 'B'=> -1}
    @w_pris = @b_pris = 0;
  end
  def copy_game
    gm=Array.new(@width+2)
    for i in -1..@width
      gm[i]=Array.new(@height+2)
      for j in -1..@height
        gm[i][j]=  @game[i][j]
      end
    end
    gm
  end
  def to_ascii
     str ='   '
      for i in 0...@width
	 str << Grid::Hlabels[i] << ' '
      end
      str << "\n"

    c= ?.
    for j in 0...@height
str << "%2d"%Grid::Vlabels[j] << ' ' 
      for i in 0...@width
        c= ( case @game[i][j]

        when ?E
       ?.
        when ?W
          ?O
        when ?B
          ?X
        end
           )
        if [3,9,15].include?(i)	  and  [3,9,15].include?(j) and c== ?.
          c= ?,
        end
        str  <<  c << ' '  
      end
      str << "%2d"%Grid::Vlabels[j] << "\n"
    end
  str << '   '
  for i in 0...@width
	 str << Grid::Hlabels[i] << ' '
      end
      str << "\n"

    str
  end
  def set_node(node)
    @current_node = node
    @current_node.action(self)
  end
  def set_fast_node(node)
    @current_node = node
    @current_node.fast_action(self)
  end
  def undo_node(node= @current_node)
    @current_node.undo(self)
  end
  def undo_fast_node
    @current_node.fast_undo(self)
  end
  def set_move_nb(nb)
    @move_nb = nb
  end
  def set_pris(w,b)
    @w_pris=w
    @b_pris=b
  end
  def set_root(root)
    undo_node(@root) if @root
    @root = root
    if  @root['SZ']
      @width,@height = @root['SZ'] 
    else
      @width= @height= 19
    end
    i=j=0
    @game=Array.new(@width+2)
    for i in -1..@width
      @game[i]=Array.new(@height+2)
      for j in -1..@height
        if i==-1 or i==@width or j==-1 or j == @height
          @game[i][j]= ?N
        else
          @game[i][j]= ?E
        end
      end
    end

    i=j=0
    @moves=Array.new(@width)
    for i in 0...@width
      @moves[i]=Array.new(@height)
      for j in 0...@height
        @moves[i][j]= -1
      end
    end

    @clocks = {'W' => -1, 'B' => -1 }
    @byos = {'W' => -1, 'B'=> -1}
    @w_pris = @b_pris = 0;

    @move_nb= @root.move_nb= 0
  end
  def set_turn (color)
    @player_turn = color
  end
  def clear_turn(color)
    @player_turn= nil
  end
  def delete_stone(col,row)
    @game[col][row]= ?E
    @moves[col][row]= -1
  end 
  def put_stone(color,col,row,nb)
#   debug "#{color}  #{col} #{row} #{nb}"
    if row
      @game[col][row]= color 
      @moves[col][row]= nb
    end
  end
  def add_pris(color,col,row,nb)
#   debug "#{color}  #{col} #{row} #{nb}"
    @game[col][row]= ?E
  end
  def del_pris(color,col,row,nb)
#   debug "#{color}  #{col} #{row} #{nb}"
    put_stone(color,col,row,nb)
  end
  def set_stone(c,col,row)
    @game[col][row]= c
  end
  def set_clock(who,clock)
    @clocks[who]= clock
  end
  def set_byo(who,byo)
    @byos[who]= byo
  end
def get_komi
   @root['KM'].to_f
end

# virtual methods
  def set_title(title); end
  def set_node_name(txt); end
  def set_title(title);end
  def set_node_name(txt);end
  def set_result(line); end
  def set_msg(msg = nil); end
  def set_info_msg(line); end
  def set_move_label(str); end
  def set_show_move_nb(val) ; end
  def set_show_coords(v=nil);end
  def set_show_hoshis(v=nil);end
  def set_show_diagram_name(v=nil);end
  def set_diagram_name(name) ;end
  def set_show_list_moves (v=nil);end
  def set_remove_captured (v=nil);end
  def add_comment(line);end
  def clear_comment;end
  def set_node_annot(txt);end
  def set_annot(txt);end
  def set_byo_time(v);end
  def set_time(v);end
  def set_number(v);end
  def set_type(v);end
  def set_handi(h);end
  def set_komi(k);end
  def set_rank(who,r);end
  def set_name(who,n);end





end # module Game


