Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8672647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:15:23+00:00 2026-06-12T19:15:23+00:00

I have got a toughie for you. I have a program that fills a

  • 0

I have got a toughie for you.

I have a program that fills a 2d array with Tile objects of “w” type. Then the program selects a random point(center) in the array and attempts to turn all of the objects within a radius of 4 from the center. Somehow the program doesn’t do it and I think the issue is the math formula, but I cant find the mistake.

class Map
  def initialize(d1,d2)
    @data = Array.new(d1) { Array.new(d2)}
  end

  def [](x, y)
   @data[x][y]
  end

  def x(x)
    @data[x]
  end

  def y(y)
    @data[y]
  end

  def []=(x, y, value)
    @data[x][y] = value
  end
end

# TILES
class Tile
  # Types = ["l", "w", "r"]
  attr_accessor :type
  def initialize(type)
    @type = type
  end

  def set(string)
    @type = string
  end
  def to_s
     @type
  end
end

# REAL ACTION HERE
def generate 
  h = 20
  w = 20
  i = 0
  worldmap = Map.new(20, 20)
  while i < h do
    n = 0
    while n < w do
      worldmap.[] = (i, n, Tile.new("w"))
      n = n + 1 
    end
    i = i + 1
  end 

  gen(worldmap)
  look(worldmap)
end

def look(map)
  z = 0
  b = 0
  c = 0
  string = " "
  while b < 20 do
    while z < 20 do  
      string = string + map.[](b, z).to_s
      z = z + 1
    end 
    b = b + 1 
    puts string
    c = c + 1
  end
end

def gen(map)
  circle_amount = 1

  i = 0
  x = 0
  y = 0
  while i < circle_amount do 
    #select a center
   cy = (rand(1..20))
   cx = (rand(1..20))
   center = map.[](cx,cy)
   center.set("C")

   radius = 4
   puts cy.to_s + " " +  cx.to_s

   while y < 20
     while x < 20
       offsetY = y - cy 
       offsetX = x - cx 
       distance = offsetY**2 + offsetX**2
       if distance <= radius**2 then 
           tile = map.[](y, x)
           tile.set("l") # I DID IT
         end
       x = x + 1
     end
      y = y + 1
   end
   i = i + 1
  end
end

generate 

Here are the examples of output

wwwwwwwwwwwwlllllwww wwwwwwwwwwwwlllllwww wwwwwwwwwwwwlllllwww
wwwwwwwwwwwwlllllwww wwwwwwwwwwwwlllllwww wwwwwwwwwwwwlllllwww
wwwwwwwwwwwwlllllwww wwwwwwwwwwwwlllllwww wwwwwwwwwwwwlllllwww
wwwwwwwwwwwwlllllwww wwwwwwwwwwwwlllllwww wwwwwwwwwwwwlllllwww
wwwwwwwwwwwwlllllwww wwwwwwwwwwwwlllllwww wwwwwwwwwwwwlllllwww
wwwwwwwwwwwwlllllwww wwwwwwwwwwwwlllllwww wwwwwwwwwwwwlllllwww
wwwwwwwwwwwwlllllwww wwwwwwwwwwwwlllllwww

