🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Tangent: Dice, Part 1.

Published April 01, 2009
Advertisement
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";	}}
Previous Entry Of Dice and Men
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement