노는게 제일 좋습니다.

베이글 게임(세자리 숫자 맞추기) 본문

Python/기타 공부

베이글 게임(세자리 숫자 맞추기)

노는게 제일 좋습니다. 2016. 2. 4. 23:57

심심하면 가끔 하던 야구게임과 유사하다.


자리와 숫자 모두 맞으면 스트라이크, 숫자만 맞고 자리가 틀리면 볼..

이런식으로 하던 게임.


이 게임에서는 세 자리의 답을 제시하며, 스트라이크는 Fermi와, 볼은 Pico와 대응된다.


이정도면 파이썬 연습은 대강 되었으니 다음부터는 Pygame엔진을 사용하는걸로.




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
###########################################################################
import random
import sys
###########################################################################
def introDisplay():
    print('베이글(Bagle)게임에 오신 것을 환영합니다!')
    print('가이드가 필요하시면 "1"을, 넘기시려면 "2"를 입력해주세요.')
    while True:
        print(' >> 입력 : ',end='')
        temp = input()
        if temp == '1':
            gameGuide()
            break
        elif temp == '2':
            print('가이드를 생략합니다.',end='\n\n')
            break
        else :
            print('잘못 입력하셨습니다. 1 또는 2를 입력해주세요.')
            continue
        
    return 0
def gameGuide():
    print(' 게임 안내를 시작합니다. ',end='\n\n')
    aLinePrint('아무 키나 누르시면 한 줄씩 설명을 출력합니다.')
    aLinePrint('베이글(Bagle)은 세 자리 숫자를 맞추는 게임입니다.')
    aLinePrint('우선 게임이 시작되면 컴퓨터는 세 자리의 숫자를 정합니다.')
    aLinePrint('이 때 중복되는 숫자는 없습니다. (예)112,252와 같은 경우는 없습니다')
    aLinePrint('그 다음 당신은 예상되는 세 자리 숫자를 입력합니다. 각 자리는 0~9입니다.')
    print()
    aLinePrint('만약 숫자는 맞으나 자리가 다르면 "Pico"라고 합니다')
    aLinePrint('만약 숫자도 맞고 자리까지 맞으면 "Fermi" 라고 합니다.')
    aLinePrint('pico에도 fermi에도 해당되지 않아, 즉 숫자도 자리도 틀리면 "Bagle"이라고 합니다.')
    print()
    aLinePrint('예를 들며 설명을 해드리겠습니다.')
    aLinePrint('컴퓨터가 정한 숫자는 "123" 입니다.')
    
    guideSimulation(456'[ Bagle! ]''어떤 것도 맞지 않기때문에 Bagle이 출력됩니다.')
    guideSimulation(156'[ Fermi! ]' ,'첫번째 자리 "1"이 숫자도 맞고 자리도 맞기 때문에 Fermi가 하나 출력됩니다.' )
    guideSimulation(516'[ Pico! ]' ,'두번째 자리 "1"이 숫자만 맞고 자리가 틀리므로 Pico가 하나 출력됩니다.' )
    guideSimulation(125'[ Fermi! Fermi! ]' ,'첫 번째, 두 번재 자리의 "1", "2"가 숫자와 자리 모두 맞으므로 Fermi가 두 개 출력됩니다.')
    guideSimulation(123'[ 정답입니다! ]','정답을 맞추셨습니다.' )
    aLinePrint('지금까지 설명을 듣고, 정답을 맞추는 과정까지 따라오셨습니다. 수고하셨습니다.')
    aLinePrint('이제, 게임을 시작합니다!')
    print('\n\n\n')
    
def aLinePrint(string):
    print(string,end='                  ...... >> press Key')
    input()
def guideSimulation(answer, str1, str2):
    while True:
        print()
        print(str(answer)+'를 입력해보세요!')
        print('>> 입력 : ',end='')
        temp = input()
        if temp==str(answer):
            print(str1)
            print(str2)
            break
        else :
            print ('잘못 입력하셨습니다.')
            continue
###########################################################################
        
def makeNumber():
    numSource = list(range(0,10))
    number = []
    #첫 번째 자리
    temp = random.choice(list(range(1,10)))
    number.append( str(temp) )
    numSource.remove(temp)
    
    #두 번째, 세 번째 자리
    for i in range(0,2):
        #print(numSource)
        temp = random.choice(numSource)
        number.append(str(temp))
        numSource.remove(temp)
    number_forReturn = number[0+number[1+ number[2]
    #print('\t\tTEST\t'+number_forReturn)
    return number_forReturn
    
###########################################################################
def getPlayerAnswer(turnNum):
    print()
    print('Fermi : 숫자와 자리 모두 맞음 / Pico : 숫자만 맞고 자리 틀림 / Bagle : 모두 틀림')
    print('현재 "'+str(turnNum)+'"번째 차례입니다.')
    print('*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*')
    print('숫자를 입력해주세요')
    print('>> 입력 : ',end='')
    answer = input()
    return answer
###########################################################################
def checkAnswer(answer,rightNum):
    result = ''
    collect = [False, False, False]
    temp = []
    
    #각 자리수마다 상태 확인
    for i in range(0,3):
        for k in range(0,3):
            if answer[i]==rightNum[k]:
                if i==k :
                    result += 'Fermi!'+' '
                    collect[i]=True
                elif i != k :
                    result += 'Pico!'+' '
    #정답인지 확인
    for i in range(0len(collect)):
        if collect[i] == False:
            break
        elif i == len(collect)-1 :
            #정답인 경우 리턴값
            return True
    #정답이 아닌 경우 리턴        
    return result
                    
    
###########################################################################
def printResult(message):
    print('[결과] '+message, end='\n\n')
      
###########################################################################
def replayGuide():
    print('다시 플레이 하시겠습니까? (1.네   /  2.아니요)')
    print(' >> 입력 : ',end='')
    temp = input()
    while True:
          if temp == '1':
            print()
            print()
            print()
            print('새로운 게임을 시작합니다!')
            print()
            print()
            print()
            break
          elif temp == '2':
            print()
            print()
            print()
            print('즐거웠습니다! 잘가요!')
            print()
            print()
            print()
            sys.exit(0)
          else :
            print('잘못 입력하셨습니다. 1 또는 2를 입력해주세요')
            continue
    return True
###########################################################################
###########################################################################
###########################################################################
###########################################################################
 
 
while True:
    secretNumber = makeNumber()
    turnNum = 0
    introDisplay()
    
    while True: 
        turnNum+=1
        
        #플레이어의 입력 받기
        playerAnswer = getPlayerAnswer(turnNum)
        #정답과 적은 숫자 비교 / 결과 표
        result = checkAnswer(playerAnswer,secretNumber)
        if result == True:
            print('숫자를 맞추셨습니다! 축하합니다.', end='\n\n')
            if replayGuide()==True:
                break 
        else :
            print('[결과] '+result, end='\n\n')
 
 
cs


Comments