Created
September 30, 2011 09:39
-
-
Save adaptives/1253269 to your computer and use it in GitHub Desktop.
LPTHW Exercise 43
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # LPTHW Exercise 43 | |
| import random | |
| """ This module contains classes which denote the Game and individual rooms of the game.""" | |
| class Game(object): | |
| def __init__(self): | |
| self.cmds = {} | |
| self.cmds["rooms"] = "Prints the list of available rooms" | |
| self.cmds["room"] = "Print the name of the current room. If followed by an argument then user will be taken to that room" | |
| self.cmds["help"] = "Lists all the commands" | |
| self.cmds["quit"] = "Quit the game" | |
| self._rooms = {} | |
| self._rooms['SpacedRepitition'] = SpacedRepitition(self) | |
| self.current_room = None | |
| self._nocmd = ["nocmd"] | |
| def start(self): | |
| print "Welcome to this wonderful game of self development!" | |
| print "Please enter your command at the prompt below. Type 'help' to get assistance." | |
| while(True): | |
| cmd = raw_input("> ") | |
| args = cmd.split(' ') | |
| while args != None and len(args) > 0 and args[0] in self.cmds: | |
| cmd_func = getattr(self, args[0]) | |
| args = cmd_func(args) | |
| if args == None: | |
| print "You did not enter a command" | |
| elif len(args) == 0: | |
| print "You did not enter a command" | |
| elif args == self._nocmd: | |
| pass | |
| else: | |
| print "Unknown command '" + args[0] + "'" | |
| def help(self, args): | |
| for cmd in self.cmds: | |
| print cmd + " - " + self.cmds[cmd] | |
| return self._nocmd | |
| def rooms(self, args): | |
| print "Rooms available in this game are:" | |
| count = 0 | |
| for room in self._rooms: | |
| count += 1 | |
| print "%d. %s" % (count, room) | |
| return self._nocmd | |
| def room(self, args): | |
| if(len(args) > 1): | |
| room_name = args[1] | |
| if room_name in self._rooms: | |
| room_obj = self._rooms[room_name] | |
| self.current_room = room_name | |
| args = room_obj.enter() | |
| return args | |
| else: | |
| print "Sorry, we could not find a room by that name '%s'" % room_name | |
| else: | |
| #we need to provide hallway as a default room | |
| if self.current_room == None: | |
| print "You are in the hallway, and have not yet entered a room." | |
| else: | |
| print "You are in '%s'" % self.current_room | |
| return self._nocmd | |
| def quit(self, args): | |
| print "Thank you for playing with us, have a wonderful day !" | |
| exit(0) | |
| def has_cmd(self, the_cmd): | |
| if(the_cmd in self.cmds): | |
| return True | |
| else: | |
| return False | |
| # ----------------------------------------------------------------------------- | |
| class SpacedRepitition(object): | |
| def __init__(self, _game): | |
| self.game = _game | |
| # Add commands available in this room | |
| self.cmds = {} | |
| self.cmds["help"] = "Lists all the commands" | |
| self.cmds["topic"] = "Prints the current topic. If followed by an argument then the user will be switched to that topic" | |
| self.cmds["topics"] = "Prints the list of available topics" | |
| self.cmds["question"] = "Request to ask a question" | |
| import java_questions | |
| import version_control_questions | |
| import javascript_questions | |
| import linux_questions | |
| import mysql_questions | |
| import misc_questions | |
| import php_questions | |
| import vim_questions | |
| from question import Question | |
| self._topics = { | |
| "version_control":version_control_questions.version_control_questions, | |
| "java":java_questions.java_questions, | |
| "javascript":javascript_questions.javascript_questions, | |
| "linux":linux_questions.linux_questions, | |
| "mysql":mysql_questions.mysql_questions, | |
| "misc":misc_questions.misc_questions, | |
| "php":php_questions.php_questions, | |
| "vim":vim_questions.vim_questions | |
| } | |
| self._current_topic = None | |
| def enter(self): | |
| # TODO: The room name should come from a static property which should also be accessed from the Game class | |
| print "Welcome to the '%s' room" % "SpacedRepitition" | |
| while(True): | |
| cmd = raw_input("SpacedRepitition > ") | |
| args = cmd.split(' ') | |
| if(args[0] in self.cmds): | |
| cmd_func = getattr(self, args[0]) | |
| cmd_func(args) | |
| elif(self.game.has_cmd(args[0])): | |
| return args | |
| else: | |
| print "Unknown command '" + args[0] + "'" | |
| def topics(self, args): | |
| print "The following topics are available" | |
| count = 0 | |
| for topic in self._topics: | |
| count += 1 | |
| print "%d. %s" % (count, topic) | |
| def topic(self, args): | |
| if(len(args) > 1): | |
| topic_name = args[1] | |
| if topic_name in self._topics: | |
| self._current_topic = topic_name | |
| print "You will now be asked questions from the following topic %s." % self._current_topic | |
| print "Please use the 'question' command to request a question." | |
| else: | |
| print "Sorry, we could not find a topic by that name '%s'" % topic_name | |
| else: | |
| #we need to provide hallway as a default room | |
| if self._current_topic == None: | |
| print "You have not selected any topic." | |
| else: | |
| print "You will be asked questions from the following topic '%s'" % self._current_topic | |
| def question(self, args): | |
| if self._current_topic != None: | |
| current_topic = self._topics[self._current_topic] | |
| question_index = random.randint(0, len(self._topics[self._current_topic])-1) | |
| the_question = current_topic[question_index] | |
| print the_question._question + "\n" | |
| answer = raw_input("Please type your answer >") | |
| print "\nThe correct answer is printed on the line below. Please compare it with what you typed....." | |
| print the_question._answer | |
| print "\n\n" | |
| else: | |
| print "Please select a topic first." | |
| def help(self, args): | |
| print "Commands available from all rooms" | |
| self.game.help(args) | |
| print "\nCommands available in this room" | |
| for cmd in self.cmds: | |
| print "%s - %s" % (cmd, self.cmds[cmd]) | |
| if(__name__ == "__main__"): | |
| game = Game() | |
| game.start() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Question(object): | |
| def __init__(self, question, answer): | |
| self._question = question | |
| self._answer = answer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from question import Question | |
| # Add version control questions | |
| version_control_questions = [] | |
| question = "In Git, how can I find out all the commits done on HEAD but not pushed to origin ?" | |
| answer = "git log origin/master..HEAD" | |
| version_control_questions.append(Question(question, answer)) | |
| question = "In Git, how can I find out what is available on origin/master for me to fetch ?" | |
| answer = "git fetch origin/master" | |
| version_control_questions.append(Question(question, answer)) | |
| question = "In Git, how can I view all the files which were part of a commit ?" | |
| answer = "git log format=short --name-only \n See http://stackoverflow.com/questions/424071/how-do-i-list-all-the-files-for-a-commit-in-git" | |
| version_control_questions.append(Question(question, answer)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment