Skip to content

Commit 1b08ff6

Browse files
authored
Dice experimental exercise for testing new package format (#321)
2 parents d0a87fd + 3554090 commit 1b08ff6

File tree

9 files changed

+295
-0
lines changed

9 files changed

+295
-0
lines changed

config.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,17 @@
247247
"text_formatting"
248248
]
249249
},
250+
{
251+
"slug" : "die",
252+
"uuid" : "dbea4db3-5044-0d00-ab77-b35a0c226d01",
253+
"core" : false,
254+
"auto_approve" : false,
255+
"unlocked_by" : null,
256+
"difficulty" : 1,
257+
"topics" : [
258+
"math"
259+
]
260+
},
250261
{
251262
"slug" : "etl",
252263
"uuid" : "d6303b3f-0f41-0d00-a5e1-db510c056502",

exercises/die/.meta/description.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# DieExperimental Custom Exercise (Don't use yet)A small DSL for dice rolling: a nice example to get started with Pharo.## HintIt is used in the book: Learning Object-Oriented Programming, Design and TDD with Pharo available at http://books.pharo.org

exercises/die/.meta/hints.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
It is used in the book: Learning Object-Oriented Programming, Design and TDD with Pharo available at http://books.pharo.org

exercises/die/.meta/metadata.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
blurb: "A small DSL for dice rolling"
3+
source: "Stephane Ducasse"
4+
source_url: "https://github.com/Ducasse/Dice"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"
2+
I represent a die. I can have different faces number and roll.
3+
4+
"
5+
Class {
6+
#name : #Die,
7+
#superclass : #Object,
8+
#instVars : [
9+
'faces'
10+
],
11+
#category : #'Exercise@Die'
12+
}
13+
14+
{ #category : #'instance creation' }
15+
Die class >> withFaces: anInteger [
16+
^ self new faces: anInteger ; yourself
17+
]
18+
19+
{ #category : #accessing }
20+
Die >> faces [
21+
22+
^ faces
23+
]
24+
25+
{ #category : #accessing }
26+
Die >> faces: aNumber [
27+
28+
faces := aNumber
29+
]
30+
31+
{ #category : #initialization }
32+
Die >> initialize [
33+
super initialize.
34+
faces := 6
35+
]
36+
37+
{ #category : #printing }
38+
Die >> printOn: aStream [
39+
super printOn: aStream.
40+
aStream nextPutAll: ' ('.
41+
aStream print: faces.
42+
aStream nextPutAll: ')'
43+
44+
]
45+
46+
{ #category : #rolling }
47+
Die >> roll [
48+
49+
^ faces atRandom
50+
]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"
2+
I represent a handle of dice.
3+
I can roll and I can be added to other handles.
4+
5+
A dieHandle can be created using a dunjon and dragon syntax:
6+
2 D20 means two dice with 20 faces.
7+
8+
2 D20 + 3 D4
9+
Means that I should roll 5 dices.
10+
11+
12+
"
13+
Class {
14+
#name : #DieHandle,
15+
#superclass : #Object,
16+
#instVars : [
17+
'dice'
18+
],
19+
#category : #'Exercise@Die'
20+
}
21+
22+
{ #category : #adding }
23+
DieHandle >> + aDieHandle [
24+
"Returns a new handle that represents the addition of the receiver and the argument."
25+
| handle |
26+
handle := self class new.
27+
dice do: [ :each | handle addDie: each ].
28+
aDieHandle dice do: [ :each | handle addDie: each ].
29+
^ handle
30+
]
31+
32+
{ #category : #adding }
33+
DieHandle >> addDie: aDie [
34+
35+
dice add: aDie
36+
]
37+
38+
{ #category : #accessing }
39+
DieHandle >> dice [
40+
41+
^ dice
42+
]
43+
44+
{ #category : #accessing }
45+
DieHandle >> diceNumber [
46+
47+
^ dice size
48+
]
49+
50+
{ #category : #initialization }
51+
DieHandle >> initialize [
52+
super initialize.
53+
dice := OrderedCollection new
54+
]
55+
56+
{ #category : #rolling }
57+
DieHandle >> roll [
58+
59+
| res |
60+
res := 0.
61+
dice do: [ :each | res := res + each roll ].
62+
^ res
63+
]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Class {
2+
#name : #DieHandleTest,
3+
#superclass : #ExercismTest,
4+
#category : #'Exercise@Die'
5+
}
6+
7+
{ #category : #tests }
8+
DieHandleTest >> testAddingTwiceTheSameDice [
9+
| handle |
10+
handle := DieHandle new.
11+
handle addDie: (Die withFaces: 6).
12+
self assert: handle diceNumber equals: 1.
13+
handle addDie: (Die withFaces: 6).
14+
self assert: handle diceNumber equals: 2.
15+
]
16+
17+
{ #category : #tests }
18+
DieHandleTest >> testCreationAdding [
19+
| handle |
20+
handle := DieHandle new
21+
addDie: (Die withFaces: 6);
22+
addDie: (Die withFaces: 10);
23+
yourself.
24+
self assert: handle diceNumber equals: 2.
25+
]
26+
27+
{ #category : #tests }
28+
DieHandleTest >> testRoll [
29+
30+
| handle |
31+
handle := DieHandle new
32+
addDie: (Die withFaces: 6);
33+
addDie: (Die withFaces: 10);
34+
yourself.
35+
1000 timesRepeat: [ handle roll between: 2 and: 16 ]
36+
]
37+
38+
{ #category : #tests }
39+
DieHandleTest >> testSimpleHandle [
40+
self assert: 2 D20 diceNumber equals: 2.
41+
]
42+
43+
{ #category : #tests }
44+
DieHandleTest >> testSumming [
45+
46+
| handle |
47+
handle := 2 D20 + 3 D10.
48+
self assert: handle diceNumber equals: 5.
49+
]
50+
51+
{ #category : #tests }
52+
DieHandleTest >> testSummingAndRolling [
53+
54+
| handle |
55+
handle := 2 D20 + 3 D10.
56+
self assert: handle diceNumber equals: 5.
57+
100 timesRepeat:
58+
[ handle roll between: 5 and: 70 ]
59+
]

exercises/die/DieTest.class.st

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"
2+
# Die
3+
4+
Experimental Custom Exercise (Don't use yet)
5+
6+
A small DSL for dice rolling: a nice example to get started with Pharo.
7+
8+
## Hint
9+
10+
It is used in the book: Learning Object-Oriented Programming, Design and TDD with Pharo available at http://books.pharo.org
11+
12+
"
13+
Class {
14+
#name : #DieTest,
15+
#superclass : #ExercismTest,
16+
#category : #'Exercise@Die'
17+
}
18+
19+
{ #category : #config }
20+
DieTest class >> customData [
21+
^{
22+
#blurb -> 'A small DSL for dice rolling'.
23+
#source -> 'Stephane Ducasse'.
24+
#source_url -> 'https://github.com/Ducasse/Dice'
25+
} asOrderedDictionary
26+
]
27+
28+
{ #category : #config }
29+
DieTest class >> exercise [
30+
"Answer the configured exercise meta data for this exercise, an ExercismExercise"
31+
32+
^(self createExerciseAfter: HelloWorldTest)
33+
isCore: false;
34+
difficulty: 1;
35+
topics: #('math');
36+
yourself
37+
38+
39+
]
40+
41+
{ #category : #config }
42+
DieTest class >> uuid [
43+
"Answer a fixed String, the unique UUID for this exercise so the Exercism platform can identify it.
44+
The id should be like: 'b5812b5e-2788-4ea6-b948-bfe54edeb0da' "
45+
46+
^'dbea4db3-5044-0d00-ab77-b35a0c226d01'
47+
]
48+
49+
{ #category : #config }
50+
DieTest class >> version [
51+
"Answer the exercise version number string this test was derived from"
52+
53+
^'0.0.1'
54+
]
55+
56+
{ #category : #tests }
57+
DieTest >> testCreationIsOk [
58+
59+
self assert: (Die withFaces: 20) faces equals: 20
60+
61+
]
62+
63+
{ #category : #tests }
64+
DieTest >> testInitializeIsOk [
65+
66+
self assert: Die new faces equals: 6
67+
]
68+
69+
{ #category : #tests }
70+
DieTest >> testRolling [
71+
|d|
72+
d := Die new.
73+
1000 timesRepeat: [ self assert: (d roll between: 1 and: 6) ]
74+
]

exercises/die/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Die
2+
3+
# DieExperimental Custom Exercise (Don't use yet)A small DSL for dice rolling: a nice example to get started with Pharo.## HintIt is used in the book: Learning Object-Oriented Programming, Design and TDD with Pharo available at http://books.pharo.org
4+
## Hint
5+
6+
It is used in the book: Learning Object-Oriented Programming, Design and TDD with Pharo available at http://books.pharo.org
7+
8+
9+
## Downloading
10+
11+
To download this exercise in Pharo, type: `die` into the `Exercism | Fetch Exercise` package menu prompt (right click on the Exercism package in the Pharo System Browser). You can also submit your solution from the same menu for any selected package. You don't normally need to use the exercism cli (as indicated on the right hand panel).
12+
13+
## Running The Tests
14+
15+
Tests can be run directly from the Pharo IDE, by clicking on the test orb next to any test.
16+
The SUnit convention is that the provided `DieTest`, will test the functionality of `Die`.
17+
18+
If you are still stuck, the track documentation has more detailed help on [running tests](https://exercism.io/tracks/pharo/tests).
19+
20+
## Language and Environment Help
21+
22+
For Pharo installation and learning resources, refer to the [track help page](https://exercism.io/tracks/pharo/learning).
23+
24+
25+
## Source
26+
27+
Stephane Ducasse [https://github.com/Ducasse/Dice](https://github.com/Ducasse/Dice)
28+
29+
30+
## Submitting Incomplete Solutions
31+
32+
Remember, it is also possible to submit an incomplete solution so you can see how others have completed this exercise and can learn from their approach.

0 commit comments

Comments
 (0)