Malo mi dosadno na ovom raspustu pa sam prekjuce krenuo da ucim python tokom dana. Citao sam osnovne stvari na http://docs.python.org/tutorial/ i za prvi "projekat" sam odlucio da napravim vjesala. Profesionalna deformacija mi je da mislim na engleskom dok programiram pa sam sve komentare pisao na engleskom
Mozda ce nekom koristiti a i meni je bilo lijepo napraviti nesto, za pocetak sam zadovoljan . 173 linija koda.
#!/usr/bin/env python
#A simple Python hangman v.0.1
from random import randint #import library for random number generation
#Hangman generation function
def printhang(ntries):
if tries == 1:
print """
|
|
|
|
"""
elif tries == 2:
print """
___
|
|
|
|
"""
elif tries == 3:
print """
___
| O
|
|
|
"""
elif tries == 4:
print """
___
| O
| |
|
|
"""
elif tries == 5:
print """
___
| O
| \|/
|
|
"""
elif tries == 6:
print """
___
| O
| \|/
| / \
|
"""
elif tries == 7:
print """
___
| |
| O
| \|/
| / \
"""
print """
Welcome to Hangmanpy
___
| O
| \|/
| / \
The original python text based hangman game
By Nenad Lukic
To begin, choose your game type
1 - Movies
2 - Actors
3 - Actresses
4 - Animals
5 - Cities
"""
gtype = int(raw_input("Enter game type (1,2,3,4,5) :"))
if gtype == 1:
print """You have chosen Movies"""
gstring = ["Iron Man", "American Pie", "Titanic"]
elif gtype == 2:
print """You have chosen Actors"""
gstring = ["Jim Carey"]
elif gtype == 3:
print """You have chosen Actresses"""
gstring = ["Sandra Bullock", "Natalie Portman", "Jennifer Aniston"]
elif gtype == 4:
print """You have chosen Animals"""
gstring = ["Giraffe", "Elephant", "Hippopotamus", "Wolf"]
elif gtype == 5:
print """You have chosen Cities"""
gstring = ["Beijing", "London", "Belgrade", "Hong Kong", "Moscow"]
else:
print "Invalid game type."
randnum = randint(0,len(gstring)-1) #Get a random number
chosen_string = gstring[randnum] #Choose a string
tries = 0 #Number of tries taken by player
maxtries = 7 #Maximum number of tries allowed
lenc = len(chosen_string) #Length of the chosen string
mlenc = 0 #Minus the spaces
noguessed = 0 #Number of guessed letters
showstr = "" #String to show the letters
#Generate the string with underscores and spaces, while counting the number of spaces
for x in range(0,lenc):
if chosen_string[x]== " ":
mlenc = mlenc+1
showstr = showstr+" "
else:
showstr = showstr+"_"
lenw = lenc-mlenc #How many letters does the user need to guess
triedl = [] #Array that will contain the letters the user tried to guess
while tries<maxtries:
usedl = []
guessed = 0 #Variable that we'll use to check if the user succesfully guessed a letter
triedit = 0 #Variable that we'll use to see if the user used a letter which he used previously
gtry = raw_input("Input your letter: ")
for x in range (0, len(gtry)):
if triedl.count(gtry[x].lower()):
triedit = 1 #The user already used that letter(s)
if triedit != 1:
for x in range (0, len(gtry)):
triedl.append(gtry[x].lower()) #Get that letter into the triedl array
if len(gtry)>1:
for x in range(0,len(gtry)):
for y in range(0,lenc):
if gtry[x] != " ":
if gtry[x].lower() == chosen_string[y].lower():
if usedl.count(gtry[x].lower()):
pass
else:
usedl.append(gtry[x].lower())
#print "Guessed " + gtry[x] used for debugging
guessed = 1;
noguessed = noguessed + 1
newshowstr = showstr[:y]+chosen_string[y]+showstr[y+1:]
showstr = newshowstr
else:
for x in range(0,lenc):
if gtry.lower() == chosen_string[x].lower():
if gtry != " ":
#print "Guessed " + gtry used for debugging
guessed = 1
noguessed = noguessed + 1
newshowstr = showstr[:x]+chosen_string[x]+showstr[x+1:]
showstr = newshowstr
if guessed == 0:
tries = tries+1
print "You didn't guess anything this time. Number of tries: " + str(tries)
printhang(tries)
else:
print showstr
if noguessed >= lenw:
print "You have succesfully completed the hangman, you have made " + str(tries) + " errors"
break
if tries == maxtries:
print "Sorry but you failed, better luck next time"
else:
print "You have already tried that letter"
Sledeca stvar koju cu da dodam jeste citanje podataka iz direktorijuma i upisivanje istih u izbor tipa igre.
edit: Dodao 5 linija koda koje su sredile jedan nacin varanja Morao sam da ispravim
|