0
\$\begingroup\$

I've recently started writing a tic-tac-toe game (following the Odin project) and am trying to write a class to hold the board game.

What I'm not sure about, is that the board class holds all the data related to the board in a variable called @board. Now after thinking about it I decided that the Board class shouldn't know about the game logic, and just hold the board itself, but now I'm not sure if it's redundant or not.

Main problem is that right now, when I try to add logic to the Game class, I get a function that looks like this:

class Game
  .
  def is_over?
    board = @board.board
    .
    .
  end
end

Which just feels wrong...
my current implementation for it is like so:

module TicTacToe
  class Board
    attr_reader :board
    def initialize(size)
      @board = create_empty_board(size)
      @size = size
    end

    def print_board
      @size.times { print "-----" }
      puts
      @board.each do |row|
        row.each do |c|
          print "| #{c} |"
        end
      puts
      @size.times { print "-----" }
      puts
      end
    end

    def make_move(move, sym)
      if board[x][y] == ' '
        board[x][y] = sym
      else
        raise ArgumentError, "Invalid, cell not empty"
      end
    end

  private
    def create_empty_board(size)
      board = Array.new
      size.times do
        row = Array.new
        size.times do
          row << ' '
        end
        board << row
      end
      board
    end
  end
end
\$\endgroup\$

closed as off-topic by 200_success Jul 1 '14 at 10:11

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. Such questions may be suitable for Stack Overflow or Programmers. After the question has been edited to contain working code, we will consider reopening it." – 200_success
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ Please post your Game class as well. \$\endgroup\$ – 200_success Jun 30 '14 at 15:01
  • \$\begingroup\$ @200_success I don't really have a 'Game' class code to post, it's still in the making and I'm trying to think about how to build it, and this point is exactly where I got stuck. \$\endgroup\$ – Nescio Jun 30 '14 at 15:25
  • \$\begingroup\$ x and y are undefined. \$\endgroup\$ – 200_success Jul 1 '14 at 10:11
2
\$\begingroup\$

Yes, your board.board has a code smell, but it is hard to say what to do without knowing Game.

Actually Board#board is an array. I would recommend to add methods to Board that allow you to use the instance of Board instead of Board#board.

Example: To get a row of yor board you can use:

class Board
  ...
  def [](row) #Access to one row
    @board[row]
  end
  def each_row #loop on all rows...
    @board.each{|row| yield row }
  end

end

class Game
  ...
  def is_over?
    @board.each_row{|row|
     row.each{|field| } ##your check
    }
    ...
  end
end
\$\endgroup\$
  • \$\begingroup\$ That's actually a really neat idea. I didn't think about that. Thanks :) \$\endgroup\$ – Nescio Jun 30 '14 at 16:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.