Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

LIEK MY DIE?

Name: Anonymous 2012-01-10 21:42


#!/usr/bin/ruby
module Game
        class Die
                class << self
                        def roll num, val, drop = 0
                                sto = Array.new num
                                0.upto(num - 1){ |a| sto[a] = rand(1 .. val) }
                                sum = 0
                                unless drop > 0
                                        sto.each { |b| sum += b }
                                else
                                        sto.sort!
                                        0.upto(num - 1 - drop) { |b| sum += b }
                                end
                                sum
                        end
                end
        end
end

Name: Anonymous 2012-01-10 21:48

Wait....

replace
0.upto(num - 1 - drop) { |b| sum += b }
with
0.upto(num - 1 - drop) { |b| sum += sto[b] }

Name: Anonymous 2012-01-10 22:10

PIG DISGUSTING!

Name: Anonymous 2012-01-10 22:16

>>3

Singleton objects are pig disgusting?

Name: Anonymous 2012-01-10 22:28

This is retarded. Why would you use OO for this shit. I can do better in two minutes.
def die(sides=6):
    from random import randrange
    if (sides == 1):
        return 1
    elif (sides <= 0):
        raise ValueError('Invalid number of sides: ' + str(sides))
    else:
        return (randrange(sides-1)+1)

def roll(sides=6, dice=1):
    if (dice <= 0):
        raise ValueError('Invalid number of dice: ' + str(dice))
    elif (dice == 1):
        return die(sides)
    else:
        result = []
        for n in range(dice): result.append(die(sides))
        return result

if (__name__ == "__main__"):
    from sys import argv
    try: print roll(sides=int(argv[1]))
    except IndexError: print roll()
    except: print "Invalid argument: " + str(argv[1])

Name: Anonymous 2012-01-10 22:32

>>4
Your code is shit.

Name: Anonymous 2012-01-10 22:35

>>5
ENTERPRISE QUALITY!

Name: Anonymous 2012-01-10 22:41

>>7
Needs more '''Documentation''' for that. Also, a fancy GUI for retards.

Name: Anonymous 2012-01-10 22:57

>>5

I took about the same amount of time as you, except where you added exceptions, I wrapped the method in a singleton object inside a namespace.

That said, why you made two functions is beyond me.

Name: Anonymous 2012-01-11 1:03

>>9
Maybe to reduce the overhead when only one die is rolled.

Name: Anonymous 2012-01-11 1:16

why is it formatted so bad?

Name: Anonymous 2012-01-11 1:38

>>11
FIOC

Name: Anonymous 2012-01-11 4:11

>>12
This is not even FIOC. It is smashed together.

Name: Anonymous 2012-01-11 8:06

end

Name: Anonymous 2012-01-11 8:21

Lots of pig disgusting untyped code here.


using System;

public class Die
{
  public uint sides
  {
    get; protected set;
  }

  public Die(uint nSides = 6)
  {
    this.sides = nSides;
  }

  public int Roll()
  {
    Random r = new Random();
    return r.Next((int)sides+1);
  }
}

Name: Anonymous 2012-01-11 8:23

Not using K&R in 2012? what is going on.

Name: Anonymous 2012-01-11 8:36

>>15
Enterprise quality C#

Name: Anonymous 2012-01-11 8:45

>>17
No, this is enterprise quality:


using System;

/// <summary>
///   Represents a die.
/// </summary>
public class Die
{
  /// <summary>
  ///   Number of sides of the die.
  /// </summary>
  public uint sides
  {
    get; protected set;
  }

  /// <summary>
  ///   Constructs a new Die object.
  /// </summary>
  /// <param name="nSides">Number of sides.</param>
  public Die(uint nSides = 6)
  {
    this.sides = nSides;
  }

  /// <summary>
  ///   Rolls the die.
  /// </summary>
  /// <returns>The result of rolling the dice.</returns>
  public int Roll()
  {
    Random r = new Random();
    return r.Next((int)sides+1);
  }
}

Name: Anonymous 2012-01-11 8:52

>>18
Why no witty ///<remarks>
uint sides, rolls and return int, sure makes sense.
Static typing fun fun fun.

Name: Anonymous 2012-01-11 9:13

>>19
Most people would declare the variable that receives the Die.Roll() value as an int, so this solution is good. But uint is required in constructor to avoid retards using negative numbers, and you don't have to check if the number is less than 0. So my solution is elegant, precise, and fast.

Name: Anonymous 2012-01-11 9:21

>>20
No your solution wan forced on you by the Random.Next()
that cannot into uint.
Wtf will your program do if someone puts a value greater than int max in the constructor? and then sides gets casted to an int?
Walk the dinosaur?
Roll negative derp?
Err, sorry I kinda got off the point here.

Name: Anonymous 2012-01-11 9:36

>>21
Then Random.Next() will throw an exception for me. As you know, negative numbers are represented using two's complement, so a number greater than int max would be negative using a signed representation.
Random.Next ENTERPRISE QUALITY documentation says that it throws:

ArgumentOutOfRangeException     maxValue is less than zero.

Name: Anonymous 2012-01-11 9:46

>>22
Exception
Even more enterprise then initially thought.
You win this one.
I wonder why do they even support uint if you can't use it anywhere.

Name: Anonymous 2012-01-11 9:49

>                        end
>                end
>        end
>end

gets me every tiem

Name: Anonymous 2012-01-11 10:10

>>23
The .NET CLR recommends that public APIs use only signed types, because of .NET language interoperability. Retarded in my opinion, every language should implement both unsigned/signed types.

Name: Anonymous 2012-01-11 10:35

>>18
[color=green]
>using XML to doc code
>allmywhy
[/color]

Name: Anonymous 2012-01-11 10:37

>>26

Let's try this again.

[color="green"]
>using XML to doc code
>allmywhy
[/color]

Name: Anonymous 2012-01-11 11:06

>>26,27
lol u dumb nigga.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List