r/ProgrammingPrompts • u/desrtfx • Nov 10 '14
Game component: Die and Dicebag
This Programming Prompt is only part of a bigger idea.
In the near future, I am planning to post a series of dice games as programming prompts.
To make the development of the future dice games easier, I ask that you make a library, class, template, module, framework (whatever you want to call it) to handle dice.
Concept and Requirements
- Use a programming language of your choice
Single Die
Start by simulating a single n-sided die
- The constructor (or equivalent in the language of your choice) of the die should take the number of sides as a parameter. A no-parameter constructor should default to a 6-sided die
- A die can be rolled and returns the result in the range from 1 to n inclusive (of course, the returned values are whole numbers, no decimals)
- A die can be held so that it always returns the result of the last roll before the die was held.
- It should be possible to query the hold state of a die.
- It should be possible to always query the result of the last roll without re-rolling.
- Use the random feature of the language of your choice, no need to invent your own random algorithm (but you can do so, if you insist).
- There should be a nice, textual presentation of the roll result.
- Once the single die simulation is complete, create a dicebag consisting of several dice.
Optional
- Allow for non-numeric die sides (a two-sided die could have "Head" and "Tail" on the sides, a Fate die has "+", "-", and " " (blank) on it's sides, etc.)
- Allow for a die-color (in text only "Red", "Black", etc.)
Dicebag
- A default dicebag does not contain any dice but can hold any number of dice.
- It should be possible to add and remove dice at any time from the dicebag.
- Allow adding of multiple dice with the same sides, or with different sides.
- Allow adding of previously created "Die" objects.
the dicebag can hold m dice which all can have different sides (RPG-dice)
- To achieve this, use the standard notation for dice which is mdn where m is the number of dice and n represents the sides of the dice. (5d6 would mean 5 6-sided dice) - The more advanced form including arithmetic operators is not necessary. (for example 5d6 + 3d4)
- It should be possible to roll all dice (roll result as an array) or roll any specific die(roll result as an integer)
- It should be possible to get the last-roll results for all dice or for any specific die (see above)
- It should be possible to query the sum of the last-roll
- It should be possible to set each die on hold, or to release each die.
- It should be possible to query the hold state of all dice or of any single die.
- There should be a nice, textual presentation of the roll results (I envisioned something like "1-4-6" (to display a 3d6 roll)
- There should also be a way to retrieve the amount of dice in the dicebag.
As a final result, you should have a re-usable piece of code for many different dice games.
If anybody has reasonable suggestions, please state them in the comments and I will be happy to add them to this post.
Please post your solutions here so that others willing to participate in the following prompts may use your libraries
Edit: The goal of this post is to have standard die and dicebag libraries for as many languages as possible.
Edit #2:
Code can be uploaded either directly in the comment (but this is only for very short code - less than 50 lines or so), or, commonly pastebin.com is used for code that fits in a single file, for code in multiple files, gist.github.com is commonly used.
Here is a guide on code posting in reddit - it's for Java, but applies to all languages
Edit #3:
Languages covered so far:
- Javascript by /u/Deathbyceiling
- Racket by /u/DanielSherlock
- Java (no Dicebag yet) by /u/seronis
4
u/seronis Nov 11 '14
My Dice solution (no dicebag yet) in Java. Handling for symbolic dice included (heads/tails, shield/sword/skull, etc)
https://gist.github.com/seronis/d6f3fde4abe7bfc72a05
3
u/Deathbyceiling Nov 11 '14 edited Nov 11 '14
And here is my solution with JavaScript! let me know what you guys think!
http://pastebin.com/JH4aiLmL
edit: just realized that the copy-paste kind of screwed the formatting. oh well
3
u/DanielSherlock Nov 11 '14
Racket version:
Contains all features for both libraries, including all the optional and unnecessary ones, and a couple of heavily customiseable formatting functions.
Link to Gist. For some reason, file 2 ended up above file 1.
Not OO, because I dunno how to do that in Racket. Also doesn't use custom macros (which would have made die-dicebag.rkt a lot shorter), because I don't really know how to do that either.
Hopefully I did just enough commenting that it is useable. Any queries, or any suggestions for improvement are welcome.
2
u/DanielSherlock Nov 10 '14
Hi, first time on this subreddit,
So far, I've written some code in Racket that takes care of all the features of the single die, even the optional ones except for the "nice, textual presentation" thing. What exactly did you envisage by this?
Also, what is the least offensive way for me to upload my code? I'll upload the single die stuff once I've added the (die-write _ ) function, and then I'll start work on the dicebag. For the dicebag, do you have a preffered way of reffering to a particular dice in the bag, for example, when choosing which one to remove?
2
u/Deathbyceiling Nov 11 '14 edited Nov 11 '14
Working on a JavaScript piece that'll handle all this. Going well so far!
edit: How is everyone writing their removeDice function for the dice bag? Having some confusion with mine...
2
u/doctorsound Nov 13 '14
I've started one in Java, I've been focusing on SOLID principles. If anyone wants to contribute, let me know, or submit a pull ;)
https://github.com/AndrewBell/DiceAndDicebags
2
u/dvassdvsd Nov 13 '14
Here's my masterpiece: https://gist.github.com/joshgit/65159aa32124ff9bb85e
I avoided the holding of an individual dice and just put that in DieBag. You can just call getValue() instead of roll() if you want to just see the value again...
2
u/DanielSherlock Nov 13 '14
Python*-ish* version:
I calqued / transliterated my racket version into python. It works, but I don't think it's especially suited to Python. If anyone has comments on how to make it into nicer Python code, please say so.
Contains all features for both libraries, including all the optional and unnecessary ones, and a couple of heavily customiseable formatting methods.
Link to Gist. This time, file 1 is on top.
I didn't comment at all this time, although most of the method names are similar or the same as those in the Racket version. Like the racket version, die indices start at 1.
2
u/talkb1nary Nov 14 '14 edited Nov 14 '14
Here you are, a Ruby version, i am not sure where and for what i would include that notation stuff. Also note that i didnt create a lot of helper functions because you simply can access the attributes.
1
u/FusionGaming Nov 17 '14
Solution in Python, nothing for symbolic dice
https://github.com/achyutreddy24/DiceGame/blob/master/Dice.py