include Debug



class GobanSc < GoSimple

attr_reader :nb_pris_w, :nb_pris_b

def initialize(parent,dimx, dimy,pw,pb,km)

	@nb_pris_w= pw
	@nb_pris_b= pb
	@nb_komi= km

 super(parent,dimx,dimy)
    @h_margin= 4

    @canvas.configure('closeenough' => Defaults::DEFS['close_enough'])
create_score
 @canvas.bind("ButtonPress-1",proc { |ev| do_click_1(ev)})
 @canvas.bind("ButtonPress-3",proc { |ev| do_click_3(ev)})

	@canvas.focus


	end


	def create_score

	@scframe= TkFrame.new(self)
	TkLabel.new(@scframe,'text'=>'white','relief'=>'raised').grid('column'=>0,'row'=>1,'sticky'=>'ew')
	TkLabel.new(@scframe,'text'=>'black','relief'=>'raised').grid('column'=>0,'row'=>2,'sticky'=>'ew')
	#	TkLabel.new(@scframe,'text'=>'territory','relief'=>'raised').grid('column'=>1,'row'=>0)
	TkLabel.new(@scframe,'text'=>'pris','relief'=>'raised').grid('column'=>1,'row'=>0,'sticky'=>'ew')
	@w_pris=TkLabel.new(@scframe,'relief'=>'ridge','text'=>"#@nb_pris_w").grid('column'=>1,'row'=>1,'sticky'=>'ew')
	@b_pris=TkLabel.new(@scframe,'relief'=>'ridge','text'=>"#@nb_pris_b").grid('column'=>1,'row'=>2,'sticky'=>'ew')
	TkLabel.new(@scframe,'text'=>'total','relief'=>'raised').grid('column'=>2,'row'=>0,'sticky'=>'ew')
	@w_total=TkLabel.new(@scframe,'relief'=>'ridge','text'=>'0').grid('column'=>2,'row'=>1,'sticky'=>'ew')
	@b_total=TkLabel.new(@scframe,'relief'=>'ridge','text'=>'0').grid('column'=>2,'row'=>2,'sticky'=>'ew')
	TkLabel.new(@scframe,'text'=>'komi','relief'=>'raised').grid('column'=>0,'row'=>3,'sticky'=>'ew')
	@komi= TkLabel.new(@scframe,'relief'=>'ridge','text'=>"#@nb_komi").grid('column'=>1,'row'=>3,'sticky'=>'ew')
	#	TkLabel.new(@scframe,'text'=>'result','relief'=>'raised').grid('column'=>4,'row'=>2,'sticky'=>'ew')
	@result= TkLabel.new(@scframe,'relief'=>'ridge','text'=>'0').grid('column'=>2,'row'=>3,'sticky'=>'ew')

	end

	def do_resize(event)
		super(event)
		@scframe.place('x'=>(@dimx+4)*@step,'y'=>0)
	end

def add_pris(color,nb)
	if color == 'W'
	@nb_pris_w += nb
	@w_pris.configure('text'=> " #@nb_pris_w")
	else
	@nb_pris_b += nb
	@b_pris.configure('text'=> "#@nb_pris_b")
	end
	end

def sub_point(color,nb)
	if color == 'W'
	@nb_point_w -= nb
	else
	@nb_point_b -= nb
	end
set_score(@nb_point_b,@nb_point_w)
	end

def set_score(b,w)
	@nb_point_w= w
	@nb_point_b = b
	@w_total.configure('text'=> "#{w}")
	@b_total.configure('text'=> "#{b}")
	if  @nb_point_w > @nb_point_b   
	@result.configure('text'=> "W+ #{@nb_point_w - @nb_point_b}")
	else
	@result.configure('text'=> "B+ #{@nb_point_b - @nb_point_w}")
	end
	end

	def  do_click_1(ev)
		col,row = @grid.grid_round(ev.x,ev.y)
  if col and row
	if @board[col][row] 
		@group= make_group( @board[col][row]) 
		@group.each {|stone|
		op = (stone.color == 'W' ? 'B' : 'W')
			add_pris(op,1)
			delete(stone)
		}

    else
      set_obj(Stone,'B',col,row)
	end
	end
end

	def  do_click_3(ev)
		col,row = @grid.grid_round(ev.x,ev.y)
  if col and row
	if @board[col][row] 
		@group= make_group( @board[col][row]) 
		@group.each {|stone|
		op = (stone.color == 'W' ? 'B' : 'W')
			add_pris(op,1)
			delete(stone)
		}
    	else
      set_obj(Stone,'W',col,row)
	end
	end
end



	end


