Roll method for DieHandle and better print for Die
This commit is contained in:
@@ -33,6 +33,13 @@ Die >> initialize [
|
||||
faces := 6
|
||||
]
|
||||
|
||||
{ #category : #initialization }
|
||||
Die >> printOn: aStream [
|
||||
|
||||
super printOn: aStream.
|
||||
aStream nextPutAll: ' (', faces printString, ')'
|
||||
]
|
||||
|
||||
{ #category : #accessing }
|
||||
Die >> roll [
|
||||
|
||||
|
51
src/Dice/DieHandle.class.st
Normal file
51
src/Dice/DieHandle.class.st
Normal 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
|
||||
]
|
49
src/Dice/DieHandleTest.class.st
Normal file
49
src/Dice/DieHandleTest.class.st
Normal 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) ]
|
||||
]
|
Reference in New Issue
Block a user