Uninitialized Constant Piece

Hi, this is my first post so apologies if I do anything wrong. I’m having trouble with inheritance and the ‘require’ keyword:

Issue:
I am making a command line chess program in Ruby, and I have a structure as such: (parent -> inherits from parent) Chess -> Piece -> Pawn, Knight, Rook, Bishop, King, Queen. Each class is in their own separate .rb file, all in the ./lib/ directory of my main chess directory.

If I require_relative chess in piece.rb, I get a Name Error: Uninitialized Constant: Piece whenever I try and run anything. Here are the files in question:

chess.rb
require_relative ‘piece’
require_relative ‘board’
require_relative ‘pawn’
require_relative ‘rook’
require_relative ‘knight’
require_relative ‘bishop’
require_relative ‘queen’
require_relative ‘king’

class Chess

  attr_accessor :board

  def initialize
  end

  def new_game
@board = Board.new
8.times do |i| # Add pawns to the board.
  @board.board[1][i] = Pawn.new(1,i,"w")
  @board.board[6][i] = Pawn.new(6,i,"b")
end

# Create white pieces
@board.board[0][0] = Rook.new(0,0,"w")
@board.board[0][1] = Knight.new(0,1,"w")
@board.board[0][2] = Bishop.new(0,2,"w")
@board.board[0][3] = Queen.new(0,3,"w")
@board.board[0][4] = King.new(0,4,"w")
@board.board[0][5] = Bishop.new(0,5,"w")
@board.board[0][6] = Knight.new(0,6,"w")
@board.board[0][7] = Rook.new(0,7,"w")

# Create black pieces
@board.board[7][0] = Rook.new(7,0,"b")
@board.board[7][1] = Knight.new(7,1,"b")
@board.board[7][2] = Bishop.new(7,2,"b")
@board.board[7][3] = Queen.new(7,3,"b")
@board.board[7][4] = King.new(7,4,"b")
@board.board[7][5] = Bishop.new(7,5,"b")
@board.board[7][6] = Knight.new(7,6,"b")
@board.board[7][7] = Rook.new(7,7,"b")
  end

  def get_valid_moves(moves) # Incomplete method
moves = moves.select{|move|}
  end
end

piece.rb
require_relative ‘chess’
require_relative ‘pawn’

class Piece < Chess
  attr_accessor :x,:y,:icon
  def initialize(x,y,colour)
    @x = x
    @y = y
    @colour = colour # Colour will be "w" or "b" 
    @icon
  end

  def valid_space?(move)
    if move[0]> 7 || move[1] > 7 || move[0] < 0 || move[1] < 0
      return false
    end
    true
  end
end

pawn.rb (as this is where my console is saying the errors are)
require_relative ‘piece’
require_relative ‘chess’

class Pawn < Piece
  def initialize(x,y,colour)
    super
    @colour == "b" ? (@icon = "♙") : (@icon = "♟")
    @move_count = 0
  end

  def list_all_moves
    moves = []
    moves.push([@x+2,@y]) if move_count == 0
    moves.push([@x+1,@y])
    moves = moves.select{|move| valid_space?(move)}
    # More stuff that will be in chess.rb
  end
  
end

What I’ve tried

  • Absolute paths to files
  • Requiring and unrequiring everything
  • Changing order of requires

Any help with this would be great, I’m completely stumped - it works fine until I require ‘chess’ in ‘piece.rb’ or I try to get Piece to inherit from Chess. (or Both).

Traceback (most recent call last):
	6: from lib/chess.rb:1:in `<main>'
	5: from lib/chess.rb:1:in `require_relative'
	4: from /home/nat/Documents/odin/projects/chess/lib/piece.rb:1:in `<top (required)>'
	3: from /home/nat/Documents/odin/projects/chess/lib/piece.rb:1:in `require_relative'
	2: from /home/nat/Documents/odin/projects/chess/lib/chess.rb:3:in `<top (required)>'
	1: from /home/nat/Documents/odin/projects/chess/lib/chess.rb:3:in `require_relative'
/home/nat/Documents/odin/projects/chess/lib/pawn.rb:4:in `<top (required)>': uninitialized constant Piece (NameError)

Hello natsydenham,

I would suggest moving:

require_relative 'pawn'

At the top of the chess.rb .

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs