Jump to content
  • Advertisement
  • entries
    359
  • comments
    237
  • views
    189653

Tangent: Dice, Part 1.

Sign in to follow this  
Telastyn

242 views

Got to do a little work on the dice example tonight. To me, it was pretty straightforward to construct the classes/methods to make the tiny DSL beginnings like I wanted. Not sure if that will translate well to other programmers. Found a few bugs doing it; they're more of an impediment at this point than syntax.

This doesn't quite work yet (the extension of int to take a RollOfDice doesn't compile right), but the syntax is there. For feedback, and those interested:


public class RollOfDice{
public delegate Roll()=>int;
public static Random rng = new Random;

protected int minimumValue;
public int MinimumValue{
get{
return(minimumValue);
}
}

protected int maximumValue;
public int MaximumValue{
get{
return(maximumValue);
}
}

public static Create( void -> int rollProcedure, int min, int max) => RollOfDice {
local RollOfDice rtn = new RollOfDice;

rtn.Roll = rollProcedure;
rtn.minimumValue = min;
rtn.maximumValue = max;

return(rtn);
}

public static CreateDie( int sides ) => RollOfDice{

return(
// bug #67,
// static scoping needs full path.

RollOfDice.Create(
()=>int{ return( (RollOfDice.rng.Next(sides))+1 ); },
1,
sides
)
);
}

}
// bug #65, custom initializer is fubar'd
//public RollOfDice d20 = RollOfDice.CreateDie(20);

public RollOfDice d20;
public RollOfDice d12;
public RollOfDice d10;
public RollOfDice d8;
public RollOfDice d6;
public RollOfDice d4;

public static create dice => void {
d20 = RollOfDice.CreateDie(20);
d12 = RollOfDice.CreateDie(12);
d10 = RollOfDice.CreateDie(10);
d8 = RollOfDice.CreateDie(8);
d6 = RollOfDice.CreateDie(6);
d4 = RollOfDice.CreateDie(4);
}

public static roll (RollOfDice die) => int{
return(die.Roll());
}


public class int{
public this (RollOfDice die) => RollOfDice{
local int min = die.MinimumValue * this;
local int max = die.MaximumValue * this;
local void -> int newDie = ()=>int{
local int rtn = 0;

foreach(int count in 0 to this){
rtn = rtn + roll die;
}
return(rtn);
}

return(RollOfDice.Create(newDie,min,max));
}
}


public static main()=>void{
create dice;

print roll 3d6 "\r\n";

foreach( int x in 0 to 10 ){
print roll d20 "\r\n";
}
}


Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!