From 2f8a1a36d55cc94694f69fd92ab7855eea31328e Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Sun, 14 Sep 2025 11:45:07 +0200 Subject: [PATCH] Roll method for DieHandle and better print for Die --- src/Dice/Die.class.st | 7 +++++ src/Dice/DieHandle.class.st | 51 +++++++++++++++++++++++++++++++++ src/Dice/DieHandleTest.class.st | 49 +++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 src/Dice/DieHandle.class.st create mode 100644 src/Dice/DieHandleTest.class.st diff --git a/src/Dice/Die.class.st b/src/Dice/Die.class.st index e73b65d..07b844a 100644 --- a/src/Dice/Die.class.st +++ b/src/Dice/Die.class.st @@ -33,6 +33,13 @@ Die >> initialize [ faces := 6 ] +{ #category : #initialization } +Die >> printOn: aStream [ + + super printOn: aStream. + aStream nextPutAll: ' (', faces printString, ')' +] + { #category : #accessing } Die >> roll [ diff --git a/src/Dice/DieHandle.class.st b/src/Dice/DieHandle.class.st new file mode 100644 index 0000000..2162c62 --- /dev/null +++ b/src/Dice/DieHandle.class.st @@ -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 +] diff --git a/src/Dice/DieHandleTest.class.st b/src/Dice/DieHandleTest.class.st new file mode 100644 index 0000000..5761e65 --- /dev/null +++ b/src/Dice/DieHandleTest.class.st @@ -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) ] +]