Results 1 to 19 of 19
  1. #1
    clam is offline Member
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Default 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!

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Are you looking for Groovy code or Java code?

  3. #3
    clam is offline Member
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Default

    The aforementioned code draws this,
    *
    **
    *.*
    *..*
    *...*
    *....*
    *...*
    *..*
    *.*
    **
    *

  4. #4
    clam is offline Member
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Default

    Having worked with Java, I don't mind either...I can covert then.

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by clam View Post
    Having worked with Java, I don't mind either...I can covert then. :)
    Not a good idea. It's always a bad idea to write code in one language and try to directly convert. What you should really be facing up to here is that you have a Groovy code problema and thus should ask in a Groovy forum.

  6. #6
    clam is offline Member
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Default

    Java and Groovy are nearly the same (Java is more punctuation-structured), so whatever error I'll have in Groovy will be the same in Java. We can pretend we're working with Java in this instance.

    There are not many Groovy forums, as developed as this one.

  7. #7
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    "Java and Groovy are nearly the same"

    Groovy syntax looks very different then java.

    It like saying that VB.Net syntax is nearly the same as C# syntax because they use the same underlying libraries.
    Last edited by mrmatt1111; 10-29-2009 at 08:17 PM.
    My Hobby Project: LegacyClone

  8. #8
    clam is offline Member
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Default

    Groovy website description:
    Groovy...
    .
    .
    .
    builds upon the strengths of Java but has additional power features inspired by languages like Python, Ruby and Smalltalk
    makes modern programming features available to Java developers with almost-zero learning curve
    .
    .
    .
    compiles straight to Java bytecode so you can use it anywhere you can use Java

    There is more. Feel free to visit the website. Effectively, Groovy is an inspiration from Java - in fact, if you try writing the for loop as written in Java (meaning with the three components within parantheses, i.e. variable declaration, condition and updating) in the groovy console, it will compile :)

    Now you guys are completely defeating the purpose of this forum and thread. All I'm asking is if you can be help me generate a compact program - whether you disagree with the fact that Java and Groovy are alike transcends the main idea of the thread. I honestly don't mind if you write it in java, this is not homework to be given in. It's for myself. I would just like to know how people generally approach these kinds of questions.

  9. #9
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Java Code:
    for y as integer = 0 to height
       for x as integer = 0 to width
          if y = 0 then
              print "*"
          elseif y = height -1 then
              print "#"
          else
               if x = 0 then
                  print "#"
               elseif x = width -1 then
                  print "*"
               else
                  if x = y then
                      print "*"
                  elseif x+1=y then
                      print "#"
                  else
                      print "-"
                  end if
               end if
          end if
        next
        println ""
    next
    My Hobby Project: LegacyClone

  10. #10
    clam is offline Member
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Default

    Thanks! But what language is your's in exactly? Pseudocode?

  11. #11
    clam is offline Member
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Default

    I'm not too sure your code works!

  12. #12
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Have you tried running it? It works for me. If you don't want to convert from psudocode to Java, just check each branch and see if the logic is valid. My guess is that's how they made the psudocode to begin with, since most people don't program in psudocode.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  13. #13
    clam is offline Member
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Default

    Ok, it does work, although partially! I'm tweeking it a bit...

  14. #14
    clam is offline Member
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Default

    your "end if" statements are unnecessary, aren't they?

  15. #15
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by clam View Post
    your "end if" statements are unnecessary, aren't they?
    I thought you said you were not concerned about language syntax?

  16. #16
    clam is offline Member
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

    Default

    I'm not concerned with language syntax - I find it unnecessary to insert the end if statements, since the if statements automatically end (/break) when the condition evaluates to true and the statement within is executed...

  17. #17
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Quote Originally Posted by clam View Post
    I'm not concerned with language syntax - I find it unnecessary to insert the end if statements, since the if statements automatically end (/break) when the condition evaluates to true and the statement within is executed...
    The end if statement marks the end of the if branch - it's similar to the close brace '}' in C style programing languages. It's common for BASIC-like syntaxes (e.g. Visual Basic).

    Quoted from If...Then...Else Statement (Visual Basic)
    Java Code:
    If condition [ Then ]
        [ statements ]
    [ ElseIf elseifcondition [ Then ]
        [ elseifstatements ] ]
    [ Else
        [ elsestatements ] ]
    End If

    Edit:
    I think their code IS Visual Basic... It's been a while but the syntax does look familiar.
    Last edited by CodesAway; 10-30-2009 at 10:59 PM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  18. #18
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Edit:
    I think their code IS Visual Basic... It's been a while but the syntax does look familiar.
    Good catch, it is VB syntax. I happened to be debugging a VB.Net web-app at the time so i converted a java version to VB real quick to make it look more psuedo-code like.
    Last edited by mrmatt1111; 10-31-2009 at 03:39 AM.
    My Hobby Project: LegacyClone

  19. #19
    clam is offline Member
    Join Date
    Oct 2009
    Posts
    11
    Rep Power
    0

Similar Threads

  1. erase shapes ???
    By h9h in forum Java 2D
    Replies: 1
    Last Post: 10-12-2009, 08:11 PM
  2. Need a simple and efficient solution.
    By Samgetsmoney in forum New To Java
    Replies: 9
    Last Post: 02-20-2009, 01:18 AM
  3. Efficient Perfect Number
    By Lite3864 in forum New To Java
    Replies: 4
    Last Post: 11-23-2008, 01:07 AM
  4. Colors and shapes.
    By Torgero in forum New To Java
    Replies: 14
    Last Post: 10-13-2008, 05:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •