Class: RubyTerminalGames::Sudoku::Puzzle

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_terminal_games/sudoku/puzzle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePuzzle

Returns a new instance of Puzzle.



5
6
7
8
# File 'lib/ruby_terminal_games/sudoku/puzzle.rb', line 5

def initialize
  @locked_positions = []
  @state = build_puzzle
end

Instance Attribute Details

#locked_positionsObject (readonly)

Returns the value of attribute locked_positions.



4
5
6
# File 'lib/ruby_terminal_games/sudoku/puzzle.rb', line 4

def locked_positions
  @locked_positions
end

#stateObject (readonly)

Returns the value of attribute state.



4
5
6
# File 'lib/ruby_terminal_games/sudoku/puzzle.rb', line 4

def state
  @state
end

Instance Method Details

#add_input(input, user_input_index) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/ruby_terminal_games/sudoku/puzzle.rb', line 10

def add_input(input, user_input_index)
  return if locked_position?(user_input_index)
  input = nil if input.zero?
  previous = state[user_input_index]
  @state[user_input_index] = input
  return true if valid?
  @state[user_input_index] = previous
  false
end

#locked_position?(user_input_index) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/ruby_terminal_games/sudoku/puzzle.rb', line 20

def locked_position?(user_input_index)
  locked_positions.include?(user_input_index)
end