题目: 扑克牌游戏(老师讲的规则我不是很清楚,就用梭哈的规则)
逻辑:牌型比较:同花顺(A)>四条(B)>顺子(C)>三条(D)>二对(E)>单对(F)>散牌(G)。分别赋值100/99/98/97/96/95/94
数字比较:A>K>Q>J>10>9>8>7>6>5>4>3>2
花式比较:黑桃>红桃>草花>方块
同类比较:①同花顺:首先比最大的那张牌,相同的话则比最大牌的花色
②四条:比炸d的牌面大小
③顺子:与同花顺相同
④三条:比三条的牌面大小
⑤两对:比两对牌的大小,比不出来再比花色
⑥一对:比对牌的大小,比不出来再比花色,若出现黑桃方块一对、红桃草花一对,则规定有黑桃的一对更大,
两对同理(直接对黑桃赋值5,红桃赋值3,草花赋值2,方块赋值1)。
⑦散牌:直接比最大的那张牌
接下来直接上代码:
import random #在52张牌里随机产生两幅牌def produce_cards(): #num = List(range(1, 13)) #color = (1, 2, 3, 4) card = [] cardone = [] cardtwo = [] total_card = [{'number':2, 'color':1},{'number':2, 'color':2},{'number':2, 'color':3},{'number':2, 'color':5}, {'number':3, 'color':1},{'number':3, 'color':2},{'number':3, 'color':3},{'number':3, 'color':5}, {'number':4, 'color':1},{'number':4, 'color':2},{'number':4, 'color':3},{'number':4, 'color':5}, {'number':5, 'color':1},{'number':5, 'color':2},{'number':5, 'color':3},{'number':5, 'color':5}, {'number':6, 'color':1},{'number':6, 'color':2},{'number':6, 'color':3},{'number':6, 'color':5}, {'number':7, 'color':1},{'number':7, 'color':2},{'number':7, 'color':3},{'number':7, 'color':5}, {'number':8, 'color':1},{'number':8, 'color':2},{'number':8, 'color':3},{'number':8, 'color':5}, {'number':9, 'color':1},{'number':9, 'color':2},{'number':9, 'color':3},{'number':9, 'color':5}, {'number':10, 'color':1},{'number':10, 'color':2},{'number':10, 'color':3},{'number':10, 'color':5}, {'number':11, 'color':1},{'number':11, 'color':2},{'number':11, 'color':3},{'number':11, 'color':5}, {'number':12, 'color':1},{'number':12, 'color':2},{'number':12, 'color':3},{'number':12, 'color':5}, {'number':13, 'color':1},{'number':13, 'color':2},{'number':13, 'color':3},{'number':13, 'color':5}, {'number':14, 'color':1},{'number':14, 'color':2},{'number':14, 'color':3},{'number':14, 'color':5}] m = 51 for i in range(10): j = random.randint(0, m) card.append(total_card[j]) total_card.remove(total_card[j]) m -= 1 for i in range(5): cardone.append(card[i]) cardtwo.append(card[i+5]) return cardone, cardtwo def my_number(e): return e['number'] def my_color(e): return e['color'] #按照牌面大小排序def sort_number(card): card.sort(key = my_number) #按照花色排序def sort_color(card): card.sort(key = my_color, reverse=True) #寻找制定牌的花色def findcolor(number, card): color_List = [] for i in range(15): if card[i]['number'] == number: color_List.append(card[i]['color']) return color_List #将牌进行分类(同花顺、四条、顺子等等)def classify(card): category = 94 flag = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] #记录牌面 Flag = [0,0,0,0,0,0] #记录花色 List1 = [] List2 = [] B_trait = 0 #四条的牌面大小 D_trait = 0 #三条的牌面大小 dui_trait = [] #表示第几张牌是对子 for i in range(5): List1.append(card[i]['number']) List2.append(card[i]['color']) shun_flag = 0 #顺子判断 dui_flag = 0 #对子判断 for i in range(5): n = List1[i] flag[n] += 1 m = List2[i] Flag[m] += 1 for i in range(15): if (flag[i-1] ==1 and flag[i] == 1): shun_flag += 1 if (flag[i] == 4): category = 99 B_trait = i if (flag[i] == 3): category = 97 D_trait = i if (flag[i] == 2): category = 95 dui_flag += 1 dui_trait.append(i) if (shun_flag == 4): category = 98 for i in range(5): if (Flag[i] == 5): category = 100 if (dui_flag == 2): category = 96 return category, B_trait, D_trait, dui_trait #四条之间比大小def B(B_trait1, B_trait2): if (B_trait1 > B_trait2): flagB = 1 else: flagB = 2 return flagB #同花顺、顺子、散牌都只需要比最大的牌面就行def A_and_C_and_G(card1, card2): max1 = card1[4]['number'] max2 = card2[4]['number'] color1 = card1[4]['color'] color2 = card2[4]['color'] if (max1 > max2): flagACG = 1 if (max1 < max2): flagACG = 2 if (max1 == max2): if(color1 > color2): flagACG = 1 else: flagACG =2 return flagACG #三条之间比大小def D(D_trait1, D_trait2): if (D_trait1 > D_trait2): flagD = 1 else: flagD = 2 return flagD #单对及双对之间比大小,因为都可以之比一对,所以两种情况可以合并def E_and_F(categorIEs, dui_trait1, dui_trait2, card1, card2): if (categorIEs == 96): i = 1 if (categorIEs != 96): i = 0 color1 = [] color2 = [] if (dui_trait1[i] > dui_trait2[i]): flagEF = 1 if (dui_trait1[i] < dui_trait2[i]): flagEF = 2 if (dui_trait1[i] == dui_trait2[i]): color1 = findcolor(dui_trait1[i], card1) color2 = findcolor(dui_trait2[i], card2) color1_sum = color1[0] + color1[1] color2_sum = color2[0] + color2[1] if (color1_sum > color2_sum): flagEF = 1 if (color1_sum < color2_sum): flagEF = 2 return flagEF #将代表花色的数字转换成直观的图案输出def print_card(card): for i in range(5): NUMBER = card[i]['number'] if (card[i]['number'] == 11): NUMBER = 'J' if (card[i]['number'] == 12): NUMBER = 'Q' if (card[i]['number'] == 13): NUMBER = 'K' if (card[i]['number'] == 14): NUMBER = 'A' if (card[i]['color'] == 1): color = '♦' if (card[i]['color'] == 2): color = '♣' if (card[i]['color'] == 3): color = '♥' if (card[i]['color'] == 5): color = '♠' print(color + str(NUMBER)) def main(): #try: card_one, card_two = produce_cards() print('你的牌是:') print_card(card_one) print("对手的牌是:") print_card(card_two) category1, btrait1, dtrait1, duitrait1 = classify(card_one) category2, btrait2, dtrait2, duitrait2 = classify(card_two) if (category1 > category2): win_flag = 1 if (category1 < category2): win_flag = 2 if (category1 == category2): if (category1 in [100,98,94]): sort_number(card_one) sort_number(card_two) win_flag = A_and_C_and_G(card_one, card_two) if (category1 in [96, 95]): win_flag = E_and_F(category1, duitrait1, duitrait2, card_one, card_two) if (category1 == 99): win_flag = B(btrait1, btrait2) if (category1 == 97): win_flag = D(dtrait1, dtrait2) if (win_flag == 1): print("恭喜,你赢了!") else: print("很遗憾,你输了这一局") #except: # UnboundLocalError main()
最后try except被我注释掉了,因为问题解决了,把局部变量尽量不和全局变量重名就行了。
随便截了几个运行结果:
总结以上是内存溢出为你收集整理的python习题:梭哈(扑克牌游戏)全部内容,希望文章能够帮你解决python习题:梭哈(扑克牌游戏)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)