I have been commissioned to make a version of the Monopoly game called Qytetet. Currently I have 4 Ruby files, 2 of them are classes and the other 2 are modules (one of them contains another class). I explain a little,
- Surprise Class : It has 3 instance attributes; @text, @type and @valor. Contains the initialize method and the to_s method.
- Qytetet class : It has the instance attribute @mazo. Also a class method that initializes @mazo with the corresponding elements: a description (@text), a value (@valor) and a type (@type).
- Modulo TipoSorpresa : Contains the types of surprise.
- Module-Class PruebaQytetet : Contains class attribute @@ game, 3 class methods to perform some operations and the main class method, which seeks to print the results of the 3 operations (and obviously create a new one) mallet and others).
The problem is that when running on NetBeans, I get the following error:
NoMethodError: undefined method '< <' for nil: NilClass
The error occurs on line 15 of qytetet.rb, just by assigning the first element to the @mazo array. I leave you with the corresponding codes.
sorpresa.rb
class Sorpresa
def initialize(nuevo_texto, nuevo_tipo, nuevo_valor)
@texto = nuevo_texto
@tipo = nuevo_tipo
@valor = nuevo_valor
end
attr_reader :texto, :tipo, :valor
def to_s
puts "Texto: #{@texto} \n Valor: #{@valor} \n Tipo: #{@tipo}"
end
end
qytetet.rb
require_relative "tipo_sorpresa"
class Qytetet
def initialize
@mazo = Array.new
end
attr_reader :mazo
def self.inicializar_cartas_sorpresa
@mazo<< Sorpresa.new("Te han pillado saqueando las arcas públicas del estado, vas a la cárcel.", 9, TipoSorpresa::IRACASILLA)
@mazo<< Sorpresa.new("No sabemos si estabas cerca de la casilla inicial o no, pero ahora lo vas a estar.", 1, TipoSorpresa::IRACASILLA)
@mazo<< Sorpresa.new("¿Eres supersticioso?", 13, TipoSorpresa::IRACASILLA)
@mazo<< Sorpresa.new("Resulta que un funcionario de la cárcel es amigo tuyo. De casualidades está hecha la vida. Sales de la cárcel.", 0, TipoSorpresa::SALIRCARCEL)
@mazo<< Sorpresa.new("Tus rivales te odian tanto que les obligamos a que te den lo que lleven suelto en la cartera.", 200, TipoSorpresa::PORJUGADOR)
@mazo<< Sorpresa.new("Parece que te está gustando el juego, por eso tendrás que recompensar a tus rivales.", -300, TipoSorpresa::PORJUGADOR)
@mazo<< Sorpresa.new("¡Enhorabuena! Te ha tocado la lotería, pero la agencia tributaria se va a quedar casi todo.", 250, TipoSorpresa::PAGARCOBRAR)
@mazo<< Sorpresa.new("Vamos a jugar a algo, tú me das algo de dinero y yo no te doy nada. ¿Qué te parece?", -250, TipoSorpresa::PAGARCOBRAR)
@mazo<< Sorpresa.new("Vaya, esta sorpresa parece que te va a quitar algo de dinero por los hoteles y casas de tus rivales, siempre y cuando tú estés de acuerdo... o no.", -150, TipoSorpresa::PORCASAHOTEL)
@mazo<< Sorpresa.new("Estás de suerte. Tus propiedades acaban de evadir impuestos y te dan algo más de dinero extra.", 200, TipoSorpresa::PORCASAHOTEL)
end
end
tipo_sorpresa.rb
module TipoSorpresa
PAGARCOBRAR = :Pagar_cobrar
IRACASILLA = :Ir_casilla
PORCASAHOTEL = :Por_casahotel
PORJUGADOR = :Por_jugador
SALIRCARCEL = :Salir_carcel
end
prueba_qytetet.rb
require_relative "sorpresa"
require_relative "qytetet"
module ModeloQytetet
class PruebaQytetet
@@juego = Array.new
def self.mayor_que_cero
mayor_cero = Array.new
for s in @@juego.mazo
if(mazo.valor > 0)
mayor_cero.mazo = s
end
end
return mayor_cero
end
def self.tipo_casilla
tipo_casilla = Array.new
for s in @@juego.mazo
if(mazo.tipo == :Ir_casilla)
tipo_casilla.mazo = s
end
end
return tipo_casilla
end
def self.tipo_sorpresa(sorpresa)
tipo_sorpresa = Array.new
for s in TipoSorpresa::constants
if(mazo.tipo == sorpresa)
tipo_sorpresa = s
end
end
return tipo_sorpresa
end
def self.main
Qytetet.inicializar_cartas_sorpresa
PruebaQytetet.mayor_que_cero
PruebaQytetet.tipo_casilla
PruebaQytetet.tipo_sorpresa
end
end
PruebaQytetet.main
end
I would appreciate a little help, I do not understand that error at all. I think it has to do with syntax @@ mallet < < [...], but that's what they've told me to do. Thanks in advance.