基於python代碼實現棋牌游戲


模塊一完成后,需要完成的小作業《棋牌游戲11點》

游戲題目及要求如下:
1、生成一副撲克牌(自己設計撲克牌的結構,小王和大王可以分別用14、15表示 )

2、3個玩家(玩家也可以自己定義)
user_list = ["alex","武沛齊","李路飛"]

3、發牌規則
默認先給用戶發一張牌,其中 J、Q、K、小王、大王代表的值為0.5,其他就是則就是當前的牌面值。
用戶根據自己的情況判斷是否繼續要牌。
要,則再給他發一張。(可以一直要牌,但是如果自己手中的牌總和超過11點,你的牌就爆掉了(牌面變成0))
不要,則開始給下個玩家發牌。(沒有牌則則牌面默認是0)
如果用戶手中的所有牌相加大於11,則表示爆了,此人的分數為0,並且自動開始給下個人發牌。

4、最終計算並獲得每個玩家的分值,例如:
result = {
"alex":8,
"武沛齊":9,
"李路飛":0
}

代碼實現如下:

  ''''撲克牌11點游戲'''

  import random

  '''1、生成撲克牌'''
  # 自定義花色
  colour = ['黑桃', '紅桃', '方塊', '梅花']
  # 生成牌面
  total_poker = []
  for colour in colour:
      for number in range(1, 11):
          score = number
          data_poker = (colour, number, score)
          total_poker.append(data_poker)
  # print(total_poker)
  # 自定義特殊牌
  special_poker = [('J', 11, 0.5), ('Q', 12, 0.5), ('K', 13, 0.5), ('小王', 14, 0.5), ('大王', 15, 0.5)]

  # 將所有牌面合並
  total_poker = total_poker + special_poker
  print(total_poker)

  '''2、自定義三個玩家'''
  user_lists = []
  while len(user_lists) < 3:
      user_name = input('請輸入玩家姓名:').strip()
      user_lists.append(user_name)
      print(f'玩家已就位:{user_lists}')

  '''3.隨機發牌一次'''

  print('各單位請注意,現在游戲開始!!!'.center(50))
  result = {}
  for user_name in user_lists:
      index = random.randint(0, len(total_poker) - 1)
      print(f'{user_name}抽到的牌為{total_poker[index]}')
      col, num, sor = total_poker[index]
      result[user_name] = sor
      print(f'{user_name}你目前的分數為{sor}')
      total_poker.pop(index)  # 刪除已經分配的牌面
  # 剩下的牌面為
  print('剩下的牌面為{}'.format(total_poker))

  # 目前的結果為
  print(f'目前結果為:{result}')

  '''4.加牌階段'''
  print('各單位請注意,現在游戲進入加牌階段!!!'.center(50))

  while True:
      if len(total_poker) - 1 > 0:
          for user_name in user_lists:
              instructions = input(f'{user_name}你需要加牌嗎?(是/否):').strip()
              if instructions == '是':
                  index = random.randint(0, len(total_poker) - 1)
                  print(f'{user_name}抽到的牌為{total_poker[index]}')
                  col, num, sor = total_poker[index]
                  total_sore = result[user_name] + sor  # 將目前該玩家結果加入總結果里
                  result[user_name] = total_sore
                  print(f'{user_name}你目前的分數為{total_sore}')
                  total_poker.pop(index)  # 刪除已經分配的牌面
                  print(f'當前結果為:{result}')
                  print(f'剩余撲克牌數量為:{len(total_poker)},剩余撲克牌為:{total_poker}')
                  if result[user_name] > 11:  # 如果該玩家總分數大於11重置為0
                      result[user_name] = 0
                      print(f'{user_name}最新的分數為{result[user_name]}')
                      print(f'各個玩家最新結果為;{result}')
                      print(f'剩余撲克牌數量為:{len(total_poker)},剩余撲克牌為:{total_poker}')
              elif instructions == '否':
                  print(f"{user_name}不加牌!下一個!")
              else:
                  print(f"{user_name}輸入不合法的指令!默認不需要加牌!下一個!")
      else:
          print(f'各單位注意!!!游戲已經結束!謝謝各位玩家{user_lists[0]}、{user_lists[1]}、{user_lists[2]}參與'.center(50))
          break


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM