Challenge: Roll That Die! (Code Golf)

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 24685 Views - Last Post: 09 March 2012 - 03:40 PM

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Challenge: Roll That Die! (Code Golf)

Post icon  Posted 29 February 2012 - 11:42 PM

Challenge: Roll That Die!

Challenge:
So given a die, the Pitch, Roll and Yaw, output which dice face is on top and also produce the dice layout with correct orientations of all the faces.

  • Implement the follow Shared Function on your Die
    Public Shared Function PRY(dieToUse As Die,
                     pitches As Integer,
                     rolls As Integer,
                     yaws As Integer) As Die
     ' ... '
    
    End Function
    
    


  • Implement the following ReadOnly Property on your Die.
    Which reveals the value of the top-most face
    Public Read Only Propery Value() As Integer
    
    


  • Override the ToString method to output the die in the follow format.

      B
     LTR
      F
      P
    
    

    B - Back
    L - Left
    T - Top
    R - Right
    F - Front
    P - Posterior

    An Example output of the canonical die starting arrangement.
        +---+
        |O  |
        |   |
        |  O|
    +---+---+---+
    |O O|   |O  |
    |   | O | O |
    |O O|   |  O|
    +---+---+---+
        |O O|
        | O |
        |O O|
        +---+
        |O O|
        |O O|
        |O O|
        +---+
    
    

    Output the dice is to be in the above style.

  • Any die produced or used mustn't be changed or mutated.


Notes:

-Pitch is Pitching Forwards
+Pitch is Pitching Backwards
-Roll is Rolling to Left
+Roll is Rolling to Right
-Yaw is Anti-Clockwise
+Yaw is Clockwise

For Example ( Pitch=-1)
    +---+
    |   |
    | O |
    |   |
+---+---+---+
|O O|O O|  O|
|   | O | O |
|O O|O O|O  |
+---+---+---+
    |O O|
    |O O|
    |O O|
    +---+
    |O  |
    |   |
    |  O|
    +---+


Notice thee orientation change of the left and right faces.

Scoring
Instead of the normal code scoring rules of the lowest number of non-whitespace characters.
This challenge will be judges by the number of lines in your Die and it supporting codes (excluding the framework of course).

Entrants
  • Winner will be the entry with the lowest score be the Start of next Month ( April 2012).
  • SourceCode must be in spoilered code tags.
    Spoiler

  • If you like you can include a Zip-File of the project / executable.
  • vb.net solutions (are the preferred programming language)


Now go have some fun thinking and coding you entry.

This post has been edited by AdamSpeight2008: 08 March 2012 - 06:42 AM


Is This A Good Question/Topic? 2
  • +

Replies To: Challenge: Roll That Die! (Code Golf)

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: Challenge: Roll That Die! (Code Golf)

Posted 04 March 2012 - 08:01 PM

I'll kick of the entries with my non-optimal 187 lines.

Note: Write to use .net 3.5 Client Profile
Spoiler


An example of it in use.
Module Module1
  Private rnd As New Random
  Public Function GetRND() As Integer
    Return rnd.Next(Integer.MinValue, Integer.MaxValue)
  End Function
    Sub Main()
    Dim d = Die.CreateNew()
    Console.WriteLine(d)
    Console.WriteLine()
    Dim R = Die.PRY(d, GetRND, GetRND, GetRND)
    Console.WriteLine(R)
    Console.WriteLine("Value:= {0}", R.Value)
    Console.Read()
    End Sub

End Module


This post has been edited by AdamSpeight2008: 04 March 2012 - 09:25 PM

Was This Post Helpful? 0
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: Challenge: Roll That Die! (Code Golf)

Posted 07 March 2012 - 08:49 AM

Here is my entry:

Spoiler


It could have less characters. All my symbols could be one character long and I could do away with data types, etc. But I think I'll leave it at this.

Also, why is the PRY function shared? You are passing in a single instance of Die to act on. It would make more sense to make it an instance method.
Was This Post Helpful? 1
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: Challenge: Roll That Die! (Code Golf)

Posted 07 March 2012 - 03:39 PM

Quote

Also, why is the PRY function shared?

My Challenge, my rules.

A really compact 44 Liner.
Spoiler

This post has been edited by AdamSpeight2008: 07 March 2012 - 08:12 PM

Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: Challenge: Roll That Die! (Code Golf)

Posted 07 March 2012 - 07:51 PM

Are we going by number of lines now? This one has 36 (30 if you take out the blank lines). I'm pretty pleased with the line count because there are only 24 possible orientations for the dice. Not the nicest code I've ever written, but that wasn't the point of the challenge.

I'm also sure you could use some of your VB magic and reduce my line count even further. My method lst() can probably go, but I couldn't work out how to create a list and call a method on it in one line.

Spoiler

Was This Post Helpful? 1
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: Challenge: Roll That Die! (Code Golf)

Posted 07 March 2012 - 08:11 PM

