Extensions to the Integer class for Dies and operation on DieHandle
This commit is contained in:
@@ -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 [
|
||||
|
||||
|
@@ -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
|
||||
]
|
||||
|
28
src/Dice/Integer.extension.st
Normal file
28
src/Dice/Integer.extension.st
Normal 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
|
||||
]
|
Reference in New Issue
Block a user