C:/Users/borya/Documents/NetBeansProjects/LearningPurporse/lib/main.rb:7:in []': >undefined method[]’ for nil:NilClass (NoMethodError)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:15:24+00:00Added an answer on June 12, 2026 at 7:15 pm
     > m = Map.new(5,5)
     => #<Map:0x007fa5d9838fc0 @data=[[nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil]]> 
    ruby-1.9.3-p125 :040 > m[1,1]="hi"
    (x=1, y=1) = hi
    @data[x][y]=hi
     => "hi" 
     > m
     => #<Map:0x007fa5d9838fc0 @data=[[nil, nil, nil, nil, nil], [nil, "hi", nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil]]> 
    

    I’d probably code this something closer to the below, although it’s a bit off-the cuff, and I preserved some of your original code even when it might not be how I would have done it.

    class Map
      attr_reader :data, :x, :y
    
      def initialize(x, y)
        @x = x
        @y = y
        @data = Array.new(x) { Array.new(y) }
      end
    
      def set(&block)
        (0..x-1).each do |i|
          (0..y-1).each do |j|
            result = yield i, j, @data[i][j]
            @data[i][j] = result if result
          end
        end
      end
    
      def [](x, y)
        @data[x][y]
      end
    
      def []=(x, y, value)
        @data[x][y] = value
      end
    
      def dump
        (0..x-1).each do |i|
          string = ""
          (0..y-1).each do |j|
            string += "#{self[i, j]} "
          end
          puts string
        end
      end
    end
    
    def generate 
      map = Map.new(20, 20)
      map.set { |x, y, at| Tile.new('w') }
      gen(map)
    end
    
    def gen(map)
      3.times do |i|
        cy = rand(0..map.x-1)
        cx = rand(0..map.y-1)
        puts "cx=#{cx}, cy=#{cy}"
        map[cx, cy].set('C')
        map.dump
    
        radius = 3.0
    
        map.set do |x, y, at|
          offsetX = x - cx
          offsetY = y - cy
          distance = Math.sqrt(offsetY**2 + offsetX**2)
          distance.abs <= radius ? '.' : false
        end
    
        puts ''
        map.dump
      end
    end
    

    Once you fix your math, you can end up with something like this:

    w w w w w w w w w w w w w w w w w . w w 
    w w w w w w w w w w w w w w w . . . . . 
    w w w w w w w w w w w w w w w . . . . . 
    w w w w w w w w w w w w w w . . . . . . 
    w w w w w . w w w w w w w w w . . . . . 
    w w w . . . . . w w w w w w w . . . . . 
    w w w . . . . . w w w w w w w w w . w w 
    w w . . . . . . . w w w w w w w w w w w 
    w w w . . . . . w w w w w w w w w w w w 
    w w w . . . . . w w w w w w w w w w w w 
    w w w w w . w w w w w w w w w w w w w w 
    w w w w w w w w w w w w w w w w w w w w 
    w w w w w w w w w w w w w w w w w w w w 
    w w w w w w w w w w w w w w w w w w w w 
    w w w w w w w w w w w w . w w w w w w w 
    w w w w w w w w w w . . . . . w w w w w 
    w w w w w w w w w w . . . . . w w w w w 
    w w w w w w w w w . . . . . . . w w w w 
    w w w w w w w w w w . . . . . w w w w w 
    w w w w w w w w w w . . . . . w w w w w 
    

    Caveat: this code is above your skill-level, you may wish to translate it back into while-loops with the extra verbosity until you’re further along.

    Boris, your original code was difficult to reason about, for a number of reasons, including the random indentation/formatting and simple syntax errors. It’s important to format your code consistently. Doing so enables both you, and others, to read it more easily.

    It’s important to become familiar with the paradigms of the language you’re using. In general, if Ruby code has while-loops, counter increments, etc. when they’re not really used for anything other than indexing, it’s a code smell. Canonical Ruby tends towards a more functional style using .each, blocks, and so on.

    You were correct that your math was wrong, although I suspect you weren’t aware just how wrong it was. Math like this is really easy to debug: do it by hand. Draw a grid. Take the program’s (cx, cy) and do the calculation by hand. Do some sanity checks to make sure what you think you’re doing is (a) what you’re actually doing, and (b) sensical.

    For example, your distances were in the hundreds sometimes. Clearly that makes no sense when your grid is (20, 20)… once you see that, the solution is obvious–you omitted a step.

    The same logic can be used for the rest of the math issues.

    Play computer… be the computer. Follow its steps, on paper. There is no faster path to understanding than internalizing what’s actually happening “underneath the screen”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got a text field, and then I type some text onto it.
I have got two routes ; category route resources.router.routes.category.type = Zend_Controller_Router_Route resources.router.routes.category.route = shopping/:idTwo/:id/*
i have got below rule that works fine with standart domain names like .net.com,.mobi
I have got a method that takes a long time to complete and want
Have got the following HAML code, combined with Markdown: %h2.slogan.align-center :markdown No more big
I have got a problem. My program is really big and java is throwing
I have got relative layout, in that i have placed two textviews, one image
I have got a page with data generated client-side that is formatted as a
I have got lots of div.articles and in that articles; i've got list of
I have got this css: #footerLinks { text-align: center; margin: auto; } #footerLinks a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.