Class method to specify for number of faces with test
This commit is contained in:
40
src/Dice/Die.class.st
Normal file
40
src/Dice/Die.class.st
Normal 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
30
src/Dice/DieTest.class.st
Normal 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
1
src/Dice/package.st
Normal file
@@ -0,0 +1 @@
|
||||
Package { #name : #Dice }
|
Reference in New Issue
Block a user