Sage Math Adventure: The Legendary Rainbow Crystal

The world you live in has been taken over by monsters. But these aren’t just your typical monsters. These are math monsters that will eat anyone who can’t answer their math questions correctly. However, there is one way to destroy them.

Legends have been passed down through the ages of a legendary rainbow crystal that can defeat all monsters. But no one has ever actually seen the legendary rainbow crystal so it is now believed to be a myth. Recently, however, an archaeologist in Asia found a stone tablet containing a map to the rainbow crystal. By random happenstance, the tablet has come into your possession.

Try to find the correct path to the rainbow crystal. There is only one correct way. If you take a wrong turn you will run into a monster willing to spare you if you correctly answer his math question. Get that question right and you can go back and take the correct path. However, if you get the question wrong you lose the game and the monster will eat you for dinner.

{{{id=1| %hide %sage import random # MAKE IT SO WE CAN TAKE OVER NOTEBOOK -- our own mode class JS(object): def eval(self, input, globals, locals): return game(input, globals, locals) jsmath = JS() # THE STATE # states: asking for name ('name'); playing game ('play') state = 'name' name = '' location = 0 answer = 0 def game(input, globals, locals): global state, location, name if state == 'name': input = input.strip() if len(input) == 0: print "Please enter your name" return "" else: name = input state = 'play' positionOne(name) if location == 1: choiceOne(input) elif location == 2: choiceTwo(input) elif location == 3: choiceThree(input) elif location == 4: choiceFour(input) elif location == 5: choiceFive(input) elif location == -1: if checkAnswer(input): positionTwo() elif location == -2: if checkAnswer(input): positionThree() elif location == -3: if checkAnswer(input): positionFour() elif location == -4: if checkAnswer(input): positionFive() elif location == -5: if checkAnswer(input): win() return '' def checkAnswer(input): if input == answer: print "The monster says 'You got it right...luckily. You won't next time though!' and with a poof the monster disappears. You decide to head back and go the other way instead." print "" return true else: lose() return false def randomPath(pathOne, pathTwo): path = random.randint(1,2) if path == 1: return pathOne else: return pathTwo def positionOne(name): global location print "Hi %s. You are now at the beginning of your journey. You are currently standing at the edge of a forest. The stone tablet you have says you must take the path through the forest until you come out the other side. You look up and see that there are 2 paths through the forest: a gravel path that seems to have been man-made and a dirt path that looks more natural. Which path would you like to take?"%name location = 1 def choiceOne(input): global location, answer path = randomPath('gravel', 'dirt') if input != 'gravel' and input != 'dirt': print "Enter 'gravel' or 'dirt' to choose a path." elif input == path: positionTwo() elif input != path: x = random.randint(50, 100) y = random.randint(50, 100) print "As you walk along the path a monster jumps out from behind a tree and asks you: 'What is the product of %s and %s?'"%(x,y) print "Enter your answer quickly!" answer = str(x*y) location = -1 def positionTwo(): global location print "When you finally emerge from the forest you see a river in front of you that you must cross. There is a creaky old bridge and farther away is another bridge that looks sturdier and new. It is so foggy here that you can't see all the way across the bridges. You can choose to take the old bridge or the new bridge. What do you want to do?" print "Enter 'old' or 'new' to choose a bridge." location = 2 def choiceTwo(input): global location, answer path = randomPath('old', 'new') if input != 'old' and input != 'new': print "Enter 'old' or 'new' to choose a bridge." elif input == path: positionThree() elif input != path: x = random.randint(2, 6) y = random.randint(3, 9) print "You walk along the bridge and about halfway across a monster emerges from the fog and asks you: 'What is %s to the %s power?'"%(x,y) answer = str(x^y) print "Enter your answer quickly!" location = -2 def positionThree(): global location print "After you have crossed the river you see a cave nearby that you must walk through. You enter the cave and turn on the flashlight you luckily thought to bring with you. After walking for almost fifteen minutes the tunnel in the cave splits into two directions. You look at the stone tablet for help, but it does not say anything about there being two tunnels in the cave. Which path do you want to take?" print "Enter 'left' to take the left path or 'right' to take the right path." location = 3 def choiceThree(input): global location, answer path = randomPath('left', 'right') if input != 'right' and input != 'left': print "Enter 'left' to take the left path or 'right' to take the right path." elif input == path: positionFour() elif input != path: x = random.randint(5, 10) print "As you walk along the tunnel a monster appears in the way of the path and asks you: 'What is %s factorial?'"%x print "Enter your answer quickly!" answer = str(factorial(x)) location = -3 def positionFour(): global location print "You finally emerge from the cave to see a large hill. You are too tired to climb over it so you decide to go around it. You can either go around the west side of the hill or the east side. What do you want to do?" print "Enter 'west' or 'east' to choose a way around the hill." location = 4 def choiceFour(input): global location, answer path = randomPath('west', 'east') if input != 'west' and input != 'east': print "Enter 'west' or 'east' to choose a path." elif input == path: positionFive() elif input != path: x = random.randint(51, 101) print "As you walk around the hill a monster jumps out from behind a tree and asks you: 'Is %s a prime number?'"%x print "Enter 'yes' or 'no' quickly!" if is_prime(x): answer = 'yes' else: answer = 'no' location = -4 def positionFive(): global location print "You come across ancient ruins. From the stone tablet it is clear that the rainbow crystal is inside the ruins. You see a tunnel entrance in the wall that may lead inside. You look around and see that there is another tunnel entrance on the back wall of the ruins. Do you want to enter the tunnel in the front or the tunnel you found in the back?" print "Enter 'front' or 'back' to choose a tunnel." location = 5 def choiceFive(input): global location, answer path = randomPath('front', 'back') if input != 'front' and input != 'back': print "Enter 'front' or 'back' to choose a tunnel." elif input == path: win() elif input != path: A = matrix(5) for i in range(5): for j in range(5): A[i,j]=randint(1,9) print "As you walk through the tunnel a monster appears and asks you: 'What is the rank of this matrix?'" print A print "Enter your answer quickly!" answer = str(A.rank()) location = -5 def win(): print "As you emerge from the tunnel you enter a giant circular room. You see a small box on a raised pillar in front of you. You approach and slowly open the box. Once the box is fully open you see the rainbow crystal inside begin to rise into the air and all of sudden a blinding white light is emitted from it. You see gold sparks flowing out of the crystal and through the walls of the ruins. You realize that it is using the legendary power within it to destroy all the monsters that roam the planet. Eventually the light fades and the rainbow crystal floats back into the box. As soon as you close the box you are engulfed in darkness and the next thing you know you are back in your room at your house. You have saved the world from the monsters." print "" print "Congratulations. You have won the game!" reset() def lose(): print "The monster says 'WRONG! Now I'm going to eat you!' and he picks you up before you can even try to run." print "" print "A monster has eaten you and you have lost the game. The world's only hope is now gone." reset() def reset(): global state, name, location, answer state = 'name' name = '' location = 0 answer = 0 /// }}}