[Python]Hang-man作成ゲーム-2(HangMan実装)

22159 ワード

前の記事では、基本的なclassと関数の宣言が始まりました.
今回はハンマンpyを実装して、ゲームが実行されているかどうかを見てみましょう.
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.ゲームの答えになる単語(String)
2.ユーザの現在の状態を保存するモニタ(List)
  • を例にmonitorをbool型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)です.実際には、最も重要な関数を作ってみてください.
    execute関数は、次の操作を行います.
    1.繰り返し条件:ゲーム終了時に条件が満たされるまで
    2.現在の状態を出力
    3.アルファベットを入力
    4.存在または存在しない.しゅつりょく
    そこで、次のように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 = '  ')
    入力Spell(self)はスペルを受け入れ、入力したスペルがword内にある場合、そのword内のインデックスに対応するモニタのすべての要素がTrueに変換されます.
    また、上記の操作が完了すると、答えが出力され、エラーが発生するとcount値が調整されます.
  • 1 1つの関数に2つの機能を追加するのは避けるべきですが、設計能力の制限のため、ここでは2つの機能を追加せざるを得ません(より良い方法があれば、伝言を残してください)
  •     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
    モニタ内のすべての要素がTrueの場合、isEnd(self)は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()
        '''
    正常に動作していることがわかります.

    完成したハンマン.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