Vieux garcon游戏程序初始对子移除功能异常排查求助
Vieux Garçon卡牌游戏开局对子移除bug修复
问题描述
我开发的Vieux Garçon卡牌游戏,开局阶段本该移除手牌中所有对子,但实际运行后手牌仍存在多张10牌的对子(比如10♠、10♣、10♡),后续游戏阶段的对子移除功能却正常。
当前程序输出
Hello. I am Robot and I distribute the cards. Your hand is: 2♠ 5♡ Q♠ Q♡ 4♡ J♢ 2♣ J♠ K♢ 8♢ 10♠ A♣ 5♣ 3♣ 6♣ 4♢ 7♣ 9♢ 2♢ J♡ A♢ 8♠ 10♣ 6♢ 10♡ Don't worry, I cannot see your cards or their order. Now discard all pairs in your hand. I will do it too. Press Enter to continue. ************************************************************ Your turn. Your hand is: 9♢ J♡ 3♣ K♢ 10♠ 2♢ 10♣ 10♡ 7♣
完整代码
# vieux garcon card game. import random def wait_for_player(): try: input("Press Enter to continue. ") except SyntaxError: pass def prepare_pack(): pack = [] colors = ['♠', '♡', '♢', '♣'] value = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] for val in value: for color in colors: pack.append(val + color) pack.remove('J♣') return pack def shuffle_pack(p): random.shuffle(p) def give_cards(p): giver = [] other = [] loop = 0 for i in p: if (loop % 2) == 0: giver.append(i) else: other.append(i) loop += 1 return (giver, other) def remove_pairs(l): colors = ['♠', '♡', '♢', '♣'] result = [] for crt in l: if crt[1] + crt[0] == '10': nbr = '10' else: nbr = crt[0] for i in colors: find = False if nbr + i in result: find = True result.remove(nbr + i) break if find == False: result.append(crt) random.shuffle(result) return result def show_cards(p): for i in p: print(i, end = " ") print() def enter_valid_position(n): enter = int(input('Please enter a number from 1 to'+ str(n) +": ")) - 1 while enter not in range(0, n): enter = int(input('Invalid position. Please enter a number from 1 to ' + str(n) +": ")) - 1 return enter def play(): p = prepare_pack() shuffle_pack(p) tmp = give_cards(p) giver = tmp[0] human = tmp[1] print("Hello. I am Robot and I distribute the cards.") print("Your hand is:") show_cards(human) print("Don't worry, I cannot see your cards or their order.") print( "Now discard all pairs in your hand. I will do it too." ) wait_for_player() giver = remove_pairs(giver) human = remove_pairs(human) #Instance = 1, human's turn, instance = 2, robot's turn instance = 1 while instance in range(1,2): if instance == 1: print('*' * 60) print('Your turn.') print('Your hand is: ') show_cards(human) print("I have ", len(giver), "cards. If 1 is the position of my first card and ") print(len(giver), "is the position of my last card, which card do you want?") entry = enter_valid_position(len(giver)) print("You asked for my card number ", entry+1, ".") print("There it is. It is a ", giver[entry]) print("With ", giver[entry]," added, your hand is:") human.append(giver[entry]) giver.remove(giver[entry]) show_cards(human) human=remove_pairs(human) if len(human) == 0: print("I finished all the cards.") win = True break print("After discarding all pairs and shuffling the cards, your hand is:") show_cards(human) wait_for_player() instance = 2 pass if instance == 2: print('*' * 60) print('My turn.') take_card = random.randint(0, len(human) - 1) giver.append(human[take_card]) human.remove(human[take_card]) print("I took your card number ", take_card+1,".") giver=remove_pairs(giver) if len(giver)==0: print("I finished all the cards.") win = False break wait_for_player() instance = 1 pass if win == True: print("Congratulations! You, Human, have won.") else: print("You lost! Me, Robot, have won.") # main play()
问题分析与修复方案
问题核心在remove_pairs函数的10牌数值提取逻辑错误:
原代码用crt[1] + crt[0] == '10'判断10牌,完全颠倒了字符顺序(10牌格式是10♠,crt[0]是'1',crt[1]是'0'),导致无法正确识别10牌,进而无法移除其对子。
修改后的remove_pairs函数:
def remove_pairs(l): colors = ['♠', '♡', '♢', '♣'] result = [] for crt in l: # 正确识别10牌:判断字符串开头是否为'10' if crt.startswith('10'): nbr = '10' else: nbr = crt[0] find = False for i in colors: if nbr + i in result: find = True result.remove(nbr + i) break if not find: result.append(crt) random.shuffle(result) return result
修复说明
- 用
crt.startswith('10')替代原错误判断,准确识别10牌 - 将
find变量初始化移至循环外,避免逻辑混乱 - 简化
if find == False为if not find,代码更简洁
修改后,开局阶段的10牌对子会被正确移除,后续游戏逻辑不受影响。
内容的提问来源于stack exchange,提问作者metal
相关产品推荐
相关产品推荐

