# -*- coding:utf-8 -*- class Base(object): num_dict = {"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"10":10,"J":11,"Q":12,"K":13} color_dict = {"黑桃":4, "紅桃":3, "梅花":2, "方片":1} class Card(Base): """一張卡""" def __init__(self,num,color): self.num = num self.color = color self.ori_value = self.num_dict[self.num] self.num_value = self.num_dict[self.num] # 根據其餘規則設置value值 self.color_value = self.color_dict[self.color] def __repr__(self): return "{color}{num}".format(color=self.color,num=self.num) class Pokers(Base): """整副撲克""" _instance = None # 使用單例模式 def __new__(cls, *args, **kwargs): """ 單例模式 :param args: :param kwargs: :return: """ # return 真實的去建立對象 if cls._instance: return cls._instance else: # obj = '真實的去建立對象' obj = object.__new__(cls, *args, **kwargs) cls._instance = obj return obj @classmethod def cards(cls): cards = [] for color in cls.color_dict: for num in cls.num_dict: cards.append(Card(num,color)) return cards class NiuNiu(Base): """根據牛牛的規則處理5張牌""" niuniu_num_dict = {"1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9, "10": 10, "J": 10, "Q": 10, "K": 10} def __init__(self,fiveCards): self.fiveCards = self.initial(fiveCards) if hasattr(self,"initial") else fiveCards def initial(self,fiveCards): for card_obj in fiveCards: card_obj.num_value = self.niuniu_num_dict[card_obj.num] # 根據牛牛的規則,修改其num_value return fiveCards @property def result(self): fiveCards = self.fiveCards result = {"status":False,"point":0,"maxCard":None,"doubling":1,"text":"無牛"} result["maxCard"] = self.max_in_somecards(fiveCards) if self.haveNiu(fiveCards): result["status"] = True point = sum(map(lambda x:x.num_value,fiveCards)) % 10 result["point"] = point if point else 10 result["text"] = "牛{n}".format(n=point if point else "牛" ) result["doubling"] = self.rule(point) return result def rule(self,point): if point == 7 or point == 8: res = 2 elif point == 9: res = 3 elif point == 10: res = 4 else: res = 1 return res def haveNiu(self,fiveCards): """ 檢查有牛沒牛 :param FiveCards: 5張牌[obj1,obj2,obj3,obj4,obj5] :return: True/False 有牛或沒牛 """ from itertools import combinations threeCards = list(combinations(fiveCards, 3)) for three_card in threeCards: if sum(map(lambda x:x.num_value,three_card)) % 10 == 0: # 3張牌的和是10的倍數,有牛 # res = sum(set(fiveCards).difference(set(threeCards))) % 10 return True # 無牛 return False def biger_in_2card(self,card1_obj,card2_obj): """ 比較兩張牌的大小(點數、花色) :param card1: :param card2: :return: 兩張牌中較大的那張牌obj """ if card1_obj.ori_value == card2_obj.ori_value: if card1_obj.color_value > card1_obj.color_value: return card1_obj else: return card2_obj else: if card1_obj.ori_value > card2_obj.ori_value: return card1_obj else: return card2_obj def max_in_somecards(self, somecards_list): """ 比較多張牌obj的最大值 :param somecards_list: [obj1,obj2,obj3....] :return: 最大值 """ from functools import reduce max_obj = reduce(self.biger_in_2card,somecards_list) return max_obj def __repr__(self): base_ret = """ 個人牌組:{fiveCards} 結果:{result} 倍數:{doubling} 最大牌:{maxCard} """ ret = base_ret.format( fiveCards=self.fiveCards, result=self.result["text"], doubling=self.result["doubling"], maxCard=self.result["maxCard"], ) return ret class Player(object): def __init__(self,name,wager=10,role="玩家",money=0): self.name = name self.role = role self.cards = [] self.niuniu_result = {} # {"status":False,"point":0,"maxCard":None,"doubling":1,"text":"無牛"} self.money = money self.wager = wager def __repr__(self): return self.name class Game(NiuNiu): pokers = Pokers.cards() def __init__(self,zhuang_obj,player_obj_list): self.zhuang = zhuang_obj self.player_objs = player_obj_list self.all_players = [zhuang_obj] + player_obj_list self.n_cards = 5 def giveCards(self,pokers): import random ALL = random.sample(pokers, len(pokers)) player_objs = self.all_players n_players = len(player_objs) n_cards = self.n_cards for idx,player in enumerate(player_objs): tmp_l = [] for n in range(n_cards): card = ALL[n*n_players+ idx] tmp_l.append(card) player.cards = tmp_l player.niuniu_result = NiuNiu(tmp_l).result def compare_2_player(self,zhuang,player): """ 比較 莊家 和 閒家 的大小 :param zhuang: :param player: :return: True莊贏,False閒贏 """ Flag = False zhuang_niuniu = zhuang.niuniu_result player_niuniu = player.niuniu_result if zhuang_niuniu["point"] == player_niuniu["point"]: bigerCard = self.max_in_somecards([zhuang_niuniu["maxCard"],player_niuniu["maxCard"]]) if bigerCard is zhuang_niuniu["maxCard"]: Flag = True elif zhuang_niuniu["point"] > player_niuniu["point"]: Flag = True if Flag: # 莊家贏了 money_change = zhuang.niuniu_result["doubling"] * player.wager zhuang.money += money_change player.money -= money_change base_res = "\033[1;31m{zhuang}贏了{money_change},餘額:{money}【{text},倍數:{doubling},最大:{maxCard},牌組:{cards}】\033[0m;{player}輸了{money_change},餘額:{money2}【{text2},賭注:{wager},倍數:{doubling2},最大:{maxCard2},牌組:{cards2}】" else: # 莊家輸了 money_change = player.niuniu_result["doubling"] * player.wager zhuang.money -= money_change player.money += money_change base_res = "{zhuang}輸了{money_change},餘額:{money}【{text},倍數:{doubling},最大:{maxCard},牌組:{cards}】;\033[1;31m{player}贏了{money_change},餘額:{money2}【{text2},賭注:{wager},倍數:{doubling2},最大:{maxCard2},牌組:{cards2}】\033[0m" res = base_res.format( zhuang = zhuang, money_change = money_change, money = zhuang.money, text = zhuang.niuniu_result["text"], doubling = zhuang.niuniu_result["doubling"], maxCard = zhuang.niuniu_result["maxCard"], cards = zhuang.cards, player = player, money2 = player.money, text2 = player.niuniu_result["text"], wager = player.wager, doubling2 = player.niuniu_result["doubling"], maxCard2 = player.niuniu_result["maxCard"], cards2 = player.cards, ) return res def start(self): self.giveCards(self.pokers) zhuang = self.zhuang players = self.player_objs for player in players: res = self.compare_2_player(zhuang,player) print(res) zhuang = Player("玩家1(莊家)") l_players = [ Player("玩家2"), Player("玩家3"), ] game = Game(zhuang,l_players) for i in range(5): print("【第{}局】".format(i+1)) game.start() print("################################")
【第1局】 玩家1(莊家)輸了10,餘額:-10【牛5,倍數:1,最大:黑桃K,牌組:[黑桃K, 紅桃4, 黑桃Q, 方片1, 梅花10]】;玩家2贏了10,餘額:10【牛牛,賭注:10,倍數:1,最大:方片9,牌組:[黑桃5, 黑桃2, 梅花9, 紅桃5, 方片9]】 玩家1(莊家)輸了10,餘額:-20【牛5,倍數:1,最大:黑桃K,牌組:[黑桃K, 紅桃4, 黑桃Q, 方片1, 梅花10]】;玩家3贏了10,餘額:10【牛牛,賭注:10,倍數:1,最大:黑桃10,牌組:[方片8, 紅桃9, 黑桃10, 紅桃1, 方片2]】 ################################ 【第2局】 玩家1(莊家)輸了20,餘額:-40【牛1,倍數:1,最大:紅桃10,牌組:[方片7, 紅桃10, 黑桃9, 紅桃4, 紅桃1]】;玩家2贏了20,餘額:30【牛8,賭注:10,倍數:2,最大:紅桃K,牌組:[紅桃2, 紅桃K, 梅花1, 紅桃6, 梅花9]】 玩家1(莊家)贏了10,餘額:-30【牛1,倍數:1,最大:紅桃10,牌組:[方片7, 紅桃10, 黑桃9, 紅桃4, 紅桃1]】;玩家3輸了10,餘額:0【牛1,賭注:10,倍數:1,最大:方片9,牌組:[黑桃2, 方片9, 黑桃7, 紅桃7, 方片6]】 ################################ 【第3局】 玩家1(莊家)贏了30,餘額:0【牛9,倍數:3,最大:黑桃K,牌組:[黑桃J, 紅桃10, 黑桃9, 方片K, 黑桃K]】;玩家2輸了30,餘額:0【無牛,賭注:10,倍數:1,最大:梅花K,牌組:[梅花4, 黑桃4, 梅花Q, 梅花K, 方片8]】 玩家1(莊家)贏了30,餘額:30【牛9,倍數:3,最大:黑桃K,牌組:[黑桃J, 紅桃10, 黑桃9, 方片K, 黑桃K]】;玩家3輸了30,餘額:-30【牛4,賭注:10,倍數:1,最大:紅桃9,牌組:[紅桃4, 梅花8, 黑桃7, 紅桃9, 方片6]】 ################################ 【第4局】 玩家1(莊家)贏了10,餘額:40【牛牛,倍數:1,最大:紅桃K,牌組:[方片4, 紅桃K, 方片5, 方片J, 黑桃1]】;玩家2輸了10,餘額:-10【無牛,賭注:10,倍數:1,最大:方片K,牌組:[梅花10, 梅花9, 方片K, 方片6, 黑桃9]】 玩家1(莊家)贏了10,餘額:50【牛牛,倍數:1,最大:紅桃K,牌組:[方片4, 紅桃K, 方片5, 方片J, 黑桃1]】;玩家3輸了10,餘額:-40【無牛,賭注:10,倍數:1,最大:紅桃Q,牌組:[紅桃Q, 紅桃4, 方片10, 梅花1, 梅花8]】 ################################ 【第5局】 玩家1(莊家)贏了20,餘額:70【牛7,倍數:2,最大:梅花K,牌組:[方片1, 方片J, 梅花K, 紅桃6, 方片Q]】;玩家2輸了20,餘額:-30【牛1,賭注:10,倍數:1,最大:梅花Q,牌組:[梅花5, 方片5, 梅花Q, 黑桃J, 紅桃1]】 玩家1(莊家)贏了20,餘額:90【牛7,倍數:2,最大:梅花K,牌組:[方片1, 方片J, 梅花K, 紅桃6, 方片Q]】;玩家3輸了20,餘額:-60【無牛,賭注:10,倍數:1,最大:紅桃9,牌組:[梅花6, 梅花9, 紅桃9, 紅桃3, 梅花3]】 ################################
import os import re RE_NAME = "Sashow@subpig.net@Disc.\d+\.(.+)" BASE_DIR = os.path.dirname(os.path.abspath(__file__)) if __name__ == '__main__': # [0]表明只walk第0層 os_walk = list(os.walk(BASE_DIR)) files = os_walk[0][2] for file_name in files: if file_name.endswith("py"): continue new_name = re.findall(RE_NAME, file_name) if new_name: print(new_name) new_name = new_name[0] old_file = os.path.join(BASE_DIR, file_name) new_file = os.path.join(BASE_DIR, new_name) try: os.rename(old_file, new_file) except: pass