Class method to specify for number of faces with test

This commit is contained in:
2025-09-14 11:12:55 +02:00
parent 222c916b3d
commit 5c9b44701f
3 changed files with 71 additions and 0 deletions

40
src/Dice/Die.class.st Normal file
View File

@@ -0,0 +1,40 @@
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 : #accessing }
Die >> roll [
^ faces atRandom
]

30
src/Dice/DieTest.class.st Normal file
View File

@@ -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) ]
]

1
src/Dice/package.st Normal file
View File

@@ -0,0 +1 @@
Package { #name : #Dice }