Class: Sudoku::MainWindow
- Inherits:
-
Shoes
- Object
- Shoes
- Sudoku::MainWindow
- Includes:
- Logging
- Defined in:
- lib/sudoku/main_window.rb
Overview
This class represents main window for the Sudoku GUI.
Constant Summary
Constants included from Logging
Instance Method Summary collapse
-
#cell_background(cell) ⇒ Object
Will determine and return background for cell.
-
#fill_cell(cell) ⇒ Object
Will fill the given container with cell content.
-
#get_cell_border(cell) ⇒ Object
Will determine border of the cell.
- #get_stroke_width(cell) ⇒ Object
-
#handle_cell_click(cell, flow, button) ⇒ Object
Will handle click on the cell.
-
#handle_end_game ⇒ Object
Will check, if the grid is solved.
-
#handle_key_press(key) ⇒ Object
If there is some selected cell and the number is pressed, the number will be used as input to the cell.
-
#index ⇒ Object
This will create new sudoku and will render main page.
-
#init_model ⇒ Object
Will initialize the model.
-
#render_cell(cell) ⇒ Object
Will render particular cell.
-
#render_grid ⇒ Object
This will render the grid for sudoku.
-
#render_help ⇒ Object
Will render basic help text.
-
#reset_selected_flow(new_flow, new_cell) ⇒ Object
Will reset selected flow and will set the selected to the new values.
Methods included from Logging
#disable_log, #error, #fatal, #info
Instance Method Details
#cell_background(cell) ⇒ Object
Will determine and return background for cell
112 113 114 115 116 117 118 119 120 121 |
# 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) ⇒ Object
Will fill the given container with cell content.
92 93 94 95 96 97 98 |
# 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) cell.as_string, align: 'center' if cell.fixed cell.as_string, align: 'center', weight: 'bold' unless cell.fixed end |
#get_cell_border(cell) ⇒ Object
Will determine border of the cell.
101 102 103 104 |
# 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) ⇒ Object
106 107 108 109 |
# 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) ⇒ Object
Will handle click on the cell
70 71 72 73 74 75 76 77 |
# File 'lib/sudoku/main_window.rb', line 70 def handle_cell_click(cell, flow, ) info "Detected click on the cell #{cell}" cell.increment if == 1 cell.decrement if == 3 reset_selected_flow(flow, cell) flow.clear { fill_cell(cell) } handle_end_game end |
#handle_end_game ⇒ Object
Will check, if the grid is solved. If so, will show alert.
124 125 126 |
# File 'lib/sudoku/main_window.rb', line 124 def handle_end_game alert 'Grid successfully solved!' if @grid.solved? end |
#handle_key_press(key) ⇒ Object
If there is some selected cell and the number is pressed, the number will be used as input to the cell.
51 52 53 54 55 56 57 58 59 |
# 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 ⇒ Object
This will create new sudoku and will render main page
19 20 21 22 23 24 25 26 27 28 29 |
# 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 ⇒ Object
Will initialize the model
12 13 14 15 16 |
# 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) ⇒ Object
Will render particular cell.
62 63 64 65 66 67 |
# 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 { || handle_cell_click(cell, cell_flow, ) } end |
#render_grid ⇒ Object
This will render the grid for sudoku.
40 41 42 43 44 45 46 47 |
# 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 ⇒ Object
Will render basic help text
32 33 34 35 36 37 |
# 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) ⇒ Object
Will reset selected flow and will set the selected to the new values.
-
new_flow
new selected flow -
new_cell
new selected cell
83 84 85 86 87 88 89 |
# 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 |