Odin项目井字棋开发:类对象跨类使用技术咨询
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
Boardinstance as@boardinGamelets you access it across all methods in theGameclass. - Encapsulate logic: Keep board-specific actions (like displaying or updating cells) in the
Boardclass, and letGamehandle the flow of the game (turns, win checks, player input). - Fix the array initialization bug: Using a block in
Array.newensures each row is a separate array, so your board behaves as expected when updating cells.
内容的提问来源于stack exchange,提问作者msmith1114