cfoley: Didn't you read the scoring rules in the first post?

First sub 30 Lines. (My head hurts, cos it has been a pain to debug.)
Spoiler

This post has been edited by AdamSpeight2008: 07 March 2012 - 08:45 PM

Was This Post Helpful? 0
  • +
  • -

#7 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: Challenge: Roll That Die! (Code Golf)

Posted 08 March 2012 - 04:18 AM

Yes, I read them but you are posting line count instead of character count with your code. And in particular:

Me.B = B : Me.L = L : Me.T = T : Me.R = R : Me.F = F : Me.P = P


This is more characters than simply having 6 lines, which led me to believe you were driving for lines rather than characters.

My question about the shared method was a serious one. I would consider it an antipattern as far as OO goes, but I know your style is more functional than mine (probably because I use Java so much). I was wondering if there were some functional delights I was missing out on, or if there was something else about the language that makes it a good idea.
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: Challenge: Roll That Die! (Code Golf)

Posted 08 March 2012 - 07:51 AM

Quote

My question about the shared method was a serious one.


I'm not a big fan of having publicly accessible constructors, for your objects.
I prefer having a public shared creator function.
Shared Public Function CreateNew(arg_0 ... arg_N) As T
' Arg checking
' If an arg is a error then throw some exception type.
End Function


As throwing exceptions in a constructor is a big no-no for me.

Yeah, it could easily be an instance method.
I'm off the idea;-
Does the method manipulate some of internal state.
No then make it shared and pass in an instance.
Think how instance methods does Enumerable have?
I think it makes the instance of an Object have a smaller memory footprint.

Quote

I was wondering if there were some functional delights I was missing out on, or if there was something else about the language that makes it a good idea.

You not side effecting objects, since you can only deal with the parameter arguments passed in.
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9570
  • View blog
  • Posts: 36,242
  • Joined: 12-June 08

Re: Challenge: Roll That Die! (Code Golf)

Posted 08 March 2012 - 01:16 PM

Bah.. I got it down to 37 lines of code I am mostly not annoyed by looking at nor too embarassed by.

Spoiler

This post has been edited by AdamSpeight2008: 09 March 2012 - 07:49 AM
Reason for edit:: name change on tostring (Adam: Added Overrides Keyword)

Was This Post Helpful? 1
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: Challenge: Roll That Die! (Code Golf)

Posted 09 March 2012 - 07:26 AM

Managed to squeeze out another 5 lines of code.
Spoiler

Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: Challenge: Roll That Die! (Code Golf)

Posted 09 March 2012 - 07:36 AM

modi123_1: Just a minor error (but I'll forgive you), of not overriding the .ToString method.
Dim foo As New Die
Console.WriteLine(foo.ToString) ' <-- This returns the type name, which is why we need to override it.
Console.WriteLine(" ")
foo = Die.PRY(foo, -1, 0, 0)
Console.WriteLine(foo.ToSt1ring) ' <-- This returns the die as a string.
Console.WriteLine(" ")



I do like idea of a function taking in 70 parameters.

This post has been edited by AdamSpeight2008: 09 March 2012 - 07:46 AM

Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9570
  • View blog
  • Posts: 36,242
  • Joined: 12-June 08

Re: Challenge: Roll That Die! (Code Golf)

Posted 09 March 2012 - 07:46 AM

Ah yeah.. Totally the same thing.

There.. fixed it.. sheeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeesh.
Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: Challenge: Roll That Die! (Code Golf)

Posted 09 March 2012 - 08:05 AM

modi123_1: On a closer inspection it's a Failure. It modifies the Die being passed in.
  Sub Main()
    Dim foo As New Die
    Console.WriteLine(foo.ToString)
    Console.WriteLine(" ")
    Dim foo2 = Die.PRY(foo, -1, 0, 0)
    Console.WriteLine(foo2.ToString)
    Console.WriteLine(" ")
    Console.WriteLine(foo.ToString)
    Console.WriteLine(" ")

  End Sub


This post has been edited by AdamSpeight2008: 09 March 2012 - 08:05 AM

Was This Post Helpful? 0
  • +
  • -

#14 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9570
  • View blog
  • Posts: 36,242
  • Joined: 12-June 08

Re: Challenge: Roll That Die! (Code Golf)

Posted 09 March 2012 - 08:06 AM

You mean live 5? Wasn't that the point of that shared function?
Was This Post Helpful? 0
  • +
  • -

#15 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,498
  • Joined: 29-May 08

Re: Challenge: Roll That Die! (Code Golf)

Posted 09 March 2012 - 08:20 AM

Quote

4. Any die produced or used mustn't be changed or mutated.

So this means it has to be immutable. (Just like the type String)

The Die being pass in is the initial orientation of die.
The output should is what orientation that die will be in after the various changes in orientation.

This post has been edited by AdamSpeight2008: 09 March 2012 - 08:21 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2