Created
October 16, 2017 06:39
-
-
Save ronfe/3fab9da00ca7c3bd41765d2f3779012e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| value = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, | |
| '9':9, 'T':10, 'J':10, 'Q':10, 'K':10, 'A':1} | |
| def check_value(cards): | |
| v = sum([value[i] for i in cards]) | |
| if v <= 11 and 'A' in cards: | |
| v += 10 | |
| return v | |
| def ai_decide(cards): | |
| ## HIDDEN | |
| pass | |
| def player_decide(cards): | |
| ## YOUR CODE HERE | |
| pass | |
| def draw_cards(player): | |
| player.append(deck.pop()) | |
| def main(): | |
| print("游戏开始") | |
| deck = list('23456789TJQKA'*16) | |
| len(deck) | |
| random.shuffle(deck) | |
| player_sum = 0 | |
| ai_sum = 0 | |
| game_set = 0 | |
| game_over = False | |
| while not game_over: | |
| print("") | |
| print("当前比分:{}-{}".format(player_sum, ai_sum)) | |
| game_set += 1 | |
| print("第{}局,牌库中剩余{}张牌".format(game_set, len(deck))) | |
| # 发牌 | |
| player_hand = [deck.pop() for _ in range(2)] | |
| ai_hand = [deck.pop() for _ in range(2)] | |
| print("玩家得到{}和{}".format(player_hand[0], player_hand[1])) | |
| print("庄家得到{}和{}".format(ai_hand[0], ai_hand[1])) | |
| while True: | |
| d = player_decide(player_hand) | |
| if check_value(player_hand) > 21: | |
| print("玩家爆牌") | |
| break | |
| elif d == "Hit" and len(deck) > 0: | |
| print("玩家要牌") | |
| x = deck.pop() | |
| print("玩家得到{}".format(x)) | |
| player_hand.append(x) | |
| elif d == "Hit" and len(deck) == 0: | |
| print("可用牌数不足,游戏结束!") | |
| game_over = True | |
| break | |
| else: | |
| print("玩家停止要牌,手上有{}".format(player_hand)) | |
| break | |
| while True: | |
| d = ai_decide(ai_hand) | |
| if check_value(ai_hand) > 21: | |
| print("庄家爆牌") | |
| break | |
| elif d == "Hit" and len(deck) > 0: | |
| print("庄家要牌") | |
| x = deck.pop() | |
| print("庄家得到{}".format(x)) | |
| ai_hand.append(x) | |
| elif d == "Hit" and len(deck) == 0: | |
| print("可用牌数不足,游戏结束!") | |
| game_over = True | |
| break | |
| else: | |
| print("庄家停止要牌,手上有{}".format(ai_hand)) | |
| break | |
| ### 总结 | |
| blackjack = False | |
| if len(player_hand) == 2 and 'K' in player_hand and 'A' in player_hand: | |
| print("玩家黑杰克,加2分") | |
| blackjack = True | |
| player_sum += 2 | |
| if len(ai_hand) == 2 and 'K' in ai_hand and 'A' in ai_hand: | |
| blackjack = True | |
| print("庄家黑杰克,加2分") | |
| ai_sum += 2 | |
| if not blackjack: | |
| player_score = check_value(player_hand) | |
| ai_score = check_value(ai_hand) | |
| if player_score > 21 and ai_score <= 21: | |
| print("玩家爆牌,庄家加1分") | |
| ai_sum += 1 | |
| elif player_score <= 21 and ai_score > 21: | |
| print("庄家爆牌,玩家加1分") | |
| player_sum += 1 | |
| elif player_score <= 21 and ai_score <= 21: | |
| if player_score < ai_score: | |
| print("玩家{},庄家{},庄家胜,加1分".format(player_score, ai_score)) | |
| ai_sum += 1 | |
| elif player_score == ai_score: | |
| print("玩家{},庄家{},平手".format(player_score, ai_score)) | |
| elif player_score > ai_score: | |
| print("玩家{},庄家{},玩家胜,加1分".format(player_score, ai_score)) | |
| player_sum += 1 | |
| else: | |
| print("两家均爆牌") | |
| x = input("请按回车开始下一局。。。") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment