Skip to content

Instantly share code, notes, and snippets.

@KazunoriUeda
Created June 21, 2019 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KazunoriUeda/fe906bca77b55033ff39edd74f84c276 to your computer and use it in GitHub Desktop.
Save KazunoriUeda/fe906bca77b55033ff39edd74f84c276 to your computer and use it in GitHub Desktop.
SmalltalkJenga v1.0.1
Object subclass: #SmalltalkJenga
instanceVariableNames: 'random classList turn'
classVariableNames: ''
poolDictionaries: ''
category: 'SmalltalkJenga'!
!SmalltalkJenga methodsFor: 'actions' stamp: 'KazunoriUeda 12/19/2018 18:00'!
playAll
[ self play ] doWhileTrue: [ 0 < (self listSize) ].
! !
!SmalltalkJenga methodsFor: 'actions' stamp: 'KazunoriUeda 6/21/2019 17:45'!
play
(0 < self listSize)
ifTrue: [
| target |
self turn: (self turn) + 1.
target := PopupChoiceDialogWindow chooseFrom: (self randomList: 10)
lines: #()
title: ('Turn {1} (classes: {2})' format: {self turn asString. self classList size asString}).
target ifNil: [ self turn: (self turn) - 1. ^self ].
target := self classList removeAt: (target key) asInteger.
DialogWindow new title: ('The class {1} has deleted!!' format: {target asString}); openModal.
"target removeFromSystem." ]
ifFalse: [
DialogWindow new title: 'Awesome!! All classes are deleted!!!!!!'; openModal ]! !
!SmalltalkJenga methodsFor: 'initialize' stamp: 'KazunoriUeda 12/19/2018 18:00'!
initialize
random := nil.
classList := nil.
turn := 0.! !
!SmalltalkJenga methodsFor: 'accessing' stamp: 'KazunoriUeda 6/11/2019 09:28'!
randomIndex: anInteger
^ self random nextInt: anInteger! !
!SmalltalkJenga methodsFor: 'accessing' stamp: 'KazunoriUeda 12/19/2018 18:00'!
listSize
^ self classList size! !
!SmalltalkJenga methodsFor: 'accessing' stamp: 'KazunoriUeda 12/19/2018 18:00'!
turn
^ turn! !
!SmalltalkJenga methodsFor: 'accessing' stamp: 'KazunoriUeda 6/11/2019 18:04'!
randomList: anInteger
| resultList tmpList |
resultList := OrderedCollection new.
tmpList := self classList deepCopy.
1 to: (anInteger min: tmpList size) do: [ :idx |
| n item |
n := self randomIndex: tmpList size.
item := Association key: n asString value: ((tmpList removeAt: n) asString).
resultList add: item. ].
^ resultList! !
!SmalltalkJenga methodsFor: 'accessing' stamp: 'KazunoriUeda 12/19/2018 18:00'!
random
^ random ifNil: [ random := Random seed: (DateAndTime now asUnixTime) ]! !
!SmalltalkJenga methodsFor: 'accessing' stamp: 'KazunoriUeda 12/19/2018 18:00'!
turn: anObject
turn := anObject! !
!SmalltalkJenga methodsFor: 'accessing' stamp: 'KazunoriUeda 12/19/2018 18:00'!
classList
^ classList ifNil: [ classList := Smalltalk globals allClasses reject: [ :each | each = self class ] ]! !
!SmalltalkJenga methodsFor: 'accessing' stamp: 'KazunoriUeda 12/19/2018 18:00'!
classList: anObject
classList := anObject! !
!SmalltalkJenga methodsFor: 'accessing' stamp: 'KazunoriUeda 6/11/2019 09:29'!
randomIndex
^ self randomIndex: self listSize! !
!SmalltalkJenga methodsFor: 'accessing' stamp: 'KazunoriUeda 12/19/2018 18:00'!
random: anObject
random := anObject! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
SmalltalkJenga class
instanceVariableNames: 'default'!
!SmalltalkJenga class methodsFor: 'instance creation' stamp: 'KazunoriUeda 12/19/2018 18:00'!
default
default ifNil: [default := self new].
^default! !
!SmalltalkJenga class methodsFor: 'actions' stamp: 'KazunoriUeda 12/19/2018 18:00'!
playAll
self default playAll! !
!SmalltalkJenga class methodsFor: 'actions' stamp: 'KazunoriUeda 12/19/2018 18:00'!
play
self default play! !
!SmalltalkJenga class methodsFor: 'class initialization' stamp: 'KazunoriUeda 12/19/2018 18:00'!
initialize
default := nil! !
SmalltalkJenga initialize!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment