search.rb
# Organizes the data collected during the search for rendering in the browser
Organizes the data collected during the search for rendering in the browser
Organizes the data collected during the search for rendering in the browser
def render
    total_pv, total_nw, total_qs = @npp_pv.inject(:+), @npp_nw.inject(:+), @npp_qs.inject(:+)
    total_nodes = total_pv + total_nw + total_qs
    moves = Array.new(64) { [] }
    1.upto(@root_n - 1) do |i|
      idx = @mv_idx[i]
      next unless @mv_legal[idx]
      m = @mv[idx]
      moves[SQ64[m.from]] << { to: SQ64[m.to], san: @mv_san[idx], fen: @mv_fen[idx], type: @mv_type[idx] }
    end
    board =
    {
      fen: @fen,
      nps: (total_nodes.to_f / @clock).round(2),
      clock: "#{@clock.round(2)} / #{@duration}",
      move: @best_move,
      pv_count: total_pv,
      nw_count: total_nw,
      qs_count: total_qs,
      node_count: total_nodes,
      board: SQ.map { |sq| @squares[sq] == EMPTY ? "" : UTF8[@colors[sq]][@squares[sq]] },
      moves: moves,
      pv_board: @pv_chart.map { |sq| (sq[:piece] != '' && sq[:depth] > 0) ?
        { piece: sq[:piece], class: "p#{sq[:ply]} d#{sq[:depth]}"} : { piece: "", class: "" } },
      pv_list: @pv_list.reverse.select { |m| m[:depth] > 0 }.map { |m|
        { moves: m[:moves], class: "d#{m[:depth]}", depth: m[:depth] } },
      evaluation: [
        { field: "Material", white: @w_material, black: @b_material },
        { field: "Mobility", white: @w_mobility, black: @b_mobility },
        { field: "Position", white: @w_position, black: @b_position },
        { field: "Control", white: @w_control, black: @b_control },
        { field: "Structure", white: @w_pawns, black: @b_pawns },
        { field: "Threat", white: @w_threat, black: @b_threat },
        { field: "Total", white: @w_total, black: @b_total }],
      npp: [
        ['pv', @npp_pv[2, 12]].flatten,
        ['nw', @npp_nw[2, 12]].flatten,
        ['qs', @npp_qs[2, 12]].flatten]
    }
  end
def render total_pv , total_nw , total_qs = @npp_pv . inject ( :+ ) , @npp_nw . inject ( :+ ) , @npp_qs . inject ( :+ ) total_nodes = total_pv + total_nw + total_qs moves = Array . new ( 64 ) { [ ] } 1 . upto ( @root_n - 1 ) do | i | idx = @mv_idx [ i ] next unless @mv_legal [ idx ] m = @mv [ idx ] moves [ SQ64 [ m . from ] ] << { to : SQ64 [ m . to ] , san : @mv_san [ idx ] , fen : @mv_fen [ idx ] , type : @mv_type [ idx ] } end board = { fen : @fen , nps : ( total_nodes . to_f / @clock ) . round ( 2 ) , clock : "#{@clock.round(2)} / #{@duration}" , move : @best_move , pv_count : total_pv , nw_count : total_nw , qs_count : total_qs , node_count : total_nodes , board : SQ . map { | sq | @squares [ sq ] == EMPTY ? "" : UTF8 [ @colors [ sq ] ] [ @squares [ sq ] ] } , moves : moves , pv_board : @pv_chart . map { | sq | ( sq [ :piece ] != '' && sq [ :depth ] > 0 ) ? { piece : sq [ :piece ] , class : "p#{sq[:ply]} d#{sq[:depth]}" } : { piece : "" , class : "" } } , pv_list : @pv_list . reverse . select { | m | m [ :depth ] > 0 } . map { | m | { moves : m [ :moves ] , class : "d#{m[:depth]}" , depth : m [ :depth ] } } , evaluation : [ { field : "Material" , white : @w_material , black : @b_material } , { field : "Mobility" , white : @w_mobility , black : @b_mobility } , { field : "Position" , white : @w_position , black : @b_position } , { field : "Control" , white : @w_control , black : @b_control } , { field : "Structure" , white : @w_pawns , black : @b_pawns } , { field : "Threat" , white : @w_threat , black : @b_threat } , { field : "Total" , white : @w_total , black : @b_total } ] , npp : [ [ 'pv' , @npp_pv [ 2 , 12 ] ] . flatten , [ 'nw' , @npp_nw [ 2 , 12 ] ] . flatten , [ 'qs' , @npp_qs [ 2 , 12 ] ] . flatten ] } end
Organizes the data collected during the search for rendering in the browser
Organizes the data collected during the search for rendering in the browser