#!/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:
Anonymous2012-01-10 21:48
Wait....
replace 0.upto(num - 1 - drop) { |b| sum += b }
with 0.upto(num - 1 - drop) { |b| sum += sto[b] }
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
/// <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);
}
}
>>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.
>>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.
>>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.
>>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.