-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.rb
225 lines (187 loc) · 5.41 KB
/
board.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# frozen_string_literal: true
require 'colorize'
require 'colorized_string'
require './tile'
# contains the board formatting, game rules checks, etc
class Board
attr_accessor :board
def initialize
@board = Array.new(9) { Array.new(9, 0) }
end
# create board
def create_board
convert_board_array_to_tiles
fill_diagonal
fill_remaining
remove_some_numbers
end
def convert_board_array_to_tiles
# map through elements of board & replace each element w/ tile instance
@board.each_with_index do |line, i|
line.each_with_index do |_char, j|
@board[i][j] = Tile.new(@board[i][j], true, true)
end
end
end
def fill_diagonal
[0, 3, 6].each do |i|
fill_box(i, i)
end
end
def fill_box(row, col)
num = 0
3.times do |i|
3.times do |j|
loop do
num = [*1..9].sample
break if unused_in_box(row, col, num)
end
update_board_value(row + i, col + j, num, false)
end
end
end
def update_board_value(row, col, num, playable)
@board[row][col].value = num
@board[row][col].playable = playable
end
def fill_remaining(row = 0, col = 3)
# Check for the end of the box
return true if row == 8 && col == 9
# if it is the end of the current row, move to the next row
if col == 9
row += 1
col = 0
end
# Skip filled spots
return fill_remaining(row, col + 1) if @board[row][col].value != 0
# fill cells
return true if fill_cells(row, col)
false
end
def fill_cells(row, col)
(1..9).each do |num|
next unless check_if_safe_move(row, col, num)
@board[row][col].value = num
@board[row][col].playable = false
return true if fill_remaining(row, col + 1)
@board[row][col].value = 0
end
false
end
# remove numbers for playing purpose
def remove_some_numbers
count = 50
while count != 0
i = rand(-1..7)
j = rand(-1..7)
next unless @board[i][j].value != 0
count -= 1
@board[i][j].value = 0
@board[i][j].playable = true
end
@board
end
def check_if_safe_move(row, col, num)
(unused_in_row(row, num) and unused_in_col(col, num) and unused_in_box(row - (row % 3), col - (col % 3), num))
end
def unused_in_row(row, num)
9.times do |col|
return false if @board[row][col].value == num
end
true
end
def unused_in_col(col, num)
9.times do |row|
return false if @board[row][col].value == num
end
true
end
def unused_in_box(row_start, col_start, num)
3.times do |i|
3.times do |j|
return false if @board[row_start + i][col_start + j].value == num
end
end
true
end
def render_board(board)
print "\n 0 1 2 3 4 5 6 7 8 \n"
board.each_with_index do |line, i|
print "#{i} "
line.each_with_index do |_char, j|
[2, 5].include?(j) ? (print "#{board[i][j].colorize} ") : (print board[i][j].colorize)
end
print "\n"
print "\n" if [2, 5].include? i
end
end
def play_round
render_board(@board)
update_board(ask_user_input)
end
# GET USER INPUT
def ask_user_input
print "\nEnter you move (e.g. 001 => [0,0], 1)\n"
user_input = gets.chomp
if valid_user_input?(user_input)
user_input
else
print 'Please enter a valid move..'
ask_user_input
end
end
def format_user_input(user_input)
int_input = user_input.chars.map(&:to_i)
[[int_input[0], int_input[1]], int_input[2]]
end
# Validate move
def valid_user_input?(user_input)
unless user_input.empty?
formatted_input = format_user_input(user_input)
return true if valid_input_length_and_chars?(formatted_input) && playable_move?(formatted_input)
end
false
end
# => [[0, 1], 1]
def valid_input_length_and_chars?(formatted_input)
input_length_two = (formatted_input.length == 2)
input_is_array = formatted_input.is_a?(Array)
value_is_int = formatted_input[1].is_a?(Integer)
input_length_two && input_is_array && pos_as_array_and_length_two(formatted_input) &&
pos_values_in_range(formatted_input) && value_is_int && value_in_range(formatted_input)
end
def pos_as_array_and_length_two(formatted_input)
formatted_input[0].is_a?(Array) && formatted_input[0].length == 2
end
def pos_values_in_range(formatted_input)
formatted_input[0][0] >= 0 && formatted_input[0][0] <= 8 &&
formatted_input[0][1] >= 0 && formatted_input[0][1] <= 8
end
def value_in_range(formatted_input)
formatted_input[1] >= 1 && formatted_input[1] <= 9
end
# => [[0,1], 4]
def playable_move?(player_move)
position = player_move[0]
@board[position[0]][position[1]].playable
end
def solved?
@board.flatten.all? { |a| !a.value.zero? && a.safe_move }
end
def update_board(user_input)
formatted_input = format_user_input(user_input)
player_input_value = formatted_input[1]
board_position = convert_input_to_board_position(formatted_input)
board_position.safe_move = if check_if_safe_move(user_input[0].to_i, user_input[1].to_i, user_input[-1].to_i)
true
else
false
end
board_position.value = player_input_value
end
# => [[0, 1], 4]
def convert_input_to_board_position(input)
position = input[0]
@board[position[0]][position[1]]
end
end