見出し画像

Python(ゲーム作成)

今回のnoteはpythonでゲームを作成したのでそれを共有したいと思います。

作成したゲームはハノイ塔ゲームになります。詳細は下記のURLを確認してください。

下記がコードになります。
是非見て実際にやってみてください。

import random

deck = [12345678910111213] * 4


def deal():
   hand = []
   for i in range(2):
       random.shuffle(deck)
       card = deck.pop()
       if card == 11:
           card = "J"
       if card == 12:
           card = "Q"
       if card == 13:
           card = "K"
       if card == 1:
           card = "A"
       hand.append(card)
   return hand


def hit(hand):
   random.shuffle(deck)
   card = deck.pop()
   if card == 11:
       card = "J"
   if card == 12:
       card = "Q"
   if card == 13:
       card = "K"
   if card == 1:
       card = "A"
   hand.append(card)
   return hand


def total(hand):
   score = 0
   for card in hand:
       if card == "J" or card == "Q" or card == "K":
           score = score + 10
       elif card == "A":
           if score >= 11:
               score = score + 1
           else:
               score += 11
       else:
           score += card
   return score


def play_again():
   again = input("もう1度プレイしますか? (Y/N): ")
   if again == "y" or again == "Y":
       # game()
       return
   else:
       print("お疲れ様でした!")
       exit()


def result(dealer_hand, player_hand):
   if total(player_hand) > total(dealer_hand):
       print(
           f"\nディーラーの合計は {total(dealer_hand)} あなたの合計は {total(player_hand)} です。\033[32mYOU WIN!\033[0m")
   elif total(dealer_hand) > total(player_hand):
       print(
           f"\nディーラーの合計は {total(dealer_hand)} あなたの合計は {total(player_hand)} です。\033[91mYOU LOSE...\033[0m")


def game():
   dealer_hand = deal()
   player_hand = deal()
   print(f"ディーラーのカードは {dealer_hand[0]} です。")
   print(f"プレイヤーのカードは {player_hand} 合計が {total(player_hand)} です。")

   choice = 0

   while choice != quit:
       choice = input("ヒットしますか? スタンドしますか? (HIT/STAND): ").lower()
       if choice == "hit":
           hit(player_hand)
           print(
               f"\nあなたに {player_hand[-1]} が配られ、カードは {player_hand} 合計は {total(player_hand)} です。")
           if total(player_hand) > 21:
               print("あなたは 21 を超えてしまいました。\033[91mYOU LOSE...\033[0m")
               choice = quit

       elif choice == "stand":
           print(
               f"\nディーラーの2枚めのカードは {dealer_hand[1]} 合計は {total(dealer_hand)} です。")
           while total(dealer_hand) < 17:
               hit(dealer_hand)
               print(
                   f"ディーラーに {dealer_hand[-1]} が配られ、カードは {dealer_hand} 合計は {total(dealer_hand)} です。")
               if total(dealer_hand) > 21:
                   print("ディーラーは 21 を超えてしまいました。\033[32mYOU WIN!\033[0m")
                   choice = quit

           if total(dealer_hand) <= 21:
               result(dealer_hand, player_hand)
               choice = quit
game()

以上になります。
じゃあまたね!!

この記事が参加している募集

#自己紹介

227,979件

#スキしてみて

523,651件

この記事が気に入ったらサポートをしてみませんか?