-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path\
68 lines (60 loc) · 1.74 KB
/
\
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
62
63
64
65
66
67
68
require "./Exception"
require "./Sudoku"
class Player
def initialize(sudoku, player_name)
@sudoku = sudoku
@player_name = player_name
end
def take_user_input
@sudoku.show
if [email protected]_filled?
begin
puts "Enter the Row position: "
row = validate_row_input(gets.chomp.to_i)
puts "Enter the Column position: "
column = validate_column_input(gets.chomp.to_i)
puts "Enter the number you want to put at above position: "
number = validate_number_input(gets.chomp.to_i)
if @sudoku.check_is_modifiable(row, column)
puts "Already their is number"
take_user_input
end
if @sudoku.validate_row(row-1, number) || @sudoku.validate_column(column-1, number) || @sudoku.validate_grid(row-1, column-1, number)
puts "Invalid number at this position.."
take_user_input
else
@sudoku.insert_number(row - 1, column - 1, number)
take_user_input
end
rescue InvalidRowError => e
puts e.message
retry
rescue InvalidColumnError => e
puts e.message
retry
rescue InvalidNumberError => e
puts e.message
retry
end
end
puts "Congrats #{@player_name} you won the game"
end
def validate_row_input(row)
if row.nil? || !row.between?(1,9)
raise InvalidRowError, "Invalid Row.."
end
return row
end
def validate_column_input(column)
if column.nil? || !column.between?(1,9)
raise InvalidColumnError, "Invalid Column.."
end
return column
end
def validate_number_input(number)
if number.nil? || !number.between?(1,9)
raise InvalidNumberError, "Invalid number.."
end
return number
end
end