Thanks a lot now it's working pretty nicely
http://pastie.textmate.org/private/0qfnvswccjfqhbe73ql7q
There are still a few things that I don't like though.
For example here
def reset_board(len = 3)
board = []
free = []
(0...(len)).each { board << [nil] * len }
# setting all the free cells
for i in (0...(len))
for j in (0...(len))
free << [i, j]
end
end
[board, free]
end
I generate the empty board and the free places, can I generate free
with
only one block?
Same thing in the check procedure, I'm first trying to call chkline
for every line,
creating the array of truth values and then calling truth.any?
It's a little be boring, I would like to do
and [ check x | x <- gen_lines board]
(as I would do in Haskell more or less)
def check(board)
chk = lambda { |vals| vals.any? and vals.uniq.length == 1 }
truth = []
chk_line = lambda { |b| b.each { |line| truth << (chk.call line) }}
chk_line.call gen_lines board
chk_line.call gen_lines board.transpose
# truth is automatically updated in the two calls
truth.any?
end
def gen_lines(board)
diag = []
to_check = []
(0...(board.length)).each { |i| diag << board[i][i]; to_check <<
board[i] }
to_check << diag
to_check
end