user3658248
user3658248

Reputation: 1

Drawing in Ruby with asterisks

I am trying to draw the letter X in ruby using *. I am not able to get a diagonal line to intersect with another diagonal line to create the letter X. Please see my code below, appreciate your help!

# Draw X
# Draw diagonal1
for y in 1..13
  for x in 1..(13-y)
    print " "
  end
  for x in 1..7
    print "*"
  end
  print "\n"
end

# Draw diagonal2
for y in 1..13
    for x in 1..(y-13)
            print " "
    end
    for x in 1..7
        print "*"
    end
    print "\n"
end 

Upvotes: 0

Views: 508

Answers (1)

Uri Agassi
Uri Agassi

Reputation: 37409

After you go down a line (print "\n") you can't go back up.

You should try to plan a little bit better, and print both diagonals at the same time:

3.downto(0).each { |i| puts ('*' * 7 + ' ' * i*2 + '*' * 7).center(20) }
5.downto(3).each { |i| puts ('*' * (i*2+1)).center(20) }
3.upto(5).each { |i| puts ('*' * (i*2+1)).center(20) }
0.upto(3).each { |i| puts ('*' * 7 + ' ' * i*2 + '*' * 7).center(20) }

Upvotes: 1

Related Questions