Ruby code broken

I had written some Ruby code and ran it but this error message popped up:
undefined local variable or method `game_code’ for main:Object
I thought I properly defined the variable but I don’t know what went wrong.
Any help would be much appreciated! :slightly_smiling_face:
Here is the code:

class String
  # Text Colorization
  def colorize(color_code)
    "\e[#{color_code}m#{self}\e[0m"
  end

  def red
    colorize(31)
  end

  def green
    colorize(32)
  end

  def yellow
    colorize(33)
  end

  def blue
    colorize(34)
  end

  def pink
    colorize(35)
  end

  def light_blue
    colorize(36)
  end
end
# Game Startup Confirmation
puts "Do you want to load the game? If so, type 'Yes' into the terminal."
answer = gets.chomp
if answer == 'Yes' or 'yes' or 'Yes.' or 'yes.'
 game_code
else answer == 'No' or 'no' or 'No.' or 'no.'
 puts "\nStopping Game...".red
 sleep 1.5
 puts "\nGame Stopped.".red
 abort
end

game_code =
class Dice

  def initialize(sides)
    @sides = sides
  end

  def sides
    return @sides
  end

  def roll
    return Random.rand(@sides)+1
  end

end

class Character

  def initialize(name, abilities)
    @name = name
    @hp = 0
    @initiative = 0
    @passive_perception = 0
    @strength = 0
    @strength_modifier = 0
    self.hp = abilities[:hp]
    self.passive_perception = abilities[:passive_perception]
    self.initiative = abilities[:initiative]
    self.strength = abilities[:strength]
  end

  def name
    return @name
  end

  def hp=(n)
    @hp = n
  end

  def hp
    return @hp
  end

  def passive_perception=(n)
    @passive_perception = n
  end

  def passive_perception
    return @passive_perception
  end

  def strength=(n)
    @strength = n
    @strength_modifier = ABILITY_SCALE[n]
  end

  def strength
    return @strength
  end

  def strength_modifier
    return @strength_modifier
  end

  def initiative=(n)
    @initiative = n
  end

  def initiative
    return @initiative
  end

  def attack(target, d20, d6)
    if(d20.roll > 13)
      target.hp = target.hp - (d6.roll + @strength_modifier)
    end
  end

end

class Adventurer < Character

end

class Monster < Character

end

# Set constants

ABILITY_SCALE = [nil, -5, -4, -4, -3, -3, -2, -2, -1, -1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4]

# Instantiate dice

d6 = Dice.new(6)
d8 = Dice.new(8)
d20 = Dice.new(20)

# Set attributes of Rogar

rogar_abilities = {
  class: 'Druid',
  hp: 8 + ABILITY_SCALE[d6.roll + d6.roll + d6.roll],
  initiative: d20.roll,
  strength: d6.roll + d6.roll + d6.roll,
  passive_perception: d20.roll + ABILITY_SCALE[d6.roll + d6.roll + d6.roll],
}

goblin_abilities = {
  class: 'Goblin',
  hp: 7,
  initiative: d20.roll,
  strength: 8,
  passive_perception: d20.roll + 6
}

# Instantiate characters

rogar = Adventurer.new('Rogar', rogar_abilities)
goblin = Monster.new('Goblin', goblin_abilities)
characters = [rogar, goblin]
# Game startup sequence
print "\nLoading Game"
sleep 0.3
print "."
sleep 0.3
print "."
sleep 0.3
print "."
sleep 4
print "\nStarting Game"
sleep 0.3
print "."
sleep 0.3
print "."
sleep 0.3
print "."
sleep 3
puts "\nYour character has been set up:"
sleep 1.5
puts "Rogar's health: #{rogar.hp}"
sleep 1.5
puts "Rogar's initiative: #{rogar.initiative}"
sleep 1.5
puts "Rogar's strength: #{rogar.strength}"
sleep 1.5
puts "Rogar's strength modifier: #{rogar.strength_modifier}"
sleep 2.5
# Play the game
puts "\nOh no! There's a Goblin!"
sleep 0.3
battling = true
attacker = goblin
target = rogar
stealth = d20.roll + ABILITY_SCALE[d6.roll + d6.roll + d6.roll]
if rogar.passive_perception < stealth
  # TBD
elsif rogar.initiative > goblin.initiative
  attacker = rogar
  target = goblin
end
sleep 3
while battling do
  puts "#{attacker.name} attacks..."
  attacker.attack(target, d20, d6)
sleep 0.5
  puts "#{target.name}'s hp after attack: #{target.hp}"
sleep 0.5
  if (target.hp > 0)
    puts "#{target.name} survived the attack.\n".green
  else
    puts "#{target.name} was killed.\n\n".red
    sleep 0.5
    puts "G".red
    sleep 0.5
    puts "a".red
    sleep 0.5
    puts "m".red
    sleep 0.5
    puts "e".red
    sleep 0.5
    puts " ".red
    sleep 0.5
    puts "O".red
    sleep 0.5
    puts "v".red
    sleep 0.5
    puts "e".red
    sleep 0.5
    puts "r".red
    sleep 0.5
    puts ".".red
    battling = false
  end
  t = target
  target = attacker
  attacker = t
end

Two open brackets is what jumps out.

1 Like