python如何实现四人制扑克牌游戏-创新互联

这篇文章主要为大家展示了python如何实现四人制扑克牌游戏,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。

创新互联是专业的门头沟网站建设公司,门头沟接单;提供网站制作、做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行门头沟网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

题目:

设计一个简单的四人制扑克牌游戏,能够完成以下功能:

1. 洗牌
2. 发牌
3.自定义规则,在每轮单张出牌时,判定赢家
4.自定义规则,判定最终的赢家

规则简化版:

仅能出单张牌,且出牌时,每个人出的是自己手中牌中刚好能压过上家的最小牌,最先出完的为赢家

import random
from random import choice
flower = ['\u2660','\u2663','\u2665','\u2666']
pai = ['3','4','5','6','7','8','9','10','J','Q','K','A','2']
list = []
list0 = []#储存发的牌
list1 = []
list2 = []
list3 = []
value = []
value0 = []#储存牌代表的值
value1 = []
value2 = []
value3 = []
l00 = []#储存进行升序排序后的牌
l11 = []
l22 = []
l33 = []
for i in flower:
  for j in pai:
    list.append(i+j)
for i in range(4):
  for j in range(13):
    value.append(j)
d = dict(zip(list,value))
for i in range(13):
  for j in range(4):
    if(j == 0):
      k = choice(list)#随机选牌
      for x in range(len(list)):
        if(k == list[x]):#和链表的牌进行匹配,删掉对应项
          value0.append(d[k])
          list.pop(x)
          break
      list0.append(k)
    if(j == 1):
      k = choice(list)
      for x in range(len(list)):
        if (k == list[x]):
          value1.append(d[k])
          list.pop(x)
          break
      list1.append(k)
    if (j == 2):
      k = choice(list)
      for x in range(len(list)):
        if (k == list[x]):
          value2.append(d[k])
          list.pop(x)
          break
      list2.append(k)
    if (j == 3):
      k = choice(list)
      for x in range(len(list)):
        if (k == list[x]):
          value3.append(d[k])
          list.pop(x)
          break
      list3.append(k)
d0 = dict(zip(list0,value0))#将每个人的牌转换为字典形式
d1 = dict(zip(list1,value1))
d2 = dict(zip(list2,value2))
d3 = dict(zip(list3,value3))
l0 = sorted(d0.values())#对牌所代表的数字进行排序
l1 = sorted(d1.values())
l2 = sorted(d2.values())
l3 = sorted(d3.values())
#对发给每个人的牌进行排序
for i in range(len(l0)):
  for j in list0:
    if(l0[i] == d0[j]):
      l00.append(j)
      break
for i in range(len(l1)):
  for j in list1:
    if(l1[i] == d1[j]):
      l11.append(j)
      break
for i in range(len(l2)):
  for j in list2:
    if(l2[i] == d2[j]):
      l22.append(j)
      break
for i in range(len(l0)):
  for j in list3:
    if(l3[i] == d3[j]):
      l33.append(j)
      break
# y = choice(['0','1','2','3'])
print("第一个人的牌:",l00)
print("第二个人的牌:",l11)
print("第三个人的牌:",l22)
print("第四个人的牌:",l33)
y = random.randint(0,3)
if (y == 0):
  y = y + 1
  n = l0[0]
  l0.pop(0)
elif (y == 1):
  y = y + 1
  n = l1[0]
  l1.pop(0)
elif (y == 2):
  y = y + 1
  n = l2[0]
  l2.pop(0)
elif (y == 3):
  y = 0
  n = l3[0]
  l3.pop(0)
for i in range(13):
  if(y == 0):
    for j in range(len(l0)):
      if(l0[j] > n):
        n = l0[j]
        l0.pop(j)
        if(len(l0) != 0 and n >= l1[len(l1)-1] and n >= l2[len(l2)-1] and n >= l3[len(l3)-1]):#判断是否当前牌中大牌,若是,则该此人继续出牌
          n = l0[0]
          l0.pop(0)
        break
    y = y + 1
  if (len(l0) == 0):
    print("赢家:第一个人")
    break
  if(y == 1):
    for j in range(len(l1)):
      if(l1[j] > n):
        n = l1[j]
        l1.pop(j)
        if (len(l1) != 0 and n >= l0[len(l0) - 1] and n >= l2[len(l2) - 1] and n >= l3[len(l3) - 1]):
          n = l1[0]
          l1.pop(0)
        break
    y = y + 1
  if (len(l1) == 0):
    print("赢家:第二个人")
    break
  if(y == 2):
    for j in range(len(l2)):
      if(l2[j] > n):
        n = l2[j]
        l2.pop(j)
        if (len(l2) != 0 and n >= l0[len(l0) - 1] and n >= l1[len(l1) - 1] and n >= l3[len(l3) - 1]):
          n = l2[0]
          l2.pop(0)
        break
    y = y + 1
  if (len(l2) == 0):
    print("赢家:第三个人")
    break
  if (y == 3):
    for j in range(len(l3)):
      if (l3[j] > n):
        n = l3[j]
        l3.pop(j)
        if (len(l3) != 0 and n >= l0[len(l0) - 1] and n >= l1[len(l1) - 1] and n >= l2[len(l2) - 1]):
          n = l3[0]
          l3.pop(0)
        break
    y = 0
  if (len(l3) == 0):
    print("赢家:第四个人")
    break
#将剩余牌从键值转化成牌
if(len(l0) != 0):
  for i in range(len(l0)):
    for j in list0:
      if(l0[i] == d0[j]):
        l0[i] = j
        break
if(len(l1) != 0):
  for i in range(len(l1)):
    for j in list1:
      if(l1[i] == d1[j]):
        l1[i] = j
        break
if(len(l2) != 0):
  for i in range(len(l2)):
    for j in list2:
      if(l2[i] == d2[j]):
        l2[i] = j
        break
if(len(l3) != 0):
  for i in range(len(l3)):
    for j in list3:
      if(l3[i] == d3[j]):
        l3[i] = j
        break
print("第一个人的牌:",l0)
print("第二个人的牌:",l1)
print("第三个人的牌:",l2)
print("第四个人的牌:",l3)

本文标题:python如何实现四人制扑克牌游戏-创新互联
分享URL:http://pwwzsj.com/article/ccdoed.html