노는게 제일 좋습니다.

행맨(내 코딩 - list, for) 본문

Python/기타 공부

행맨(내 코딩 - list, for)

노는게 제일 좋습니다. 2016. 1. 29. 02:22

 

유명한 게임인 행맨을 만들어보았다.

 

책의 예제로 나와있는건데, 일부러 예제를 안보고 알고있는 지식과 검색만으로 코딩을 해보았다.

아마 책에 비하면 굉장히 괴랄한 코드일 것 같은데..

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import random
import time
import sys
 
 
 
def getWord():
    return wordList[random.randint(0,len(wordList)-1)]
 
wordList = ['mouse''laptop''tablet''computer'#문제에 나올 단어 지정
missingNum = 0 #단어를 틀린 횟수
missedLetters = '' #놓친 문자
selectedWord = getWord() #맞혀야 할 단어.
wordScreen = '_'*len(selectedWord) #단어를 맞히는 현황 표시
 
def printGraphic():
    #print out man.-------------------------------------------------
    print()
    print('-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-')
    print()
    print('H  A  N  G  M  A  N')
    print('     +------+')
    print('      l\tl')
 
    if missingNum >= 1:
        print('     O\tl')
        if missingNum == 1 :
            print('      \tl')
            print('      \tl')
        elif missingNum == 2 :
            print('      l\tl')
            print('      \tl')
        elif missingNum == 3 :
            print('    / l\tl')
            print('      \tl')
        elif missingNum == 4 :
            print('    / l\\tl')
            print('      \tl')
        elif missingNum ==5 :
            print('    / l\\tl')
            print('    /\tl')
        elif missingNum ==6 :
            print('    / l\\tl')
            print('    /  \\tl')
    if missingNum == 0 :
        print('      \tl')
        print('      \tl')
        print('      \tl')
    
    print(' \tl')
    print('=========')
 
    #print out game state ------------------------------------------
    print('Missed letters : '+missedLetters)
    for i in wordScreen:
        print(i , end=' ')
    print()
    print('Guess a letter.')
    print(' >> Your Answer : ',end='')
 
 
def checkCorrect(letter):
    correct = False
 
    global selectedWord
    global wordScreen
 
    k=0
    temp=0
    for i in selectedWord:
        if i==letter:
            correct = True
 
            #i가 selectedWord에서 몇 번째 문자인지 알아내기
            while k<len(selectedWord):
                if i == selectedWord[k]:
                    temp=k
                    k+=1
                    break
                k+=1
 
            #wordScreen 내용 바꾸기
            temp_before = wordScreen[0:temp]
            temp_after = wordScreen[temp+1:len(selectedWord)]
            
            wordScreen = temp_before + letter +  temp_after
 
    return correct
            
def checkOverlap(letter):
    overlap = False
 
    for i in wordScreen:
        if letter == i:
            overlap = True
            break;
        
    for i in missedLetters:
        if letter == i:
            overlap = True
            break
 
    return overlap
 
def replayGuide():
    
    print('Do you want to play again? (yes / no)')
    while True:
        playAgain = input()
        if playAgain == 'yes':
            print()
            print()
            print()
            print()
            print()
            print('-=-=-=-=-=-=-=-=-=-=-=-=-')
            print(          'New game start')
            print('-=-=-=-=-=-=-=-=-=-=-=-=-')
            print()
            print()
            print()
            print()
            print()
            return playAgain
            break;
        elif playAgain=='no':
            print('Okay. Bye.')
            sys.exit(0)
        else :
            print('please type yes or no')
            continue
            
 
 
####################################################################
####################################################################
####################################################################
##
        #                            이 아래에서 위 함수들을 사용해 프로그램을 돌림
 
temp = 0
 
while True:
    temp = 0
    printGraphic()
    letter = input()
    letter = letter.lower()
    
    #철자가 하나인지 확인
    if len(letter) > 1:
        print('You should type just a letter. ( 1 letter )')
        continue
 
    #철자 중복여부 확인
    if checkOverlap(letter) == True :
        print('You have already guessed that letter. Choose again.')
        continue
 
    #주어진 단어안에 입력한 철자가 있는지 확인후 wordScreen 내용 바꾸기
    correct = checkCorrect(letter)
    if correct == False :
        missingNum+=1
        missedLetters += letter
 
    #정답을 모두 맞추었는지 확인
    for i in wordScreen:
        if i=='_':
            break
        
        #정답을 모두 맞춘경우
        if temp==len(wordScreen)-1 :    
            printGraphic()
            print()
            print()
            print('====================================================')
            print('You win!')
            if replayGuide()=='yes':
                missingNum = 0
                missedLetters = ''
                selectedWord = getWord()
                wordScreen = '_'*len(selectedWord)
                temp = 0
                break
            
        temp+=1
 
    #6회 이상 틀렸는지 확인 후 게임오버처리.
    if missingNum == 6:
        printGraphic()
        print()
        print()
        print('====================================================')
        print('Oh no... The man passed away.')
        print('You lose')
        if replayGuide()=='yes':
            missingNum = 0
            missedLetters = ''
            selectedWord = getWord()
            wordScreen = '_'*len(selectedWord)
            temp = 0
            continue
        
        
 
 
 
 
 
 
 
 
 
 
    
 
cs

 

list : 다른 언어의 배열 처럼 사용할 수 있음. array와 같고 이름만 다른지, list가 자료구조할 때 그 list인지는 아직 모름

for : 신기하게 생김 https://wikidocs.net/22

Comments