Visualizzazione dei risultati da 1 a 1 su 1
  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2014
    Messaggi
    17

    [Python] Gioco impiccato

    Ciao a tutti, sta sera non sapevo cosa fare e ho iniziato a scrivere un gioco (così per passare il tempo, da scrivere in una mezz'oretta).
    Il gioco è un impiccato, la cosa che volevo sapere è se il mio codice è abbastanza elegante o se si potrebbe migliorare
    codice:
    from random import randrange
    
    
    print("The Hangman -- by luc99a")
    
    
    running = True
    tentatives = 4
    
    #lo status GAME non serve, ma lo lascio comunque
    GAME = 0
    WIN = 1
    GAME_OVER = 2
    
    
    status = 0
    
    
    def random_word():
        #si le parole sono poche
        words = ["hello", "car", "pizza", "ornithorhynchus", "viking"]
        rand = randrange(len(words))
        return words[rand]
    
    
    def menu():
        print("Choose an option:")
        print("1) Buy letter")
        print("2) Guess word")
        choice = int(input(">"))
        while choice != 1 and choice != 2:
             print("Non valid choice, choose again")
             choice = int(input(">"))
        return choice
    
    
    def hint(word, buyed_letters):
        vowels = ["a", "e", "i", "o", "u"]
        #si so che questo qui sotto è orribile da vedere
        hint = [i if i in buyed_letters else "+" if i in vowels else "-" for i in word]    
        return hint
    
    
    def check_win(word, letters, guess):
        if guess == word:
            return True
        for i in word:
            if not i in letters:
                return False
        return True
    
    
    letters = []
    word = random_word()
    guess = ""
    
    
    while running:
        print(" ".join(hint(word, letters)))
        choice = menu()
        if choice == 1:
            print("Wich letter would you like to buy?")
            letter = input(">")
            if letter in word:
                letters.append(letter)
                if check_win(word, letters, guess):
                    running = False
                    status = WIN
            else:
              tentatives -= 1
              if tentatives == 0:
                  running = False
                  status = GAME_OVER
        elif choice == 2:
            print("Guess the world")
            guess = input(">")
            if check_win(word, letters, guess):
                running = False
                status = WIN
            else:
                running = False
                status = GAME_OVER
    
    
    if status == WIN:
        print("You win")
    elif status == GAME_OVER:
        print("Game Over")
    Grazie

    Ah scusate l'ot:
    chiedo scusa se rispondo raramente, ma quando sono al computer non ho tempo di rispondere, cercherò di essere più attivo in futuro

    Ho notato che ho sbagliato il titolo, ero distratto e ho scritto solo [Python] scusate
    Ultima modifica di luc99a; 12-08-2014 a 22:54

Tag per questa discussione

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.