Controller
[sourcecode language="css"]
import view
import model
class Controller:
def __init__(self):
self.view = view.View()
“”"
Call this method to start a new game starting at level 1
“”"
def start_new_game(self):
self.level_num = 1
self.start_game()
“”"
Call this method to start a new game at a certain level
“”"
def start_game_at_level(self, level):
self.level_num = level
self.start_game()
“”"
Call this method to start a game based on current level
“”"
def start_game(self):
self.score = 0
if self.level_num < 3:
self.level = model.CountingLevel()
else:
self.level = model.AdditionLevel()
self.ask_new_question()
"""
Asks the current question
"""
def ask_question(self):
self.view.display_question(self.question, self.handle_answer)
"""
Asks a newly generated question
"""
def ask_new_question(self):
self.question = self.level.get_question()
self.ask_question()
"""
Returns a boolean whether the given answer matches the level's answer or not
"""
def verify_answer(self, answer):
return self.level.is_correct(answer)
"""
Callback for the view when an answer is given. Determines what to do with the given answer and how to proceed.
"""
def handle_answer(self, answer):
if self.verify_answer(answer):
# Success, go to next level
self.score = self.score + 3
if (self.level.questions_left > 0) and (self.score < (3 * 15)):
# Ask another question
self.ask_new_question()
else:
# Answered all of the questions in the level, ask if want to go to next level
if self.view.ask_next_level():
self.level_num = self.level_num + 1
self.start_game()
else:
# User wants to stay on this level, so ask new questions
self.ask_new_question()
else:
# Failed, try again with same question?
self.ask_question()
[/sourcecode]
Model
[sourcecode language="css"]
from random import randint
class Level:
def __init__(self):
pass
“”"
Returns a new question to ask
“”"
def get_question(self):
raise NotImplementedError( “Should have implemented this” )
“”"
Returns True if answer is the solution to the question
“”"
def is_correct(self, answer):
raise NotImplementedError( “Should have implemented this” )
class CountingLevel(Level):
def __init__(self):
self.questions_left = 20
“”"
Returns a new question to ask
“”"
def get_question(self):
self.questions_left = self.questions_left – 1
self.answer = randint(0, 10) # Number of objects
# Return in a way for the view to parse and display animals/objects
self.question = “counting: ” + str(self.answer)
return self.question
“”"
Returns True if answer is the solution to the question
“”"
def is_correct(self, answer):
try:
given_answer = int(answer)
except ValueError:
return False
return given_answer == self.answer
class AdditionLevel(Level):
def __init__(self):
self.questions_left = 20
“”"
Returns a new question to ask
“”"
def get_question(self):
self.questions_left = self.questions_left – 1
self.addend1 = randint(0, 10)
self.addend2 = randint(0, 10)
self.answer = self.addend1 + self.addend2
# Return in a way for the view to parse and display animals/objects
self.question = “addition: ” + str(self.addend1) + ” ” + str(self.addend2)
return self.question
“”"
Returns True if answer is the solution to the question
“”"
def is_correct(self, answer):
try:
given_answer = int(answer)
except ValueError:
return False
return given_answer == self.answer
[/sourcecode]
View
[sourcecode language="css"]
class View:
def __init__(self):
pass
def display_question(self, question, callback):
# TODO: Parse question to know how to represent the animals/objects
self.callback = callback
print(question) # Test
def handle_answer(self, user_input):
#self.callback(user_input)
pass
def background(self):
#show window background
pass
def correct_view(self):
pass
[/sourcecode]