Friday, 8 February 2013

Some example Smalltalk code for a simple quiz

I've been playing with Pharo Smalltalk over the past few weeks and have been impressed with its elegance and feature set, literally everything is right there in the environment, from UI dialog boxes, to sound effects.

One of the things that surprised me though was the lack of example code on the internet. This is a real contrast to something like Python/Django, which has example code for everything easily available.

So I'm going to try posting a bit of code now and again to see if this of use to others.

In this example:
  • Some simple classes for a quiz, you can easily extend this to ask any sorts of text questions
  • Basic use of OrderedCollection
  • Popup dialog boxes for the questions and answers
First add a new category for your quiz classes, I called mine EricQuiz:

SystemOrganization addCategory: #EricQuiz


Now add a new class Question and its methods:

Object subclass: #Question
    instanceVariableNames: 'question answers correctAnswer'
    classVariableNames: '' 

    poolDictionaries: '' 
    category: 'EricQuiz'
askQuestion
    "Ask the question and return 1 if answered correctly, 0 otherwise. If the user gets the answer wrong, they can try again."
    | choice | 
    choice := UIManager default chooseFrom: answers 
        message: question. 
    (choice = correctAnswer) ifTrue: [ 
        self inform: 'Correct!!'. 
        ^ 1 
    ifFalse: [ 
        (self confirm: 'Sorry wrong answer. Try again?') 
        ifTrue: [ ^ self askQuestion ] 
        ifFalse: [ ^ 0 ] 
    

setAnswers: 
list 
    answers := list 

setCorrectAnswer: 
index 
    correctAnswer := index 

setQuestion: 
text 
    question := text

Now add the Quiz class, which is a collection of questions and a score.

Object subclass: #Quiz 
    instanceVariableNames: 'questions score' 
    classVariableNames: '' 
    poolDictionaries: '' 
    category: 'EricQuiz'

Add these methods as class methods:

capitalCitiesQuiz 
    | quiz | 
    quiz := Quiz new. 
    quiz questions add: (Question new setQuestion: 'What''s the capital of England'; setAnswers: #('Paris' 'London' 'Amsterdam'); setCorrectAnswer: 2). 
    quiz questions add: (Question new setQuestion: 'What''s the capital of the Netherlands'; setAnswers: #('Brussles' 'Schipol' 'Amsterdam'); setCorrectAnswer: 3). 
    ^ quiz

spellingQuiz 
    | quiz | 
    quiz := Quiz new. 
    quiz questions add: (Question new 
        setQuestion: 'Which is the correct spelling?'; 
        setAnswers: #('Abuve' 'Above' 'Abov'); 
        setCorrectAnswer: 2). 
    quiz questions add: (Question new 
        setQuestion: 'Which is the correct spelling?'; 
        setAnswers: #('Anser' 'Ansor' 'Answer' 'Arnser'); 
        setCorrectAnswer: 3). 
    quiz questions add: (Question new 
        setQuestion: 'Which is the correct spelling?'; 
        setAnswers: #('Ardvark' 'Ardvarc' 'Aardvark'); 
        setCorrectAnswer: 3). 
    ^ quiz

And these as instance methods:

askQuestions
    score := 0. 
    questions do: [ :question | 
        score := score + question askQuestion ]. 
    self inform: 'You scored ', score asString, ' out of ', (questions size) asString

questions 
    questions ifNil: [ questions := OrderedCollection new ]. 
    ^ questions

questions: anObject 
    questions := anObject

Finally, you can run the quiz by running:

Quiz capitalCitiesQuiz askQuestions.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Update:

For basic language examples, like Strings and Streams, there's a great free book called "Smalltalk by Example: the Developer's Guide" by Alex Sharp:
http://stephane.ducasse.free.fr/FreeBooks/ByExample/

Thanks to St├ęphane Ducasse for the links: http://stephane.ducasse.free.fr/FreeBooks.html

No comments:

Post a Comment