Class: Xudoku::Cli::Game
- Inherits:
-
Object
- Object
- Xudoku::Cli::Game
- Defined in:
- lib/xudoku/cli/game.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#generated_puzzle ⇒ Object
readonly
Returns the value of attribute generated_puzzle.
-
#puzzle ⇒ Object
readonly
Returns the value of attribute puzzle.
-
#scratch ⇒ Object
Returns the value of attribute scratch.
-
#solution ⇒ Object
readonly
Returns the value of attribute solution.
-
#solved ⇒ Object
Returns the value of attribute solved.
-
#stack ⇒ Object
Returns the value of attribute stack.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #analyze_game ⇒ Object
- #calculate_errors ⇒ Object
- #check_solved ⇒ Object
- #clue?(x, y) ⇒ Boolean
- #delete ⇒ Object
- #down ⇒ Object
- #each_cell ⇒ Object
- #error?(x, y) ⇒ Boolean
- #errors? ⇒ Boolean
- #get ⇒ Object
- #grid_for(x, y) ⇒ Object
-
#initialize(generated = nil) ⇒ Game
constructor
A new instance of Game.
- #left ⇒ Object
- #position(x, y) ⇒ Object
- #restart ⇒ Object
- #right ⇒ Object
- #same_col?(_, y) ⇒ Boolean
- #same_grid?(x, y) ⇒ Boolean
- #same_row?(x, _) ⇒ Boolean
- #selected?(x, y) ⇒ Boolean
- #set(num) ⇒ Object
- #solved? ⇒ Boolean
- #undo ⇒ Object
- #up ⇒ Object
Constructor Details
#initialize(generated = nil) ⇒ Game
Returns a new instance of Game.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/xudoku/cli/game.rb', line 12 def initialize(generated = nil) @generated_puzzle = generated || Xudoku.generate @puzzle = @generated_puzzle.puzzle.to_a @solution = @generated_puzzle.solution.to_a # Temporary scratch pad @scratch = @puzzle.map(&:dup) # Current position @x = 0 @y = 0 @solved = false @stack = [] @errors = {} end |
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
10 11 12 |
# File 'lib/xudoku/cli/game.rb', line 10 def errors @errors end |
#generated_puzzle ⇒ Object (readonly)
Returns the value of attribute generated_puzzle.
9 10 11 |
# File 'lib/xudoku/cli/game.rb', line 9 def generated_puzzle @generated_puzzle end |
#puzzle ⇒ Object (readonly)
Returns the value of attribute puzzle.
9 10 11 |
# File 'lib/xudoku/cli/game.rb', line 9 def puzzle @puzzle end |
#scratch ⇒ Object
Returns the value of attribute scratch.
10 11 12 |
# File 'lib/xudoku/cli/game.rb', line 10 def scratch @scratch end |
#solution ⇒ Object (readonly)
Returns the value of attribute solution.
9 10 11 |
# File 'lib/xudoku/cli/game.rb', line 9 def solution @solution end |
#solved ⇒ Object
Returns the value of attribute solved.
10 11 12 |
# File 'lib/xudoku/cli/game.rb', line 10 def solved @solved end |
#stack ⇒ Object
Returns the value of attribute stack.
10 11 12 |
# File 'lib/xudoku/cli/game.rb', line 10 def stack @stack end |
#x ⇒ Object
Returns the value of attribute x.
10 11 12 |
# File 'lib/xudoku/cli/game.rb', line 10 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
10 11 12 |
# File 'lib/xudoku/cli/game.rb', line 10 def y @y end |
Instance Method Details
#analyze_game ⇒ Object
130 131 132 133 134 |
# File 'lib/xudoku/cli/game.rb', line 130 def analyze_game yield calculate_errors check_solved end |
#calculate_errors ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/xudoku/cli/game.rb', line 144 def calculate_errors errors.clear scratch.each_with_index do |row, i| row.each_with_index do |_col, j| next unless puzzle[i][j] == "_" && scratch[i][j].to_s.match?(/\A[1-9]\z/) && scratch[i][j].to_i != solution[i][j] errors[[i, j]] = true end end end |
#check_solved ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/xudoku/cli/game.rb', line 157 def check_solved return if errors.any? @solved = solution.each_with_index.all? do |row, i| row.each_with_index.all? { |col, j| col.to_i == scratch[i][j].to_i } end end |
#clue?(x, y) ⇒ Boolean
82 83 84 |
# File 'lib/xudoku/cli/game.rb', line 82 def clue?(x, y) @puzzle[x][y] != "_" end |
#delete ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/xudoku/cli/game.rb', line 99 def delete analyze_game do if puzzle[x][y] == "_" current = scratch[x][y] stack << [x, y, { from: current, to_value: nil }] scratch[x][y] = nil end end end |
#down ⇒ Object
38 39 40 |
# File 'lib/xudoku/cli/game.rb', line 38 def down @x = (@x + 1) % 9 end |
#each_cell ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/xudoku/cli/game.rb', line 136 def each_cell @scratch.each_with_index do |row, x| row.each_with_index do |col, y| yield x, y, col end end end |
#error?(x, y) ⇒ Boolean
74 75 76 |
# File 'lib/xudoku/cli/game.rb', line 74 def error?(x, y) @errors.key?([x, y]) end |
#errors? ⇒ Boolean
78 79 80 |
# File 'lib/xudoku/cli/game.rb', line 78 def errors? @errors.any? end |
#get ⇒ Object
126 127 128 |
# File 'lib/xudoku/cli/game.rb', line 126 def get scratch[@x][@y] end |
#grid_for(x, y) ⇒ Object
54 55 56 |
# File 'lib/xudoku/cli/game.rb', line 54 def grid_for(x, y) (x / 3) * 3 + (y / 3) end |
#left ⇒ Object
42 43 44 |
# File 'lib/xudoku/cli/game.rb', line 42 def left @y = (@y - 1) % 9 end |
#position(x, y) ⇒ Object
29 30 31 32 |
# File 'lib/xudoku/cli/game.rb', line 29 def position(x, y) @x = x @y = y end |
#restart ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/xudoku/cli/game.rb', line 86 def restart analyze_game do @stack.clear @errors.clear @solved = false @x = 0 @y = 0 @scratch = @puzzle.map(&:dup) end end |
#right ⇒ Object
46 47 48 |
# File 'lib/xudoku/cli/game.rb', line 46 def right @y = (@y + 1) % 9 end |
#same_col?(_, y) ⇒ Boolean
66 67 68 |
# File 'lib/xudoku/cli/game.rb', line 66 def same_col?(_, y) y == @y end |
#same_grid?(x, y) ⇒ Boolean
70 71 72 |
# File 'lib/xudoku/cli/game.rb', line 70 def same_grid?(x, y) grid_for(x, y) == grid_for(@x, @y) end |
#same_row?(x, _) ⇒ Boolean
62 63 64 |
# File 'lib/xudoku/cli/game.rb', line 62 def same_row?(x, _) x == @x end |
#selected?(x, y) ⇒ Boolean
58 59 60 |
# File 'lib/xudoku/cli/game.rb', line 58 def selected?(x, y) x == @x && y == @y end |
#set(num) ⇒ Object
120 121 122 123 124 |
# File 'lib/xudoku/cli/game.rb', line 120 def set(num) analyze_game do scratch[@x][@y] = num end end |
#solved? ⇒ Boolean
50 51 52 |
# File 'lib/xudoku/cli/game.rb', line 50 def solved? !!@solved end |
#undo ⇒ Object
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/xudoku/cli/game.rb', line 109 def undo analyze_game do return unless @stack.any? latest = stack.pop scratch[latest[0]][latest[1]] = latest[2][:from] @x = latest[0] @y = latest[1] end end |
#up ⇒ Object
34 35 36 |
# File 'lib/xudoku/cli/game.rb', line 34 def up @x = (@x - 1) % 9 end |