Add ReDo Pharo excercises
This commit is contained in:
3
pharo-mooc/redo/.project
Normal file
3
pharo-mooc/redo/.project
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
'srcDirectory' : 'src'
|
||||
}
|
||||
3
pharo-mooc/redo/src/.properties
Normal file
3
pharo-mooc/redo/src/.properties
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
#format : #tonel
|
||||
}
|
||||
47
pharo-mooc/redo/src/Dice/Die.class.st
Normal file
47
pharo-mooc/redo/src/Dice/Die.class.st
Normal file
@@ -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
|
||||
]
|
||||
61
pharo-mooc/redo/src/Dice/DieHandle.class.st
Normal file
61
pharo-mooc/redo/src/Dice/DieHandle.class.st
Normal file
@@ -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
|
||||
]
|
||||
61
pharo-mooc/redo/src/Dice/DieHandleTest.class.st
Normal file
61
pharo-mooc/redo/src/Dice/DieHandleTest.class.st
Normal file
@@ -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
|
||||
]
|
||||
30
pharo-mooc/redo/src/Dice/DieTest.class.st
Normal file
30
pharo-mooc/redo/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) ]
|
||||
]
|
||||
28
pharo-mooc/redo/src/Dice/Integer.extension.st
Normal file
28
pharo-mooc/redo/src/Dice/Integer.extension.st
Normal file
@@ -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
|
||||
]
|
||||
1
pharo-mooc/redo/src/Dice/package.st
Normal file
1
pharo-mooc/redo/src/Dice/package.st
Normal file
@@ -0,0 +1 @@
|
||||
Package { #name : #Dice }
|
||||
60
pharo-mooc/redo/src/MyCounter/Counter.class.st
Normal file
60
pharo-mooc/redo/src/MyCounter/Counter.class.st
Normal file
@@ -0,0 +1,60 @@
|
||||
"
|
||||
Counter is a simple concrete class which supports incrementing and
|
||||
decrementing a counter.
|
||||
|
||||
Its API is
|
||||
- decrement, increment
|
||||
- count
|
||||
Its creation API is message startingAt:
|
||||
"
|
||||
Class {
|
||||
#name : #Counter,
|
||||
#superclass : #Object,
|
||||
#instVars : [
|
||||
'count'
|
||||
],
|
||||
#category : #MyCounter
|
||||
}
|
||||
|
||||
{ #category : #accessing }
|
||||
Counter class >> startingAt: anInteger [
|
||||
^ self new count: anInteger
|
||||
]
|
||||
|
||||
{ #category : #accessing }
|
||||
Counter >> count [
|
||||
|
||||
^ count
|
||||
]
|
||||
|
||||
{ #category : #accessing }
|
||||
Counter >> count: anInteger [
|
||||
|
||||
count := anInteger
|
||||
]
|
||||
|
||||
{ #category : #operation }
|
||||
Counter >> decrement [
|
||||
|
||||
count := count - 1
|
||||
]
|
||||
|
||||
{ #category : #operation }
|
||||
Counter >> increment [
|
||||
|
||||
count := count + 1
|
||||
]
|
||||
|
||||
{ #category : #initialization }
|
||||
Counter >> initialize [
|
||||
super initialize.
|
||||
count := 0
|
||||
]
|
||||
|
||||
{ #category : #accessing }
|
||||
Counter >> printOn: aStream [
|
||||
|
||||
super printOn: aStream. "a Counter"
|
||||
aStream nextPutAll: ' withValue: ', count printString.
|
||||
aStream cr
|
||||
]
|
||||
55
pharo-mooc/redo/src/MyCounter/CounterTest.class.st
Normal file
55
pharo-mooc/redo/src/MyCounter/CounterTest.class.st
Normal file
@@ -0,0 +1,55 @@
|
||||
Class {
|
||||
#name : #CounterTest,
|
||||
#superclass : #TestCase,
|
||||
#category : #MyCounter
|
||||
}
|
||||
|
||||
{ #category : #tests }
|
||||
CounterTest >> testCountIsSetAndRead [
|
||||
| c |
|
||||
c := Counter new.
|
||||
c count: 7.
|
||||
self assert: c count equals: 7
|
||||
]
|
||||
|
||||
{ #category : #tests }
|
||||
CounterTest >> testCounterWellInitialized [
|
||||
self assert: (Counter new increment; increment; count) equals: 2
|
||||
]
|
||||
|
||||
{ #category : #tests }
|
||||
CounterTest >> testDecrement [
|
||||
| c |
|
||||
c := Counter new.
|
||||
c count: 2.
|
||||
c decrement; decrement.
|
||||
self assert: c count equals: 0
|
||||
]
|
||||
|
||||
{ #category : #tests }
|
||||
CounterTest >> testIncrement [
|
||||
| c |
|
||||
c := Counter new.
|
||||
c count: 2.
|
||||
c increment; increment.
|
||||
self assert: c count equals: 4
|
||||
]
|
||||
|
||||
{ #category : #tests }
|
||||
CounterTest >> testInizialize [
|
||||
self assert: Counter new count equals: 0
|
||||
]
|
||||
|
||||
{ #category : #tests }
|
||||
CounterTest >> testStartingAt5 [
|
||||
| c |
|
||||
c := Counter startingAt: 5.
|
||||
self assert: c count equals: 5
|
||||
]
|
||||
|
||||
{ #category : #tests }
|
||||
CounterTest >> testStartingAt5Increment [
|
||||
| c |
|
||||
c := Counter startingAt: 5.
|
||||
self assert: (c increment; count) equals: 6
|
||||
]
|
||||
1
pharo-mooc/redo/src/MyCounter/package.st
Normal file
1
pharo-mooc/redo/src/MyCounter/package.st
Normal file
@@ -0,0 +1 @@
|
||||
Package { #name : #MyCounter }
|
||||
Reference in New Issue
Block a user