文章目录

  • 游戏玩法
  • 代码 - v1
  • 测试
  • v2
  • 测试

游戏玩法

游戏玩法: 该游戏由 2 到 6 个人玩,使用除大小王之外的 52 张牌,
游戏者的目标是使手中的牌的点数之和不超过 21 点且尽量大。
有着悠久的历史。黑杰克简称为21点,1700年左右法国赌场就有这种21点的纸牌游戏。
1931年,当美国内华达州宣布赌博为合法活动时,21点游戏第一次公开出现在内华达州的赌场俱乐部,
15年内,它取代掷骰子游戏,而一举成为非常流行的赌场庄家参与的赌博游戏。

代码 - v1

import randomdeck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11] * 4random.shuffle(deck)print("                       **********************************************************                                    ")
print("                                   Welcome to the game Casino - BLACK JACK (21点)!                                         ")
print("                       **********************************************************                                    ")d_cards = []  # Initialising dealer's cards
p_cards = []  # Initialising player's cardswhile len(d_cards) != 2:random.shuffle(deck)d_cards.append(deck.pop())if len(d_cards) == 2:print('荷官有 X ', d_cards[1])# Displaying the Player's cards
while len(p_cards) != 2:random.shuffle(deck)p_cards.append(deck.pop())if len(p_cards) == 2:print("你一共 ", str(sum(p_cards)) + "点 :",p_cards)if sum(p_cards) > 21:print("你的点数:",p_cards)print("你输了 !\n  **************荷官 Wins !!******************\n")exit()if sum(d_cards) > 21:print("荷官的点数:", d_cards)print("荷官输了 !\n   ************** You are the Winner !!******************\n")exit()if sum(d_cards) == 21:print("荷官的点数:", d_cards)print("***********************荷官 is the Winner !!******************")exit()if sum(d_cards) == 21 and sum(p_cards) == 21:print("*****************The match is tie 平手!!*************************")exit()def dealer_choice():if sum(d_cards) < 17:while sum(d_cards) < 17:random.shuffle(deck)d_cards.append(deck.pop())print("你一共 " + str(sum(p_cards)) + "点 :", p_cards)print("荷官一共 " + str(sum(d_cards)) + "点 :", d_cards)if sum(p_cards) == sum(d_cards):print("***************The match is tie 平手!!****************")exit()if sum(d_cards) == 21:if sum(p_cards) < 21:print("***********************Dealer is the Winner !!******************")elif sum(p_cards) == 21:print("********************There is tie !!**************************")else:print("***********************Dealer is the Winner !!******************")elif sum(d_cards) < 21:if sum(p_cards) < 21 and sum(p_cards) < sum(d_cards):print("***********************Dealer is the Winner !!******************")if sum(p_cards) == 21:print("**********************Player is winner !!**********************")if sum(p_cards) < 21 and sum(p_cards) > sum(d_cards):print("**********************Player is winner !!**********************")else:if sum(p_cards) < 21:print("**********************Player is winner !!**********************")elif sum(p_cards) == 21:print("**********************Player is winner !!**********************")else:print("***********************Dealer is the Winner !!******************")while sum(p_cards) < 21:k = input('Want to hit or stay?\n Press 1 for hit and 0 for stay ')if k == 1:random.shuffle(deck)p_cards.append(deck.pop())print('你的点数:' + str(sum(p_cards)), p_cards)if sum(p_cards) > 21:print('*************你输了 !*************\n Dealer Wins !!')if sum(p_cards) == 21:print('*******************你赢了 !!*****************************')else:dealer_choice()break

测试

case-1

                       **********************************************************                                    Welcome to the game Casino - BLACK JACK (21点)!                                         **********************************************************
荷官有 X  3
你一共  9点 : [6, 3]
Want to hit or stay?Press 1 for hit and 0 for stay 1
你一共 9点 : [6, 3]
荷官一共 18点 : [5, 3, 10]
***********************Dealer is the Winner !!******************

case-2

                       **********************************************************                                    Welcome to the game Casino - BLACK JACK (21点)!                                         **********************************************************
荷官有 X  1
你一共  18点 : [8, 10]
Want to hit or stay?Press 1 for hit and 0 for stay 0
你一共 18点 : [8, 10]
荷官一共 17点 : [5, 1, 9, 2]
**********************Player is winner !!**********************

v2

