Roll method for DieHandle and better print for Die

This commit is contained in:
2025-09-14 11:45:07 +02:00
parent 5c9b44701f
commit 2f8a1a36d5
3 changed files with 107 additions and 0 deletions

View File

@@ -33,6 +33,13 @@ Die >> initialize [
faces := 6
]
{ #category : #initialization }
Die >> printOn: aStream [
super printOn: aStream.
aStream nextPutAll: ' (', faces printString, ')'
]
{ #category : #accessing }
Die >> roll [

View File

@@ -0,0 +1,51 @@
Class {
#name : #DieHandle,
#superclass : #Object,
#instVars : [
'dice'
],
#category : #Dice
}
{ #category : #initialization }
DieHandle >> addDie: aDie [
dice add: aDie
]
{ #category : #initialization }
DieHandle >> dice [
^ dice
]
{ #category : #initialization }
DieHandle >> diceNumber [
^ dice size
]
{ #category : #initialization }
DieHandle >> initialize [
super initialize.
dice := OrderedCollection new.
]
{ #category : #accessing }
DieHandle >> maxValue [
| res |
res := 0.
dice do: [ :each | res := res + each faces ].
^ res
]
{ #category : #accessing }
DieHandle >> roll [
| res |
res := 0.
dice do: [ :each | res := res + each roll ].
^ res
]

View File

@@ -0,0 +1,49 @@
Class {
#name : #DieHandleTest,
#superclass : #TestCase,
#category : #Dice
}
{ #category : #tests }
DieHandleTest >> testCreationAndAdding [
| handle |
handle := DieHandle new
addDie: (Die withFaces: 6);
addDie: (Die withFaces: 10);
yourself.
self assert: handle diceNumber equals: 2
]
{ #category : #tests }
DieHandleTest >> testCreationWithTheSameDice [
| handle |
handle := DieHandle new addDie: (Die withFaces: 6).
self assert: handle diceNumber equals: 1.
handle addDie: (Die withFaces: 6).
self assert: handle diceNumber equals: 2.
]
{ #category : #tests }
DieHandleTest >> testMaxValue [
| handle |
handle := DieHandle new
addDie: (Die withFaces: 6);
addDie: (Die withFaces: 10);
yourself.
self assert: handle maxValue equals: 16
]
{ #category : #tests }
DieHandleTest >> testRoll [
| handle |
handle := DieHandle new
addDie: (Die withFaces: 6);
addDie: (Die withFaces: 10);
yourself.
10 timesRepeat:
[ self assert: (handle roll between: handle diceNumber and: handle maxValue) ]
]