Class: Xudoku::Cli::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/xudoku/cli/game.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#errorsObject

Returns the value of attribute errors.



10
11
12
# File 'lib/xudoku/cli/game.rb', line 10

def errors
  @errors
end

#generated_puzzleObject (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

#puzzleObject (readonly)

Returns the value of attribute puzzle.



9
10
11
# File 'lib/xudoku/cli/game.rb', line 9

def puzzle
  @puzzle
end

#scratchObject

Returns the value of attribute scratch.



10
11
12
# File 'lib/xudoku/cli/game.rb', line 10

def scratch
  @scratch
end

#solutionObject (readonly)

Returns the value of attribute solution.



9
10
11
# File 'lib/xudoku/cli/game.rb', line 9

def solution
  @solution
end

#solvedObject

Returns the value of attribute solved.



10
11
12
# File 'lib/xudoku/cli/game.rb', line 10

def solved
  @solved
end

#stackObject

Returns the value of attribute stack.



10
11
12
# File 'lib/xudoku/cli/game.rb', line 10

def stack
  @stack
end

#xObject

Returns the value of attribute x.



10
11
12
# File 'lib/xudoku/cli/game.rb', line 10

def x
  @x
end

#yObject

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_gameObject



130
131
132
133
134
# File 'lib/xudoku/cli/game.rb', line 130

def analyze_game
  yield
  calculate_errors
  check_solved
end

#calculate_errorsObject



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_solvedObject



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

Returns:

  • (Boolean)


82
83
84
# File 'lib/xudoku/cli/game.rb', line 82

def clue?(x, y)
  @puzzle[x][y] != "_"
end

#deleteObject



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

#downObject



38
39
40
# File 'lib/xudoku/cli/game.rb', line 38

def down
  @x = (@x + 1) % 9
end

#each_cellObject



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

Returns:

  • (Boolean)


74
75
76
# File 'lib/xudoku/cli/game.rb', line 74

def error?(x, y)
  @errors.key?([x, y])
end

#errors?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/xudoku/cli/game.rb', line 78

def errors?
  @errors.any?
end

#getObject



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

#leftObject



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

#restartObject



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

#rightObject



46
47
48
# File 'lib/xudoku/cli/game.rb', line 46

def right
  @y = (@y + 1) % 9
end

#same_col?(_, y) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/xudoku/cli/game.rb', line 66

def same_col?(_, y)
  y == @y
end

#same_grid?(x, y) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


62
63
64
# File 'lib/xudoku/cli/game.rb', line 62

def same_row?(x, _)
  x == @x
end

#selected?(x, y) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


50
51
52
# File 'lib/xudoku/cli/game.rb', line 50

def solved?
  !!@solved
end

#undoObject



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

#upObject



34
35
36
# File 'lib/xudoku/cli/game.rb', line 34

def up
  @x = (@x - 1) % 9
end