지난 글에서 기본적인 class와 함수의 선언이 시작되었다.
이번에는 HangMan.py을 구현해서 game이 실행되는지를 확인해보자
from GameImpl import GameImpl
from User import User
class HangMan(GameImpl):
def __init__(self, user : User):
pass
def getUserName(self):
return self.user.getUserName()
def execute(self):
pass
def gameOver(self):
pass
HangMan은 임의의 단어 하나를 사용자가 맞추는 게임이다.
게임에 필요한 요소들은 다음과 같다.
1.Game의 정답이될 word (String)
2.user의 현재 상황을 저장할 monitor (List)
3.Rank에서 사용될 score, 다만 나는 활용도를 생각해 count(int)로 선언했다.
def __init__(self, user : User):
self.user = user
self.word = "velog"
self.monitor = [False for i in range (len(self.word))]
self.count = 0
다음은 execute(self)이다. 사실상 가장 중요한 함수하고 봐도 좋다.
excute함수는 다음과 같이 실행된다.
1.반복조건 : 게임이 끝날조건이 충족될때까지
2.현재 상황 출력
3.알파벳 입력
4.존재합니다 or 존재하지 않습니다. 출력
따라서 나는 다음과 같이 execute를 작성했다.
def execute(self):
while self.isEnd() == False:
self.printMonitor()
self.inputSpell()
사용할 함수들을 선언
def isEnd(self):
pass
def printMonitor(self):
pass
def inputSpell(self):
pass
선언된 함수들을 만들어 주자
printMonitor(self)는 monitor배열을 참고하며, 각 배열의 요소가 True일때는 word의 index를 출력라고 아닐 경우에는 ' _ '를 출력한다. 또한 줄바꿈을 고려하여 print(end = ' ')를 설정해준다.
def printMonitor(self):
for index in range(len(self.monitor)):
if self.monitor[index] == True:
print(self.word[index], end = ' ')
else:
print('_', end = ' ')
inputSpell(self) 는 spelling(char)을 하나 입력 받으며, 입력된 spelling이 word내에 존재할 경우 해당 word내의 index들에 해당하는 모든 monitor의 요소들을 True로 바꿔준다.
또한 위의 작업이 완료 되었을 경우 정답의 여부를 출력해주며, 틀렸을 경우에는 count값을 조정해준다.
def inputSpelling(self):
spelling = input("input the alphabet : ")
is_there = False
for index in range(len(self.word)):
if spelling[0] == self.word[index]:
self.monitor[index] = True;
is_there = True
if is_there:
print("존재합니다\n\n")
else:
print("존재하지 않습니다\n\n")
self.count += 1
isEnd(self)는 monitor의 모든 요소가 True라면 True를 반환해준다.
내가 monitor의 배열 요소를 bool로 결정한 이유이다.
def isEnd(self):
if all(self.monitoring):
return True
else:
return False
gameOver(self)는 후에 수정이 좀 더 필요하지만 우선은 마지막으로 단어를 추가해준다.
def gameOver(self):
print("단어가 완성되었습니다.")
print("---", self.word ,"---")
이로써 기본적인 게임의 메커니즘 설계는 끝났다.
Main.py를 일시적으로 수정하고 실행해보자
from HangMan import HangMan
from Rank import Rank
from User import User
if __name__ == "__main__":
ranking_path ="...Python_Hangman/Rank.txt"
#rank = Rank(ranking_path)
user = User()
#user.setName()
game = HangMan(user)
game.execute()
game.gameOver()
'''
rank.addUser(user)
rank.sort()
rank.view()
user.view()
'''
정상적으로 동작하는 것을 볼 수 있다.
(임시)완성된 HangMan.py 코드다.
from GameImpl import GameImpl
from User import User
class HangMan(GameImpl):
def __init__(self, user : User):
self.user = user
self.word = "velog"
self.monitor = [False for i in range (len(self.word))]
self.count = 0
def getUserName(self):
return self.user.getUserName()
def execute(self):
while self.isEnd() == False:
self.printMonitor()
print()
self.inputSpelling()
def gameOver(self):
print("단어가 완성되었습니다.")
print("---", self.word ,"---")
self.user
def isEnd(self):
if all(self.monitor):
return True
else:
return False
def printMonitor(self):
for index in range(len(self.monitor)):
if self.monitor[index] == True:
print(self.word[index], end = ' ')
else:
print('_', end = ' ')
def inputSpelling(self):
spelling = input("input the alphabet : ")
is_there = False
for index in range(len(self.word)):
if spelling[0] == self.word[index]:
self.monitor[index] = True;
is_there = True
if is_there:
print("존재합니다\n\n")
else:
print("존재하지 않습니다\n\n")
self.count += 1