import randomsuits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
values = {'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8,'Nine': 9, 'Ten': 10, 'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 11}playing = Trueclass Card:def __init__(self, suit, rank):self.suit = suitself.rank = rankdef __str__(self):return self.rank + ' of ' + self.suitclass Deck:def __init__(self):self.deck = []for suit in suits:for rank in ranks:self.deck.append(Card(suit, rank))def __str__(self):deck_comp = ''for card in self.deck:deck_comp += '\n ' + card.__str__()def shuffle(self):random.shuffle(self.deck)def deal(self):single_card = self.deck.pop()return single_cardclass Hand:def __init__(self):self.cards = []self.value = 0self.aces = 0  # to keep track of acesdef add_card(self, card):self.cards.append(card)self.value += values[card.rank]if card.rank == 'Ace':self.aces += 1def adjust_for_ace(self):while self.value > 21 and self.aces:self.value -= 10self.aces -= 1class Chips:def __init__(self):self.total = 100self.bet = 0def win_bet(self):self.total += self.betdef lose_bet(self):self.total -= self.betdef take_bet(chips):while True:try:chips.bet = int(input('How many chips would you like to bet? '))except ValueError:print('Your bet must be an integer! Try again.')else:if chips.bet > chips.total or chips.bet <= 0:print("Your bet cannot exceed your balance and you have to enter a positive bet! Your current balance is: ",chips.total)else:breakdef hit(deck, hand):hand.add_card(deck.deal())hand.adjust_for_ace()def hit_or_stand(deck, hand):global playingwhile True:x = input("Would you like to Hit or Stand? Enter '1' or '0' ")if x.lower() == '1':hit(deck, hand)elif x.lower() == '0':print("You chose to stand. Dealer will hit.")playing = Falseelse:print("Wrong input, please try again.")continuebreakdef show_some(player, dealer):print("\nDealer's Hand:")print(" { hidden card }")print('', dealer.cards[1])print("\nYour Hand:", *player.cards, sep='\n ')def show_all(player, dealer):print("\nDealer's Hand:", *dealer.cards, sep='\n ')print("Dealer's Hand =", dealer.value)print("\nYour Hand:", *player.cards, sep='\n ')print("Your Hand =", player.value)def player_busts(player, dealer, chips):print("You are BUSTED !")chips.lose_bet()def player_wins(player, dealer, chips):print("You are the winner!")chips.win_bet()def dealer_busts(player, dealer, chips):print("Dealer has BUSTED !")chips.win_bet()def dealer_wins(player, dealer, chips):print("Dealer is the winner!")chips.lose_bet()def push(player, dealer):print("The match is tie !")# GAMEPLAY
player_chips = Chips()while True:print("\t              **********************************************************")print("\t                       Welcome to the game Casino - BLACK JACK !                                                     ")print("\t              **********************************************************")print("\t                                   ***************")print("\t                                   * A           *")print("\t                                   *             *")print("\t                                   *      *      *")print("\t                                   *     ***     *")print("\t                                   *    *****    *")print("\t                                   *     ***     *")print("\t                                   *      *      *")print("\t                                   *             *")print("\t                                   *             *")print("\t                                   ***************")print('\nRULES: Get as close to 21 as you can but if you get more than 21 you will lose!\n  Aces count as 1 or 11.')deck = Deck()deck.shuffle()player_hand = Hand()player_hand.add_card(deck.deal())player_hand.add_card(deck.deal())dealer_hand = Hand()dealer_hand.add_card(deck.deal())dealer_hand.add_card(deck.deal())take_bet(player_chips)show_some(player_hand, dealer_hand)while playing:hit_or_stand(deck, player_hand)show_some(player_hand, dealer_hand)if player_hand.value > 21:player_busts(player_hand, dealer_hand, player_chips)breakif player_hand.value <= 21:while dealer_hand.value < 17:hit(deck, dealer_hand)show_all(player_hand, dealer_hand)if dealer_hand.value > 21:dealer_busts(player_hand, dealer_hand, player_chips)elif dealer_hand.value > player_hand.value:dealer_wins(player_hand, dealer_hand, player_chips)elif dealer_hand.value < player_hand.value:player_wins(player_hand, dealer_hand, player_chips)else:push(player_hand, dealer_hand)print("\nYour current balance stands at", player_chips.total)if player_chips.total > 0:new_game = input("Would you like to play another hand? Enter '1' or '0' ")if new_game.lower() == '1':playing = Truecontinueelse:print("Thanks for playing!\n \t$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n \t      Congratulations! You won {} coins!\n\t$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n ".format(player_chips.total))breakelse:print("Oops! You have bet all your chips and we are sorry you can't play more.\nThanks for playing! Do come again to Casino BLACK JACK!")break

测试

               **********************************************************Welcome to the game Casino - BLACK JACK !                                                     ************************************************************************** A           **             **      *      **     ***     **    *****    **     ***     **      *      **             **             ****************RULES: Get as close to 21 as you can but if you get more than 21 you will lose!Aces count as 1 or 11.
How many chips would you like to bet? 1Dealer's Hand:{ hidden card }Ten of HeartsYour Hand:Six of ClubsTen of Diamonds
Would you like to Hit or Stand? Enter '1' or '0' 0
You chose to stand. Dealer will hit.Dealer's Hand:{ hidden card }Ten of HeartsYour Hand:Six of ClubsTen of DiamondsDealer's Hand:Four of HeartsTen of HeartsTwo of ClubsSix of Hearts
Dealer's Hand = 22Your Hand:Six of ClubsTen of Diamonds
Your Hand = 16
Dealer has BUSTED !Your current balance stands at 101
Would you like to play another hand? Enter '1' or '0' 0
Thanks for playing!$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$Congratulations! You won 101 coins!$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

