Loading ...
Sorry, an error occurred while loading the content.

Re: [XP] online bowling game solutions?

Expand Messages
  • June Kim
    ... [snip] I am not very sure if you might like this or not, but I am posting this as an different approach (with a different language or paradigm). Following
    Message 1 of 41 , Apr 25 6:03 AM
    View Source
    • 0 Attachment
      On 4/6/06, David Carlton <carlton@...> wrote:
      > I finally got around to working through the bowling game myself, and
      > now I'm very curious about what solutions are out there.
      [snip]

      I am not very sure if you might like this or not, but I am posting
      this as an different approach (with a different language or paradigm).

      Following is written in J (http://jsoftware.com and
      http://c2.com/cgi/wiki?JayLanguage), a descendant of APL:

      of=: @
      sum=: +/
      for=: ^:
      match=: -:
      applied=: &>/
      head=: {.
      andbonus=: conjunction def '(m+n)&{. ; m&}.'

      open=: 2 andbonus 0
      spare=: 2 andbonus 1
      strike=: 1 andbonus 2

      isStr=: 10 match head
      isSpr=: 10 match sum of (2&head)

      summed=: (sum@[ , ])applied@ (@])
      attach=: [,head@]
      rest=: }.@]
      accumed=: ([`(attach;rest)`) (`:6)
      frame=: open`spare@.isSpr`strike@.isStr
      doframe=: frame summed accumed applied

      limit=: (<&)(@#)
      init=: ''&;
      score=: >@head
      frames=: doframe for (10 limit of score) for (_:`init)
      gscore=: sum of score of frames

      empty_case=: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      open_case=: 9 0 8 1 7 2 6 3 5 4 4 5 3 6 2 7 8 1 0 9
      spare_case=: 9 1 8 2 7 3 6 4 5 5 4 6 3 7 2 8 1 9 8 2 1
      strike_case=: 10 10 10 10 10 10 10 10 10 10 10 10
      alt_case=: 9 1 10 8 2 10 7 3 10 6 4 10 5 5 10 4 6
      none_and_spare_case=: 0 10 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
      strike_none_spare_case=: 10 0 10 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0

      assert 0-:gscore empty_case
      assert 90-:gscore open_case
      assert 145-:gscore spare_case
      assert 300-:gscore strike_case
      assert 200-:gscore alt_case
      assert 30-:gscore none_and_spare_case
      assert 34-:gscore strike_none_spare_case


      The code is programmed using combinators(which combines verbs). There
      are verbs, adverbs, conjunctions, nouns and pronouns in the code.

      I could make this much shorter and conciser(I actually do have about
      five lines of solution, which could be automatically contracted to one
      line), but I tried hard to keep this readable(though I could have not
      succeeded very well) and to keep this J-friendly; and there were some
      tradeoffs as you might see. Actually I had done this in TDD but the
      TDDed scenario is on a different machine that I have no easy access at
      the moment, unfortunately. You might easily analyze the code if you
      play in the J interactive shell with the parts of code.

      For, maybe more J-ic approach, here is Henry Rich's:

      NB. Index of each ball that starts a frame
      framex =: 10 {. 0 {~^:a:~ _1 _1 ,~ i.@# > :@:+ 10&~:
      NB. Score for each ball, assuming that ball starts a frame
      scoreifframe =: 3 +^:(9<])`+/@|.\ ,&0
      NB. Pick the balls that actually start frames & add em up
      gamescore =: [: +/ framex { scoreifframe

      June
    • June Kim
      ... Oh, sorry. I sometimes forget that you have been programming longer than I have lived so far. (I m in the mid-30s) ... Yes, and there are some more
      Message 41 of 41 , Apr 25 7:39 AM
      View Source
      • 0 Attachment
        On 4/25/06, Ron Jeffries <ronjeffries@...> wrote:
        > On Tuesday, April 25, 2006, at 10:02:07 AM, June Kim wrote:
        >
        > > On 4/25/06, Ron Jeffries <ronjeffries@...> wrote:
        > >> On Tuesday, April 25, 2006, at 9:03:30 AM, June Kim wrote:
        > >>
        > >> > Following is written in J (http://jsoftware.com and
        > >> > http://c2.com/cgi/wiki?JayLanguage), a descendant of APL:
        > >>
        > >> Interesting, and one of the few languages where I can't at least get
        > >> the drift of what's happening. Especially Henry Rich's version!
        > >>
        > >> Fascinating ... wonder whether I dare go to that site ... ;->
        >
        > > I quickly built an examplary page in the c2 wiki for the feared. ;->
        >
        > > http://c2.com/cgi/wiki?SelfNumbersInJayLanguage
        >
        > > I hope you find as much gems in J(and other Ken Iverson's works) as I did.
        >
        > I used to use APL a bit and like it.

        Oh, sorry. I sometimes forget that you have been programming longer
        than I have lived so far. (I'm in the mid-30s)

        > J looks like it's typable.

        Yes, and there are some more enhancements. Especially I like the idea
        of tacit definition.

        > I guess I'll have to try it out. Thanks for the new page!

        My pleasure.

        >
        > Ron Jeffries
        > www.XProgramming.com
        > Learn the principle, abide by the principle, and dissolve the principle.
        > -- Bruce Lee
        >
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.