class Sudoku::MainWindow

This class represents main window for the Sudoku GUI.

Public Instance Methods

cell_background(cell) click to toggle source

Will determine and return background for cell

# File lib/sudoku/main_window.rb, line 112
def cell_background(cell)
  color1 = rgb(108, 140, 236)
  color2 = rgb(213, 152, 108)
  x, y = cell.coordinates.coordinate_x, cell.coordinates.coordinate_y
  if x.coordinate.between?(3, 5)
    y.coordinate.between?(3, 5) ? (return color1) : (return color2)
  else
    y.coordinate.between?(3, 5) ? (return color2) : (return color1)
  end
end
fill_cell(cell) click to toggle source

Will fill the given container with cell content.

# File lib/sudoku/main_window.rb, line 92
def fill_cell(cell)
  background cell_background(cell)
  strokewidth get_stroke_width(cell)
  border get_cell_border(cell)
  banner cell.as_string, align: 'center' if cell.fixed
  banner cell.as_string, align: 'center', weight: 'bold' unless cell.fixed
end
get_cell_border(cell) click to toggle source

Will determine border of the cell.

# File lib/sudoku/main_window.rb, line 101
def get_cell_border(cell)
  return black if @selected.nil? || @selected[1] != cell
  red
end
get_stroke_width(cell) click to toggle source
# File lib/sudoku/main_window.rb, line 106
def get_stroke_width(cell)
  return 1 if @selected.nil? || @selected[1] != cell
  2
end
handle_cell_click(cell, flow, button) click to toggle source

Will handle click on the cell

# File lib/sudoku/main_window.rb, line 70
def handle_cell_click(cell, flow, button)
  info "Detected click on the cell #{cell}"
  cell.increment if button == 1
  cell.decrement if button == 3
  reset_selected_flow(flow, cell)
  flow.clear { fill_cell(cell) }
  handle_end_game
end
handle_end_game() click to toggle source

Will check, if the grid is solved. If so, will show alert.

# File lib/sudoku/main_window.rb, line 124
def handle_end_game
  alert 'Grid successfully solved!' if @grid.solved?
end
handle_key_press(key) click to toggle source

If there is some selected cell and the number is pressed, the number will be used as input to the cell.

# File lib/sudoku/main_window.rb, line 51
def handle_key_press(key)
  info "Pressed key #{key}"
  return if @selected.nil? || !('0'..'9').include?(key)
  flow, cell = @selected
  cell.value = key.to_i
  flow.clear { fill_cell(cell) }
  info "Value of cell #{cell.coordinates} set to #{cell.value}"
  handle_end_game
end
index() click to toggle source

This will create new sudoku and will render main page

# File lib/sudoku/main_window.rb, line 19
def index
  info 'Rendering GUI'
  init_model
  flow width: 1.0, height: 0.8 do
    render_grid
  end
  flow width: 1.0, height: 0.2 do
    render_help
  end
  info 'GUI rendered'
end
init_model() click to toggle source

Will initialize the model

# File lib/sudoku/main_window.rb, line 12
def init_model
  info 'Initializing model'
  @grid = Sudoku::GridFactory.new.create_random
  info "Generated grid\n#{@grid}"
end
render_cell(cell) click to toggle source

Will render particular cell.

# File lib/sudoku/main_window.rb, line 62
def render_cell(cell)
  cell_flow = flow width: 0.1111  do
    fill_cell(cell)
  end
  cell_flow.click { |button| handle_cell_click(cell, cell_flow, button) }
end
render_grid() click to toggle source

This will render the grid for sudoku.

# File lib/sudoku/main_window.rb, line 40
def render_grid
  info 'Rendering grid'
  flow width: 700, heiht: 700 do
    @grid.cells.values.sort.each { |x| render_cell(x) }
  end
  keypress { |key| handle_key_press(key) }
  info 'Grid rendered.'
end
render_help() click to toggle source

Will render basic help text

# File lib/sudoku/main_window.rb, line 32
def render_help
  caption 'Help'
  para ' - Left click on the cell will select and increment the cell.'
  para ' - Right click on the cell will select and decrement the cell.'
  para ' - When cell is selected, you can use number keys to fill it.'
end
reset_selected_flow(new_flow, new_cell) click to toggle source

Will reset selected flow and will set the selected to the new values.

  • new_flow new selected flow

  • new_cell new selected cell

# File lib/sudoku/main_window.rb, line 83
def reset_selected_flow(new_flow, new_cell)
  return @selected if new_cell.fixed
  return @selected = [new_flow, new_cell] if @selected.nil?
  old_flow, old_cell = @selected
  @selected = [new_flow, new_cell]
  old_flow.clear { fill_cell(old_cell) }
end