~yacinechaouche/+junk/BZR

« back to all changes in this revision

Viewing changes to PROG/HANG/hang.py

  • Committer: yacinechaouche at yahoo
  • Date: 2015-01-14 22:23:03 UTC
  • Revision ID: yacinechaouche@yahoo.com-20150114222303-6gbtqqxii717vyka
Ajout de CODE et PROD. Il faudra ensuite ajouter ce qu'il y avait dan TMP

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import random,sys
 
2
 
 
3
class Application:
 
4
    def __init__(self):
 
5
        """
 
6
        """
 
7
        self.max_tries_dict = {1:10,2:5,3:3}
 
8
        self.words = [word.strip() for word in open("/usr/share/dict/words").readlines()]
 
9
 
 
10
    def run(self):
 
11
        self.welcome_screen()
 
12
        self.difficulty = self.difficulty_menu()
 
13
        self.max_tries  = self.max_tries_dict[self.difficulty]
 
14
        stop_playing    = False
 
15
        while not stop_playing:
 
16
            stop_playing = self.enter_game()
 
17
            
 
18
 
 
19
    def enter_game(self):
 
20
        hang_word     = self.get_random_word()
 
21
        tries         = self.max_tries
 
22
        tried_letters = []
 
23
 
 
24
        while tries > 0 :
 
25
            print "".join(hang_word.pattern)
 
26
            letter = raw_input("? ")
 
27
            if letter in tried_letters:
 
28
                print "you already tried", letter
 
29
                continue
 
30
            else:
 
31
                tried_letters.append(letter)
 
32
 
 
33
            if letter and letter in hang_word.word:
 
34
                print "right"
 
35
                hang_word.fill(letter)
 
36
                if hang_word.filled() :
 
37
                    print "you got it :)"
 
38
                    print "".join(hang_word.pattern)
 
39
                    break
 
40
            else:
 
41
                tries -= 1
 
42
                print "tries left",tries," | ".join(tried_letters)
 
43
        if not hang_word.filled():
 
44
            print hang_word.word
 
45
 
 
46
        return raw_input("play again ? ") == "n"
 
47
 
 
48
    def welcome_screen(self):
 
49
        """
 
50
        """
 
51
        message = """ 
 
52
Hello.
 
53
"""
 
54
        print message
 
55
 
 
56
 
 
57
    def difficulty_menu(self):
 
58
        """
 
59
        """
 
60
        menu = """
 
61
Choose your difficulty :
 
62
1. Easy
 
63
2. Medium
 
64
3. Hard
 
65
"""
 
66
        ok = False
 
67
        while not ok :
 
68
            print menu
 
69
            level = sys.stdin.readline().strip()
 
70
            if level.isdigit() and 0 < int(level) < 4:
 
71
                level = int(level)
 
72
                ok = True
 
73
            else:
 
74
                print "wrong entry. Please choose one of 1, 2, or 3."
 
75
        return level
 
76
        
 
77
    def get_random_word(self):
 
78
        """
 
79
        """
 
80
        return HangWord(random.choice(self.words).lower())
 
81
        
 
82
 
 
83
 
 
84
    def exit(self):
 
85
        """
 
86
        """
 
87
        print "good bye ! looser..."
 
88
 
 
89
 
 
90
class HangWord:
 
91
 
 
92
    def __init__(self,word):
 
93
        self.word         = word
 
94
        self.pattern      = list("-"*len(self.word))
 
95
        self.pattern_dict = {}
 
96
        for index,letter in enumerate(word) :
 
97
            self.pattern_dict[letter] = self.pattern_dict.get(letter,[]) + [index]
 
98
 
 
99
    def fill(self,letter):
 
100
        for index in self.pattern_dict[letter]:
 
101
            self.pattern[index] = letter
 
102
 
 
103
    def filled(self):
 
104
        return self.word == "".join(self.pattern)
 
105
 
 
106
if __name__ == "__main__":
 
107
 
 
108
    app = Application()
 
109
    app.run()
 
110
    app.exit()