0

Currently I have code that looks like:

def sudoku_generator

  @initial_board = [0] * 81

  a = (1..9).sort_by{rand}
  b = (1..9).sort_by{rand}
  c = (1..9).sort_by{rand}

  @initial_board[0..2] = a[0..2]
  @initial_board[9..11] = a[3..5]
  @initial_board[18..20] = a[6..8]
  @initial_board[30..32] = b[0..2]
  @initial_board[39..41] = b[3..5]
  @initial_board[48..50] = b[6..8]

  @initial_board[60..62] = c[0..2]
  @initial_board[69..71] = c[3..5]
  @initial_board[78..80] = c[6..8]

  @initial_board.each_slice(9) do |make_better|
    puts make_better.join(' | ')
  end

end

print sudoku_generator

This returns:

3 | 7 | 9 | 0 | 0 | 0 | 0 | 0 | 0
1 | 5 | 6 | 0 | 0 | 0 | 0 | 0 | 0
2 | 8 | 4 | 0 | 0 | 0 | 0 | 0 | 0
0 | 0 | 0 | 1 | 5 | 8 | 0 | 0 | 0
0 | 0 | 0 | 6 | 7 | 2 | 0 | 0 | 0
0 | 0 | 0 | 3 | 4 | 9 | 0 | 0 | 0
0 | 0 | 0 | 0 | 0 | 0 | 5 | 1 | 3
0 | 0 | 0 | 0 | 0 | 0 | 4 | 8 | 7
0 | 0 | 0 | 0 | 0 | 0 | 6 | 2 | 9

I am trying to find a way to now replace the zeros within this array with underscores "_". Any feedback would be appreciated. Thanks.

5
  • It does not affect the code, but what is "seduku"? Commented Feb 11, 2015 at 22:34
  • Sudoku misspelled. Or perhaps alternatively spelled. Commented Feb 11, 2015 at 22:36
  • 1
    It does not affect the code, but what is "sedoku"? Commented Feb 11, 2015 at 22:37
  • 3
    Why not do @initial_board = Array.new(81, "_") from the beginning? Commented Feb 11, 2015 at 22:38
  • That Sudoku would probably appear in the paper on a Monday. Commented Feb 11, 2015 at 23:24

2 Answers 2

2

As @sawa suggested in the comments, I'd default the array to '_', but you can always just swap 0 for '_' as you print:

puts make_better.map { |x| x == 0 ? '_' : x }.join(' | ')
Sign up to request clarification or add additional context in comments.

3 Comments

You can do x.zero?.
May as well use map!
Thank you all for the quick responses. I learned something today. ;)
1

How about using classes? It'a an object oriented language, right? I guess the method #to_s could have been prettier, though.

class Grid
  SIZE = 3

  def initialize
    @array = Array.new(3) { |line| Array.new(3) { |column| generator.call(line, column) } }
  end

  def generator
    proc { '_' }
  end

  def to_a
    @array
  end
end

class RandomGrid < Grid
  def generator
    @random_numbers ||= (1..9).to_a.shuffle
    proc { @random_numbers.shift }
  end
end


class DiagonalBoard < Grid
  def generator
    proc { |line, column| (line == column ? RandomGrid : Grid).new }
  end

  def to_s
    @array.map do |grids|
      grids.map(&:to_a).transpose.map do |rows|
        rows.flatten.join('|')
      end.join("\n")
    end.join("\n")
  end
end

puts DiagonalBoard.new

Outputs

9|2|8|_|_|_|_|_|_
5|3|7|_|_|_|_|_|_
1|4|6|_|_|_|_|_|_
_|_|_|8|1|5|_|_|_
_|_|_|6|2|9|_|_|_
_|_|_|3|4|7|_|_|_
_|_|_|_|_|_|2|9|1
_|_|_|_|_|_|7|5|6
_|_|_|_|_|_|3|8|4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.