Skip to content

Commit

Permalink
Made extract methods. Implemented CardHistory.
Browse files Browse the repository at this point in the history
  • Loading branch information
frandegrandis committed Jun 15, 2020
1 parent bd5173d commit f2cf9ca
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 68 deletions.
6 changes: 3 additions & 3 deletions repository/IngSoft2-Model/Acceleration.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ Class {
}

{ #category : #playing }
Acceleration >> triggerEffectFor: aGame [
aGame moveActualPlayerForward: 1.
Acceleration >> triggerEffectFor: aGame [
aGame moveActualPlayerForward: 1
]

{ #category : #playing }
Acceleration >> useEffectFor: aGame by: aPlayer [
aGame addPermanent: self by: aPlayer.
aGame addPermanent: self by: aPlayer
]
3 changes: 1 addition & 2 deletions repository/IngSoft2-Model/Cancellation.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ Cancellation >> triggerEffectFor: aGame [

{ #category : #playing }
Cancellation >> useEffectFor: aGame by: aPlayer [
target isValid
ifFalse: [ InvalidAction signal: 'Cannot be used without a target' ].
target isValid.
aGame removePermanentCard: target objective.
aGame addToDeck: self
]
12 changes: 6 additions & 6 deletions repository/IngSoft2-Model/Card.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ Card class >> to: aTarget [
]

{ #category : #comparing }
Card >> = aCard [
^ (aCard isMemberOf: self class)
Card >> = aCard [
^ aCard isMemberOf: self class
]

{ #category : #initialization }
Card >> initializeWithTarget: aTarget [
target := aTarget.
Card >> initializeWithTarget: aTarget [
target := aTarget
]

{ #category : #comparing }
Card >> isIdenticalTo: aCard [
^ (aCard = self) & aCard target= target
Card >> isIdenticalTo: aCard [
^ aCard = self & aCard target = target
]

{ #category : #accessing }
Expand Down
4 changes: 2 additions & 2 deletions repository/IngSoft2-Model/CardDeck.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ CardDeck class >> withAllCards [
{ #category : #accessing }
CardDeck >> draw [
| aCard |
aCard := (cards at: (source nextInt: cards size)).
aCard := cards at: (source nextInt: cards size).
^ aCard copy
]

{ #category : #initialization }
CardDeck >> initializeWithCardTypes: aCollection of: aSource [
cards := aCollection.
source := aSource.
source := aSource
]
54 changes: 54 additions & 0 deletions repository/IngSoft2-Model/CardHistory.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
Class {
#name : #CardHistory,
#superclass : #Object,
#instVars : [
'thrownCards'
],
#category : #'IngSoft2-Model-Card'
}

{ #category : #acccessing }
CardHistory >> add: aCard [
thrownCards add: aCard
]

{ #category : #initialization }
CardHistory >> initialize [
thrownCards := OrderedCollection new
]

{ #category : #acccessing }
CardHistory >> last [
^ thrownCards last copy
]

{ #category : #acccessing }
CardHistory >> remove: aCard [
| cardsIdenticalToCard cardsNotIdenticalToCard |
cardsIdenticalToCard := self selectIdenticalTo: aCard.
self removeFirstFrom: cardsIdenticalToCard.
cardsNotIdenticalToCard := self selectNotIdenticalTo: aCard.
cardsNotIdenticalToCard addAll: cardsIdenticalToCard.
thrownCards := cardsNotIdenticalToCard
]

{ #category : #private }
CardHistory >> removeFirstFrom: cardsIdenticalToCard [
cardsIdenticalToCard isEmpty
ifFalse: [ cardsIdenticalToCard removeFirst ]
]

{ #category : #private }
CardHistory >> selectIdenticalTo: aCard [
^ thrownCards select: [ :card | card isIdenticalTo: aCard ]
]

{ #category : #private }
CardHistory >> selectNotIdenticalTo: aCard [
^ thrownCards select: [ :card | (card isIdenticalTo: aCard) not ]
]

{ #category : #playing }
CardHistory >> triggerEffectsFor: aGame [
thrownCards do: [ :aCard | aCard triggerEffectFor: aGame ]
]
16 changes: 6 additions & 10 deletions repository/IngSoft2-Model/Game.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Class {
'scoreboard',
'board',
'deck',
'thrownCards'
'cardHistory'
],
#category : #'IngSoft2-Model-Game'
}
Expand Down Expand Up @@ -41,7 +41,7 @@ Game class >> with: dice on: aBoard playedBy: players withLaps: laps shuffling:

{ #category : #playing }
Game >> activatePermanentEffects [
thrownCards do: [ :aCard | aCard triggerEffectFor: self ]
cardHistory triggerEffectsFor: self
]

{ #category : #accessing }
Expand All @@ -57,7 +57,7 @@ Game >> addPermanent: aCard by: aPlayer [

{ #category : #playing }
Game >> addToDeck: aCard [
thrownCards add: aCard
cardHistory add: aCard
]

{ #category : #results }
Expand Down Expand Up @@ -108,7 +108,7 @@ Game >> initializeDice: aDice withBoard: aBoard andPlayers: players andLaps: amm
state := Started new.
laps := ammountLaps.
scoreboard := Scoreboard withRows: rows.
thrownCards := OrderedCollection new.
cardHistory := CardHistory new.

]

Expand All @@ -119,7 +119,7 @@ Game >> laps [

{ #category : #accessing }
Game >> lastCard [
^thrownCards last copy
^cardHistory last
]

{ #category : #accessing }
Expand Down Expand Up @@ -209,11 +209,7 @@ Game >> podiumOf: aScoreboardRaw [

{ #category : #playing }
Game >> removePermanentCard: aCard [
| aColl |
aColl := thrownCards select: [ :card | (card isIdenticalTo: aCard) ].
aColl isEmpty ifFalse: [aColl removeFirst].
thrownCards :=(thrownCards select: [ :card | (card isIdenticalTo: aCard) not]) addAll: aColl.

cardHistory remove: aCard
]

{ #category : #playing }
Expand Down
7 changes: 6 additions & 1 deletion repository/IngSoft2-Model/NonTarget.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ Class {
#category : #'IngSoft2-Model-Card'
}

{ #category : #comparing }
NonTarget >> = aTarget [
^ (aTarget isMemberOf: NonTarget)
]

{ #category : #checking }
NonTarget >> isValid [
^false
InvalidAction signal: 'Cannot be used without a target'
]

{ #category : #accessing }
Expand Down
5 changes: 2 additions & 3 deletions repository/IngSoft2-Model/Overload.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Overload >> triggerEffectFor: aGame [

{ #category : #playing }
Overload >> useEffectFor: aGame by: aPlayer [
target isValid
ifFalse: [ InvalidAction signal: 'Cannot be used without a target' ].
aGame addPermanent: self by: aPlayer.
target isValid.
aGame addPermanent: self by: aPlayer
]
2 changes: 1 addition & 1 deletion repository/IngSoft2-Model/Repeat.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ Repeat >> triggerEffectFor: aGame [
{ #category : #playing }
Repeat >> useEffectFor: aGame by: aPlayer [
aGame repeatLastEffect.
aGame addToDeck: self.
aGame addToDeck: self
]
10 changes: 5 additions & 5 deletions repository/IngSoft2-Model/Speed.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ Speed class >> to: aTarget [
]

{ #category : #playing }
Speed >> triggerEffectFor: aGame [
((target objective )= (aGame actualPlayer)) ifTrue: [ aGame moveActualPlayerForward: 1 ]
Speed >> triggerEffectFor: aGame [
target objective = aGame actualPlayer
ifTrue: [ aGame moveActualPlayerForward: 1 ]
]

{ #category : #playing }
Speed >> useEffectFor: aGame by: aPlayer [
target isValid
ifFalse: [ InvalidAction signal: 'Cannot be used without a target' ].
aGame addPermanent: self by: aPlayer.
target isValid.
aGame addPermanent: self by: aPlayer
]
8 changes: 4 additions & 4 deletions repository/IngSoft2-Model/Target.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ Target class >> pointing: anAcceleration [

{ #category : #comparing }
Target >> = aTarget [
^ (aTarget isMemberOf: aTarget class)
^ (aTarget isMemberOf: self class)
ifTrue: [ objective = aTarget objective ]
ifFalse: [ false ]
]

{ #category : #initialization }
Target >> initializePointingTo: anObjective [
objective := anObjective.
Target >> initializePointingTo: anObjective [
objective := anObjective
]

{ #category : #checking }
Expand All @@ -31,5 +31,5 @@ Target >> isValid [

{ #category : #accessing }
Target >> objective [
^objective
^ objective
]
1 change: 0 additions & 1 deletion repository/IngSoft2-Model/TargetCard.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ Class {

{ #category : #checking }
TargetCard >> isValid [
^true
]
1 change: 0 additions & 1 deletion repository/IngSoft2-Model/TargetPlayer.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ TargetPlayer >> initializePointingTo: aPlayerName [

{ #category : #checking }
TargetPlayer >> isValid [
^true
]
23 changes: 17 additions & 6 deletions repository/IngSoft2-Tests/CardTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ Class {
#category : #'IngSoft2-Tests-Card-Tests'
}

{ #category : #tests }
CardTest >> assertAverageIsBetween16and17Of: overloadCount [
self
assert: ((self averagePercentOf: overloadCount) between: 16 and: 17)
]

{ #category : #tests }
CardTest >> averagePercentOf: overloadCount [
^ (overloadCount / 100000)*100
]

{ #category : #tests }
CardTest >> testCancellationCannotBeInitializeWithATargetPlayer [
self
Expand All @@ -27,12 +38,12 @@ CardTest >> testCardDeckHasUniformDistribution [
count: [ :card | card isMemberOf: Cancellation ].
redoCount := cardsDrawn count: [ :card | card isMemberOf: Redo ].
repeatCount := cardsDrawn count: [ :card | card isMemberOf: Repeat ].
self assert: (overloadCount // 1000 between: 16 and: 17).
self assert: (speedCount // 1000 between: 16 and: 17).
self assert: (accelerationCount // 1000 between: 16 and: 17).
self assert: (cancellationCount // 1000 between: 16 and: 17).
self assert: (redoCount // 1000 between: 16 and: 17).
self assert: (repeatCount // 1000 between: 16 and: 17)
self assertAverageIsBetween16and17Of: overloadCount.
self assertAverageIsBetween16and17Of: speedCount.
self assertAverageIsBetween16and17Of: accelerationCount.
self assertAverageIsBetween16and17Of: cancellationCount.
self assertAverageIsBetween16and17Of: redoCount.
self assertAverageIsBetween16and17Of: repeatCount
]

{ #category : #tests }
Expand Down
Loading

0 comments on commit f2cf9ca

Please sign in to comment.