def select(crds): # -> idxSet, zid assert type(crds) is HandCards, "TypeError: not HandCards" assert 0 < len(crds) <= 5, "IndexError: overrange" keyMap = {'Z': 0, 'X': 1, 'C': 2, 'V': 3, 'B': 4} keyStr = "ZXCVB" keyStr = keyStr[0:len(crds)] # fix length to n print('use [%s] to select:' % keyStr) idxSet = set() selC, actLst = [], [] while True: k = getkey(blocking=True) # block version kn = keys.name(k) if len(kn) == 1 and kn in keyMap: if kn in idxSet: # already in set idxSet ^= {keyMap[kn]} # xor, flip else: idxSet |= {keyMap[kn]} # or, update selC = Cards([crds[i] for i in idxSet]) actLst = match(selC) print(selC, actLst) elif kn in {'ENTER', 'SPACE'} and len(actLst) > 0: if len(actLst) > 1: return idxSet, actLst[choose(actLst)] else: return idxSet, actLst[0] elif kn == "ESC": idxSet = set() else: pass
def check(self, inputs): if not self.cards.isContain(Cards(inputs)): return False, "played not exist card! your turn:" v = cardStringsValue(inputs) if v[1] <= 0: return False, "invalid card type! your turn:" return True, v
def draw( player: 'Player', deck: Deck, num: int=1 ) -> typing.Tuple['Player', Deck]: if num not in range(1, len(deck) + 1): return player, deck return ( Player(player.name(), hand=player.hand() + Cards(deck[:num])), Deck(deck[num:]))
def play(self, last_round): print("hand: %s \n" % self.cards) inputs = input("your turn, enter letter to play or enter p to pass:"******"p": return PLAY_PASS inputs = upper(inputs) check_ret, v = self.check(inputs) if check_ret is True: cards = Cards(inputs, v[0], v[1]) self.removeCards(cards) return cards inputs = input(v)
def play(self, last_round): print("AI", self.hc) if last_round in (PLAY_FIRST, PLAY_PASS): p = self.initiativePlay() else: p = self.passivePlay(last_round) if p == PLAY_PASS: print("PASS") return p v = cardStringsValue(p) ret = Cards(p, v[0], v[1]) self.hc.remove(ret) print("play", ret) return ret
def selectR(crds): # -> idxSet, zid assert type(crds) is HandCards, "TypeError: not HandCards" assert 0 < len(crds) <= 5, "IndexError: overrange" bag = list(range(1, 2**len(crds))) while True: if len(bag) > 0: r = randint(0, len(bag) - 1) # as index else: raise RuntimeError("no choice in bag") selPat = [bag[r] & (2**i) > 0 for i in range(5)] # binary exp idxSet = [] selC = [] for i in range(5): if selPat[i]: selC.append(crds[i]) idxSet.append(i) actLst = match(Cards(selC)) if len(actLst) >= 1: return set(idxSet), choice(actLst) else: del bag[r] # remove this val
def read_cards(database, query_tuple): """ read card data """ conn = sqlite3.connect(database) cursor = conn.cursor() if query_tuple == "all": cursor.execute("SELECT DISTINCT posid, statid FROM cards") all_cards = cursor.fetchall() else: all_cards = query_tuple cards = Cards() for t in all_cards: cursor.execute( """SELECT time FROM cards WHERE posid = ? AND statid = ? ORDER BY time ASC""", t) results = cursor.fetchall() results = [time[0] for time in results] card = Card(results, t) cards.add(card) cursor.close() conn.close() return cards
def reset_profile(self): self.cards = Cards() self.identity = None self.landlord_score = None
# game object, use ring to keep game running from card import Deck, Cards from player import Player from ring import Ring from time import sleep deck = Deck() disP = Cards() p1 = Player("A", deck, disP) p2 = Player("B", deck, disP) players = Ring([p1, p2]) turn = 0 while True: turn = turn + 1 print("turn#%d " % turn, end='') players.now().action() players.goNext() sleep(1)
def __init__(self, name: str, *, hand: Cards=Cards()) -> None: self._name = name self._hand = hand
# load asset npc kiri = pygame.image.load('assets/kiri.png') kanan = pygame.image.load('assets/kanan.png') atas = pygame.image.load('assets/atas.png') kiri2 = pygame.image.load('assets/kiri.png') kanan2 = pygame.image.load('assets/kanan.png') atas2 = pygame.image.load('assets/atas.png') # width, height npc kiri = pygame.transform.scale(kiri, (90, 120)) kanan = pygame.transform.scale(kanan, (90, 120)) atas = pygame.transform.scale(atas, (120, 90)) kiri2 = pygame.transform.scale(kiri, (90, 120)) kanan2 = pygame.transform.scale(kanan, (90, 120)) atas2 = pygame.transform.scale(atas, (120, 90)) cards = Cards(pygame, screen) cards.load('assets/PNGs/new/') submit_button = Button(pygame, screen, 'assets/submit.png', 1050, 600) start_button = Button(pygame, screen, 'assets/start.png', 831, 465) draw_button = Button(pygame, screen, 'assets/draw.png', 950, 600) skip_button = Button(pygame, screen, 'assets/skip.png', 950, 600) submit_button.scale(94,40) draw_button.scale(90,40) skip_button.scale(56,40) select_color = 0 while(True): if game_status == None:
def __init__(self): self.id = None self.cards = Cards()
def __init__(self, name, credits): self.name = name self.credits = credits self.cards = Cards() self.identity = None self.landlord_score = None
def deal_cards(self, last_cards, last_person_identity=None): ## 先给玩家看自己手中的牌 MessagePoster.post_hint(hint_type='player_show_cards', unshowed_cards=str(self.cards)) ## 让用户一直出牌, 直到牌大过上家或者放弃出牌 while True: raw_order = MessageGetter.get_order(order_type='deal_cards') ## 退出游戏 if isinstance(raw_order, GameExiter): return GameExiter() ## 如果玩家要不起/不要, 则返回None if raw_order == 'p': ## 有牌权出牌不能跳过 if last_cards is not None: MessagePoster.post_hint( hint_type='person_abandon_dealing_cards', output_name=self.output_name) return else: MessagePoster.post_hint( hint_type= 'player_can_not_skip_deal_cards_without_last_cards') continue undealed_cards = Cards([Card(pattern) for pattern in raw_order]) ## 牌的验证 ## 牌的子集验证 if not undealed_cards in self.cards: MessagePoster.post_hint( hint_type='invaild_cards_pattern_or_too_many_cards') continue meta_data = undealed_cards.cal_meta_data() ## 牌的元数据验证(实际是有效性验证) if meta_data is None: MessagePoster.post_hint(hint_type='invaild_cards_species') continue ## 出牌 ## 如果是有牌权出牌, 可以直接出 if last_cards is None: MessagePoster.post_hint(hint_type='person_dealed_cards', output_name=self.output_name, unprinted_cards=str(undealed_cards)) self.cards -= undealed_cards return undealed_cards ## 牌型相符且牌比上家大(考虑到炸弹类型) elif undealed_cards > last_cards: MessagePoster.post_hint(hint_type='person_dealed_cards', output_name=self.output_name, unprinted_cards=str(undealed_cards)) self.cards -= undealed_cards return undealed_cards ## 牌型不符或牌型一样但没有上家大 else: MessagePoster.post_hint(hint_type='cards_size_too_small') continue
def newRound(self): self._all_cards = Cards()
disc = drawC.pickup(idxSet) print("drawWD(): drawC, disc ->", drawC, disc) self.discPile.join(disc) # drop one card self.hand.join(drawC) # drawC >> hand def action(self): # exec, deal and draw print("player(%s) action" % self.name) self.deal() self.draw() # self.drawD(deck) # note: let upper object contorl turn if __name__ == "__main__": deck = Cards([13, 12, 11, 10, 9, 8, 7, 6]) deck = Deck([0, 1, 1, 1, 1, 1, 1, 1, 1]) disP = Cards() p = Player("s", deck, disP, [1, 2, 3]) print(p) p.deal() print("after deal()", p) print("draw(1) from deck") p.draw(1) print("p,deck= ", p, deck) p._clearHand() print("draw(2) from deck") p.draw(2) print("p,deck= ", p, deck)
# 查表、計算點數,準備輸出的資料位置。 # 修正數值:五行生剋、物理打盾 # 最終結果 # TODO: add log to system # TODO: change state of game if __name__ == '__main__': # print('test _getVect()') # crds = Cards([1,3,5]) # print('cards xing -> ', _getVect(crds,'xing')) # print('cards lv -> ', _getVect(crds,'lv')) # print('test _getPatType()') # print('_getPatType(crds) -> ', _getPatType(crds,'xing')) crds_z15 = Cards([1, 1, 2, 4]) # v2J1H1S = makeVecFilter('V2J1H1S') # print('test vectFilter v2J1H1S(crds) -> ', crds_z15, v2J1H1S(crds_z15)) print('test match(z15) -> ', crds_z15, match(crds_z15)) print('getVect(z15) -> ', _getVect(crds_z15, 'xing')) crds_z22 = Cards([1, 2, 3]) # n3 = makeNumFilter('N3') # print('test numFilter n3(crds) -> ', crds_z22, n3(crds_z22)) # xflat = makeXFilter("Xflat") # print('test xFilter xflat(crds) -> ', crds_z22, xflat(crds_z22)) print('test match(z22) -> ', crds_z22, match(crds_z22)) print('getPatType(z22) -> ', _getPatType(crds_z22, 'xing')) # for i in zenFilter: print(i)