Extensions to the Integer class for Dies and operation on DieHandle

This commit is contained in:
2025-09-16 09:40:03 +02:00
parent 2f8a1a36d5
commit 6829d8e6a2
3 changed files with 50 additions and 0 deletions

View File

@@ -7,6 +7,16 @@ Class {
#category : #Dice
}
{ #category : #initialization }
DieHandle >> + aDieHandle [
| handle |
handle := self class new.
self dice do: [ :each | handle addDie: each ].
aDieHandle dice do: [ :each | handle addDie: each ].
^ handle
]
{ #category : #initialization }
DieHandle >> addDie: aDie [

View File

@@ -47,3 +47,15 @@ DieHandleTest >> testRoll [
10 timesRepeat:
[ self assert: (handle roll between: handle diceNumber and: handle maxValue) ]
]
{ #category : #tests }
DieHandleTest >> testSimpleHandle [
self assert: 2 D20 diceNumber equals: 2
]
{ #category : #tests }
DieHandleTest >> testSumming [
self assert: (3 D4 + 2 D6) diceNumber equals: 5
]

View File

@@ -0,0 +1,28 @@
Extension { #name : #Integer }
{ #category : #'*Dice' }
Integer >> D20 [
self D: 20
]
{ #category : #'*Dice' }
Integer >> D4 [
^ self D: 4
]
{ #category : #'*Dice' }
Integer >> D6 [
^ self D: 6
]
{ #category : #'*Dice' }
Integer >> D: anInteger [
| handle |
handle := DieHandle new.
self timesRepeat: [ handle addDie: (Die withFaces: anInteger) ].
^ handle
]