#! /usr/bin/python2.2 #! /usr/bin/env python # -*- coding: iso-8859-1 -*- """ gjettTall.py - A number guessing program. Originally written by Axel Bojer in august 2003 Last change: 5.9.2004 Copyright (C) 2003 Axel Bojer This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ---------------------Some general explanations------------------- Description of the variables: 'Blue/Green*... osv og 'ColourEnd': start- and ending strings for colours 'stjerneX' the (blinking) stars in different colours. 'Lines' to small, green lines before/after correct answers. 'Frame' only used in the logo. 'Indent' indents messages (and the logoe). 'IndentG/indentB' indents and paints green/blue. 'Start' is Yes or No. The actual letter used is language dependent 'Count1' counts how many times the user used the program currently in a row 'RandomNumber' gives a random number between 1 and 10. 'Correct' og 'Tcerroc' comes first and last in the last program message. 'Messagexx' and 'Congratulations' are different evaluations. Following variables must be remade for each round: 'Guess' has to be bigger than the biggest poosible guess (10*10); it will be changed when the user makes his/her guess If 'Guess' would not be regenerated for each round, then a result could 'automaticically' be right. 'CountGuesses' counts how many times the user has guessed. 'TooHigh' and 'TooLow' counts if you have guessed too high or too low several times in a row. 'TotalTooHigh' and 'TotalTooLow' counts how many times the guesses were too high or too low. """ #---------------------------Variables---------------------------- #General Count1 = 1 # Enable input from the shell import os, sys # Generate random number import random RandomNumber = random.randint (1,10) if sys.platform == "win32" : # Colour-less formatting for Windows. Blue = Green = PaleGreen = Red = Yellow = ColourEnd = "" Lines = "--" StarB = StarB2 = StarG = StarY = StarR = "*" LogoColour = "" AA = "aa" else: # Colour formating with linux codes. Blue = "" Green = "" PaleGreen = "" Red = "" Yellow = "" ColourEnd = "" # BlueWhite ="" # YellowBrown ="" # Lilac = "" # RedBrown ="" # Pink = "" # Black ="" # Stars and stripes Lines = Green + "--" + ColourEnd StarB = "*" + ColourEnd StarB2 = "*" + ColourEnd StarG = "*" + ColourEnd StarY = "*" + ColourEnd StarR = "*" + ColourEnd LogoColour = "" AA = "å" # Formating for messages Indent = " " * 3 IndentG = Indent + Green IndentB = Indent + Blue Correct = IndentG + Lines + StarB2 + Green + " " Tcerroc = StarB2 + Lines + ColourEnd # Logo only SpaceInLogo = LogoColour + " " + ColourEnd #--------------------------Program core-------------------------- # Take input from the shell # input = sys.stdin # output = sys.stdout # if sys.argv [1] == "-v" : # print Indent + "'Gjett tall' version 1.1., september 2004." # print Indent + "Copyright: Axel Bojer. Released under the 'GNU GPL License'." # sys.exit() # if sys.argv [1] == "-h" or "--help": # print: # """ # Usage: gjettTall1.1.py [OPTION]... # A guessing program, just for fun :-) # -nb, --norwegian Use norwegian strings without question. # -de, --german Use german strings without question. # -en, --english Use english strings without question. # -lt, --lithuanian Use lithuanian strings without question. # -v Show version number and licensing information. # -h or --help Display this help. # """ # sys.exit() # elif sys.argv [1] == "-nb" or "--norwegian" : # Language = 1 # elif sys.argv [1] == "-de" or "--german" : # Language = 2 # elif sys.argv [1] == "-en" or "--english" : # Language = 3 # elif sys.argv [1] == "-lt" or "--lithuanian" : # Language = 4 # else : # Language = 0 # Get the right language file Languages = 4 # (The number of different language-files) Language = 0 try : if Language == 0 : print print IndentG + "1 = Norwegian bokm" + AA + "l (nb)" + ColourEnd, print IndentG + "2 = German (de)" + ColourEnd, print IndentG + "3 = English (en)" + ColourEnd, print IndentG + "4 = Lithuanian (lt)" + ColourEnd print print IndentB + "From this list, choose the language you prefere to use (1 - %s): " % Languages + ColourEnd, Language = input () if Language == 1 : execfile ("./gjettTall_strings_nb.py"); LogoLength = len(Welcome) elif Language == 2 : execfile ("./gjettTall_strings_de.py"); LogoLength = len(Welcome) elif Language == 3 : execfile ("./gjettTall_strings_en.py"); LogoLength = len(Welcome) elif Language == 4 : execfile ("./gjettTall_strings_lt.py"); LogoLength = 39 else : print IndentB + "Choose a number between 1 and %s: " % Languages + ColourEnd, Language = input () except KeyboardInterrupt : print import sys sys.exit() # Introduction/logo # 'LogoLength' is The number of letters and spaces in the logomessage (named 'Welcome') Frame = " " *5 + SpaceInLogo + "%s-%s" % (LogoColour,ColourEnd) * (LogoLength+14) + SpaceInLogo print LogoColour + ColourEnd print Frame print " " * 5 + Yellow + "| " +StarG+StarY+StarR+ColourEnd+Yellow+Indent+ Welcome +Indent+StarR+StarY+StarG+Yellow+ " |" + ColourEnd print Frame print print IndentG + Welcome2 + ColourEnd print IndentG + Welcome3 + ColourEnd print print IndentG + Welcome4 % (Red,ColourEnd,Green) + ColourEnd print IndentG + Welcome5 % (Red,ColourEnd,Green) + ColourEnd print Start = Yes # Exit gracfully on keyboard interrupts try : # Main loop (the whole program). Runs until the user selects 'No' :-) # The strings for 'No/Yes' are language spesific. while Start == Yes : Guess = 101 CountGuesses = 0 TooHigh = 0 TotalTooHigh = 0 TooLow = 0 TotalTooLow = 0 # Ask the user for a number. To be stored in the variabel 'Number' print Blue + " " + "-" *20 + " %s. %s " % (Count1,Round) + "-" *20 + ColourEnd if Count1 == 1 : print InputNumber1, UsersNumber = input () else : print InputNumber2, UsersNumber = input () print Number = UsersNumber * RandomNumber # Prompt the user for a number until the correct answer is given while Guess != Number : # On the first guess if CountGuesses == 0 : if 0 < UsersNumber <= 10 : print GuessSmall, Guess = input () CountGuesses = CountGuesses + 1 elif UsersNumber >= 10 : print GuessHigh, Guess = input () CountGuesses = CountGuesses + 1 elif UsersNumber < 0 : print GuessNegative, Guess = input () CountGuesses = CountGuesses + 1 elif UsersNumber == 0 : print GuessZero, Guess = input () CountGuesses = CountGuesses + 1 if Guess > Number : print Indent + MessageH0 TooHigh = TooHigh + 1 TotalTooHigh = TotalTooHigh + 1 elif Guess < Number : print Indent + MessageL0 TooLow = TooLow + 1 TotalTooLow = TotalTooLow + 1 print # Do this by all guesses except for the first else : print TryAgain, Guess = input () CountGuesses = CountGuesses + 1 # If the number is too high ... if Guess > Number : # If a former guess also was too low, # but was followed by a too high one, ignore it. TooLow = 0 # Check how many times in a row the guess was too high. if TooHigh == 0 : print Indent + MessageH1 elif TooHigh == 1 : print Indent + MessageH2 else : print Indent + MessageH3 TooHigh = TooHigh + 1 TotalTooHigh = TotalTooHigh +1 print # And if the guess was too low... elif Guess < Number : # If a former guess also was too high, # but was followed by a too low one, ignore it. TooHigh = 0 # Check how many times in a row the guess was too high. if TooLow == 0 : print Indent + MessageL1 elif TooLow == 1 : print Indent + MessageL2 else : print Indent + MessageL3 TooLow = TooLow + 1 TotalTooLow = TotalTooLow + 1 print # If the guess is neighter too high nor too low, proceed... else : print # Congratulate the user for his correct answer # and tell how many times he/she guessed. if CountGuesses == 1 : print Correct + CorrectSi % Number + Tcerroc else : print Correct + CorrectPl % (Number,CountGuesses) + Tcerroc # Give some respons on the users performance. if Guess == Number : if CountGuesses == 1 : print IndentG + Excellent + ColourEnd elif CountGuesses < 4 : print IndentG + VeryGood + ColourEnd elif CountGuesses < 6 : print IndentG + Good + ColourEnd elif CountGuesses < 8 : print IndentG + Nice + ColourEnd else : print IndentG + Hmm + ColourEnd # Make correct use of plural wenn told the result. # Show part one: too high if TotalTooHigh == 0 and TotalTooLow == 0 : pass elif TotalTooHigh == 0 : print Indent + IndentG + ResultH0, elif TotalTooHigh == 1 : print Indent + IndentG + ResultHsi, elif TotalTooHigh != 1 : print Indent + IndentG + ResultHpl % TotalTooHigh, else : pass # Make correct use of plural wenn told the result. # Show part two: too low if TotalTooHigh == 0 and TotalTooLow == 0 : pass elif TotalTooLow == 0 : print ResultL0 + ColourEnd elif TotalTooLow == 1 : print ResultLsi + ColourEnd elif TotalTooLow != 1 : print ResultLpl % TotalTooLow + ColourEnd else : pass # The second big question print print Question2, Guess2 = input () Count1 = Count1 + 1 # Check if the answer to question nr. two was right. if Guess2 == RandomNumber or Guess2 == UsersNumber: print print Correct + Congratulations + " %s x %s = %s." % (UsersNumber,RandomNumber,Number) + Tcerroc else : print print Correct + Sorry + Tcerroc print # Show some tips, but only after the first round. if Count1 == 2 : print PaleGreen + Indent + Tips1 + ColourEnd print PaleGreen + Indent + Tips2 + ColourEnd print # Take either a new round with another random number or quit. ShortcutYes = Red + Yes + ColourEnd ShortcutNo = Red + No + ColourEnd print Again % (ShortcutYes,ShortcutNo), Start = raw_input () # If the answer to question two was right, then generate a new random number. # If not, then reuse the same number. if Guess2 == RandomNumber or UsersNumber : import random RandomNumber = random.randint (1,10) else: pass # Give a goodbye greeting or an empty line. if Start == No : print print IndentB + ThankYou + ColourEnd print else : print except KeyboardInterrupt : print print print IndentB + Interrupt + ColourEnd print IndentB + ThankYou + ColourEnd print # Thats it! #------------------------------The End---------------------------