-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku.rb
61 lines (53 loc) · 1.15 KB
/
Sudoku.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Sudoku
def initialize(array, hash)
@array = array
@hash = hash
end
def show
@array.map{|number| puts number.join(' ') }
end
def check_is_modifiable?(row, column)
if @hash.key?("#{row}#{column}")
return true
else
return false
end
end
def is_filled?
return true if @array.flatten.count{|cnt| cnt.between?(1,9)}.equal?(81)
return false
end
def validate_row(row, number)
if @array[row].include?(number) ? true : false
end
end
def validate_column(column, number)
if @array.transpose[column].include?(number) ? true : false
end
end
def validate_grid(row, column, number)
row_start = (row / 3) * 3
col_start = (column / 3) * 3
pos = 0
9.times do
return true if @array[row_start + pos % 3][col_start + pos / 3].equal?(number)
pos += 1
end
return false
end
def insert_number(row, column, number)
@array[row][column] = number
return true
end
def generate_flag
row = 0
9.times do
col = 0
9.times do
@hash[row][col] = 1 if @array[row][col] > 0
col += 1
end
row += 1
end
end
end