Efficient Looping to Draw Shapes
Right now, we're working with Groovy, a variation on Java, so quite similar, if not just like, Java.So please bear with me if some syntax looks unfamiliar, it's easily convertible to Java syntax!
Anyhow, here's the shape I'm trying to draw,
*******
#*----*
##*---*
#-#*--*
#--#*-*
#---#**
#----#*
#######
For the first and last row, these can be expressed at the begining and end of the program, like so
7.times{print '*') and likewise for #.
I'm trying to output one character at a time, so that's when the for looping comes into play. The involvment of two characters is what's throwing me off. I managed to write the following for another shape, which projects the same idea of incrementing/decrementing spacing...
//Answer to Q2.9
n = 3
println '*'
for (row in 1..7)
{
if (row < 5)
{
print '*'
(row - 1).times{
print ' '
}
println '*'
}
else
{
print '*'
(row - n).times{
print ' '
}
n = n + 2
println '*'
}
}
println '*'
Any idea how I can go on about with this?
Many thanks!