I am coding a Mastermind game in Ruby and looking to add further functionality to the game whereby the player can choose between being a 'codemaker' or 'codebreaker'. To do this I have created two classes called Codebreaker & Codemaker that inherit from the Players class.
For reasons unknown when I create an instance of Codebreaker, then call the random_code method which it should have inherited from the Players Class I am getting 'undefined method shuffle' error message. Yet this method has not had a problem before with the more simplistic version.
class Players
attr_accessor :peg_colors, :select_role, :select_player
def initialize
@peg_colors = :peg_colors
@select_role = :select_role
@select_player = :select_player
end
def random_code
mix_colors = @peg_colors.shuffle
mix_colors[0...4]
end
def player
puts "Would you like to be the Codebreaker or Codemaker? Type your answser:"
@select_role= gets.chomp
@select_role = @select_role.downcase
end
def select_player(choice)
if choice == "codebreaker"
@select_player = Codebreaker
else
@select_player = Codemaker
end
end
def pegs
@peg_colors = ['Blue', 'Pink', 'Yellow', 'White', 'Purple', 'Green']
end
end
class Codemaker < Players
def random_code
get_code
end
def get_code ## thids needs re-coding
puts "\n"
puts "#{@peg_colors}"
puts "\n"
code = []
while code.length < 5
code.push(gets.chomp)
end
end
end
class Codebreaker < Players
def initialize
super
end
end
require_relative 'board.rb'
require_relative 'players.rb'
require_relative 'game.rb'
play = Players.new
pegs = play.pegs
codebreaker_or_codemaker = play.player
select_player = play.select_player(codebreaker_or_codemaker)
player = select_player.new
code = player.random_code
board = Board.new(code, pegs)
game = Game.new(board)
game.play
The "shuffle" is method in Array class which should be available for all Array instances. I see it is working fine on the repl.com link you have shared. If you are still facing this issue, please share the error message or verify the object by which shuffle method is called. I hope this helps. Thank you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.