Tic tac toe--creating instance methods

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?

21%20PM02%20PM

Nvm. I think I’m just sleep deprived. If I’m thinking clearly, the problem should be the prc ||= Proc.new {|obj| obj} statement, which when used in the method chain .each_position.select, passes in the marks at each respective position rather than the position itself. Will test in the morning. Also, there’s no reason for me to do each_position AND select, I may as well have just written a select method if that was my intention

Hi James,
Great to see you figured out a solution for your refactoring. Thank you for sharing. Please feel free to keep posting questions if anything comes up moving forward.