Python小游戏-Las Vegas Black Jack- CASINO (21点)相关推荐

  1. python编的俄罗斯方块游戏_手把手制作Python小游戏:俄罗斯方块(一)

    手把手制作Python小游戏:俄罗斯方块1 大家好,新手第一次写文章,请多多指教 A.准备工作: 这里我们运用的是Pygame库,因为Python没有内置,所以需要下载 如果没有pygame,可以到官 ...

  2. python简单小游戏代码_一个简单的python小游戏---七彩同心圆

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 用pygame做一个简单的python小游戏-七彩同心圆 玩法:每次点击鼠标时,会以鼠标为圆心,不断 ...

  3. python图形小游戏代码_手把手制作Python小游戏:俄罗斯方块(一)

    手把手制作Python小游戏:俄罗斯方块1 大家好,新手第一次写文章,请多多指教 A.准备工作: 这里我们运用的是Pygame库,因为Python没有内置,所以需要下载 如果没有pygame,可以到官 ...

  4. python小游戏之三

    猜拳游戏 Python代码实现猜拳小游戏 Python代码实现猜拳小游戏_zhangtongyuan0909的博客-CSDN博客_python猜拳游戏代码 用python中类与对象写一个猜拳游戏 用p ...

  5. python小游戏-16行代码实现3D撞球小游戏!-源码下载

    python小游戏-16行代码实现3D撞球小游戏!-源码下载 所属网站分类: 资源下载 > python小游戏 作者:搞笑 链接: http://www.pythonheidong.com/bl ...

  6. python小游戏编程arcade----坦克动画图片合成

    python小游戏编程arcade----坦克动画图片合成 前言 坦克动画图片合成 1.PIL image 1.1 读取文件并转换 1.2 裁切,粘贴 1.3 效果图 1.4 代码实现 2.处理图片的 ...

  7. 用pygame做一个简单的python小游戏---贪吃蛇

    用pygame做一个简单的python小游戏-贪吃蛇 贪吃蛇游戏博客链接:(方法一样,语言不一样) c++贪吃蛇:https://blog.csdn.net/weixin_46791942/artic ...

  8. 用pygame做一个简单的python小游戏---七彩同心圆

    用pygame做一个简单的python小游戏-七彩同心圆 这个小游戏原是我同学python课的课后作业,并不是很难,就简单实现了一下,顺便加强一下pygame库的学习. 玩法:每次点击鼠标时,会以鼠标 ...

  9. 用pygame做一个简单的python小游戏---生命游戏

    用pygame做一个简单的python小游戏-生命游戏 生命游戏(Game of Life) 生命游戏(Game of Life)是剑桥大学约翰·何顿·康威(John Horton Conway)教授 ...

最新文章

  1. vue项目结构php写哪里,Vue-cli搭建项目后目录结构的分析(图文)
  2. CSharp数据库代码生成工具
  3. HDU 2795 Billboard (线段树+贪心)
  4. 指定的命名连接在配置中找不到、非计划用于 EntityClient 提供程序或者无效
  5. Django(part11)--MTV模式及模板
  6. SQL Server各个版本功能比较
  7. 编写歌唱比赛评分_营造园区浓厚文化氛围 三亚崖州湾科技城“最强音”歌唱比赛落幕...
  8. Mybatis懒加载机制
  9. 专访死马:为什么说Egg.js是企业级Node框架
  10. CCF201609-1 最大波动(100分)【序列处理】
  11. [LeetCode]Subsets II生成组合序列
  12. C++ Primer Plus 第二章编程练习
  13. 软件测试的错误优先级,软件测试典型错误
  14. c语言有趣源代码,分享一段有趣的小代码
  15. 局域网在线计算机扫描仪,局域网内也共享扫描仪
  16. 永远感谢雷神-雷霄骅!
  17. 学习Python的Django执行python manage.py startapp myApp创建应用出现的问题
  18. Kali linux 学习笔记(四十一)Web渗透——扫描工具之w3af 2020.3.18
  19. VC实现顶层窗口的透明与实现子窗口的透明【重点:子窗口透明处理】
  20. bibtex类型以及格式要求

热门文章

  1. 银行业灾备及业务连续性管理:从混沌走向清明
  2. C++学习 十五、类继承(1)基类,派生类,访问权限,protected
  3. android 设置背景ah,Ahjesus,
  4. veriog中的latch问题
  5. npm模块之opn使用教程(node **.js直接再浏览器中打开相应的文件)
  6. 《从零开始的记账本开发》第2篇 概要设计
  7. 神经网络中的反向传播
  8. 阿里巴巴大规模神龙裸金属 Kubernetes 集群运维实践
  9. 架构模式-VIPER
  10. dicom文件的处理