diff options
| author | Eugen Wissner <belka@caraus.de> | 2025-11-23 17:01:59 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2025-11-23 17:01:59 +0100 |
| commit | bf11813e4fa859a4833cab226c4ea560765d6d77 (patch) | |
| tree | 480e175d8dbb27376841c27dd4250f89f7bfb9a9 /pharo-mooc/redo/src/Dice | |
| parent | 8568518b57dba05500b63a4e471f8fa65883d10a (diff) | |
| download | book-exercises-bf11813e4fa859a4833cab226c4ea560765d6d77.tar.gz | |
Add ReDo Pharo excercises
Diffstat (limited to 'pharo-mooc/redo/src/Dice')
| -rw-r--r-- | pharo-mooc/redo/src/Dice/Die.class.st | 47 | ||||
| -rw-r--r-- | pharo-mooc/redo/src/Dice/DieHandle.class.st | 61 | ||||
| -rw-r--r-- | pharo-mooc/redo/src/Dice/DieHandleTest.class.st | 61 | ||||
| -rw-r--r-- | pharo-mooc/redo/src/Dice/DieTest.class.st | 30 | ||||
| -rw-r--r-- | pharo-mooc/redo/src/Dice/Integer.extension.st | 28 | ||||
| -rw-r--r-- | pharo-mooc/redo/src/Dice/package.st | 1 |
6 files changed, 228 insertions, 0 deletions
diff --git a/pharo-mooc/redo/src/Dice/Die.class.st b/pharo-mooc/redo/src/Dice/Die.class.st new file mode 100644 index 0000000..07b844a --- /dev/null +++ b/pharo-mooc/redo/src/Dice/Die.class.st @@ -0,0 +1,47 @@ +Class { + #name : #Die, + #superclass : #Object, + #instVars : [ + 'faces' + ], + #category : #Dice +} + +{ #category : #'instance creation' } +Die class >> withFaces: anInteger [ + + | d | + d := Die new. + d faces: anInteger. + ^ d +] + +{ #category : #accessing } +Die >> faces [ + ^ faces +] + +{ #category : #accessing } +Die >> faces: anInteger [ + faces := anInteger +] + +{ #category : #initialization } +Die >> initialize [ + + super initialize. + faces := 6 +] + +{ #category : #initialization } +Die >> printOn: aStream [ + + super printOn: aStream. + aStream nextPutAll: ' (', faces printString, ')' +] + +{ #category : #accessing } +Die >> roll [ + + ^ faces atRandom +] diff --git a/pharo-mooc/redo/src/Dice/DieHandle.class.st b/pharo-mooc/redo/src/Dice/DieHandle.class.st new file mode 100644 index 0000000..6820367 --- /dev/null +++ b/pharo-mooc/redo/src/Dice/DieHandle.class.st @@ -0,0 +1,61 @@ +Class { + #name : #DieHandle, + #superclass : #Object, + #instVars : [ + 'dice' + ], + #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 [ + + 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/pharo-mooc/redo/src/Dice/DieHandleTest.class.st b/pharo-mooc/redo/src/Dice/DieHandleTest.class.st new file mode 100644 index 0000000..ac6bc55 --- /dev/null +++ b/pharo-mooc/redo/src/Dice/DieHandleTest.class.st @@ -0,0 +1,61 @@ +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) ] +] + +{ #category : #tests } +DieHandleTest >> testSimpleHandle [ + + self assert: 2 D20 equals: 2 +] + +{ #category : #tests } +DieHandleTest >> testSumming [ + + self assert: (3 D4 + 2 D6) diceNumber equals: 5 +] diff --git a/pharo-mooc/redo/src/Dice/DieTest.class.st b/pharo-mooc/redo/src/Dice/DieTest.class.st new file mode 100644 index 0000000..f62b2af --- /dev/null +++ b/pharo-mooc/redo/src/Dice/DieTest.class.st @@ -0,0 +1,30 @@ +Class { + #name : #DieTest, + #superclass : #TestCase, + #category : #Dice +} + +{ #category : #tests } +DieTest >> testCreationIsOk [ + + | d | + d := Die withFaces: 20. + self assert: d faces equals: 20 +] + +{ #category : #tests } +DieTest >> testInitializationIsOk [ + + | d | + d := Die new. + self assert: d faces equals: 6 +] + +{ #category : #tests } +DieTest >> testRolling [ + + | d | + d := Die new. + 10 timesRepeat: + [ self assert: (d roll between: 1 and: 6) ] +] diff --git a/pharo-mooc/redo/src/Dice/Integer.extension.st b/pharo-mooc/redo/src/Dice/Integer.extension.st new file mode 100644 index 0000000..f97a827 --- /dev/null +++ b/pharo-mooc/redo/src/Dice/Integer.extension.st @@ -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 +] diff --git a/pharo-mooc/redo/src/Dice/package.st b/pharo-mooc/redo/src/Dice/package.st new file mode 100644 index 0000000..30779b6 --- /dev/null +++ b/pharo-mooc/redo/src/Dice/package.st @@ -0,0 +1 @@ +Package { #name : #Dice } |
