I managed to create a fully functional tic-tac-toe game (although the rendering is very simplistic, and without knowing the rows are numbered 0-2, one would likely misplace their mark).
Anyhow, in creating the get_move method for the Computer Player class, I decided to implement an each_positions method in the Board class, as follows:
class ComputerPlayer
def get_move
possible_moves = []
board.each_position do |pos|
possible_moves << pos if board.empty?(pos)
end
possible_moves.each do |move|
return move if winning_move?(move)
end
possible_moves.sample
end
end
class Board
def each_position(&prc)
prc ||= Proc.new {|obj| obj}
grid.each_with_index do |row, row_idx|
row.each_index do |col_idx|
prc.([row_idx, col_idx])
end
end
end
end
I was hoping to clean up my get_move code a tiny bit. I intended to have the array of possible_moves = board.each_position.select {|pos| board.empty?(pos)} but this caused a TypeError of no implicit conversion of Symbol to Integer. What am I missing here? Why does the code I wrote above work but attempting to pass .each_position into .select cause an error?