You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odin项目井字棋开发:类对象跨类使用技术咨询

Using Class Objects Across Classes in Your Tic-Tac-Toe Project

Hey there! Let's walk through exactly how to use your Board class instance within your Game class (or any other class) for your Tic-Tac-Toe build. Based on the code snippet you shared, here's a practical breakdown:

1. Instantiate the Board in Your Game Class

First, you'll want to create an instance of Board inside your Game class—this gives your game logic a concrete board to work with. You can do this in the Game's initialize method so it's ready when the game starts:

class Game
  def initialize
    # Create a new Board instance and store it as an instance variable
    @board = Board.new
    @current_player = 'X' # Track whose turn it is
  end
end

2. Access Board Properties and Methods

Once you have the @board instance in Game, you can call any of its methods (like display_board) or access its attributes (like @board.board) directly. For example, you could add a start method to Game that displays the board and handles turns:

class Game
  def initialize
    @board = Board.new
    @current_player = 'X'
  end

  def start
    puts "Let's play Tic-Tac-Toe!"
    display_current_board
    # Add turn loop logic here later
  end

  private

  def display_current_board
    # Call the Board class's display_board method
    @board.display_board
  end
end

3. Modify the Board State

To update the board (like placing an 'X' or 'O' in a cell), you can interact with the @board instance's board attribute. First, fix a small issue in your Board initializer—right now, Array.new(3, Array.new(3, 'X')) creates three references to the same array, so changing one row will affect all three. Instead, use a block to create independent rows:

class Board
  attr_accessor :board

  def initialize
    # Initialize with empty spaces instead of 'X' for a fresh game
    @board = Array.new(3) { Array.new(3, ' ') }
  end

  def display_board
    @board.each do |row|
      puts row.join(' | ')
      puts '---------'.freeze unless row == @board.last
    end
  end

  # Add a method to place a move on the board
  def place_move(row, col, player)
    @board[row][col] = player if @board[row][col] == ' '
  end
end

Then, in your Game class, you can add a method to handle player moves that uses this place_move method:

class Game
  # ... existing code ...

  def handle_move(row, col)
    if @board.place_move(row, col, @current_player)
      puts "Player #{@current_player} moved to #{row},#{col}!"
      display_current_board
      switch_player
    else
      puts "That spot is taken! Try again."
    end
  end

  private

  def switch_player
    @current_player = @current_player == 'X' ? 'O' : 'X'
  end
end

4. Using the Classes Together

To tie it all together, you can create a Game instance and start the game:

game = Game.new
game.start
# You'd add logic here to get user input for moves and check for wins

Key Takeaways

  • Instance variables are your friend: Storing the Board instance as @board in Game lets you access it across all methods in the Game class.
  • Encapsulate logic: Keep board-specific actions (like displaying or updating cells) in the Board class, and let Game handle the flow of the game (turns, win checks, player input).
  • Fix the array initialization bug: Using a block in Array.new ensures each row is a separate array, so your board behaves as expected when updating cells.

内容的提问来源于stack exchange,提问作者msmith1114

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:56:15