本文主要是介绍石头剪刀布游戏摇骰子游戏数字炸弹游戏购票模拟游戏二十一点(21点)游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、石头剪刀布的游戏规矩很简单,首先我们要了解好石头剪刀布是怎么赢和怎么输的,了解这两点我们才可以完成我们的代码和实现功能。(1.石头剪刀布(由电脑随机输出石头剪刀布中其中一个,玩家根据自身选择输入自身判断经过游戏结果)
import random print("1------剪刀") print("2------石头") print("3------布") a=int(input("请输入您要输入的数字:")) print("玩家:",a) b=random.randint(1,3) print("电脑:",b) if a==b:print("玩家与电脑平局") elif (a==1 and b==3)or(a==2 and b==1)or(a==3 and b==2):print("玩家vs电脑")print("玩家胜") elif (a==1 and b==2)or(a==2 and b==3)or(a==3 and b==1):print("玩家vs电脑")print("电脑胜") else:print("")
二、摇骰子相信大家对这个游戏一点都不陌生规则就不多说了直接奔主题(电脑和玩家分别随机生成三个1~6之间的数,分别计算总数和,并对双方的总数和进行判断判断后输出游戏结果)
import random print("玩家vs电脑") wa = random.randint(1, 6) wa1 = random.randint(1, 6) wa2 = random.randint(1, 6) wa3 = wa + wa1 + wa2 print("玩家三个筛子数为:", wa, wa1, wa2) print("总点数为:", wa3) da = random.randint(1, 6) da1 = random.randint(1, 6) da2 = random.randint(1, 6) da3 = da + da1 + da2 print("电脑三个筛子数为:", da, da1, da2) print("总点数为:", da3) if wa3 == da3:print("平局") elif wa3 > da3:print("玩家胜") else:print("电脑胜")
三、数字炸弹(在1-99之间随机生成一个炸弹,由玩家手动输入想要查看的数字,当输入数字大于或小于炸弹的话,由电脑给出相应提示,当输入数字等于炸弹时引爆炸弹,结束游戏)
import random x = random.randint(1, 99) print("炸弹数字小游戏") start = 1 end = 99 while True:num = int(input("输入{}-{}之间的整数:".format(start, end)))if num > x:end = numprint("你猜大了")elif num == x:print("你输了,游戏结束!")breakelif num < x:start = numprint("你猜小了")computer = random.randint(start, end)print("电脑认为炸弹是:", computer)if computer > x:end = computerprint("电脑认为你猜大了")elif computer == x:print("你赢了")print("游戏结束")breakelif computer < x:start = computerprint("电脑认为你猜小了")
四、购票模拟小游戏 (制作购票模拟器,人数由玩家进行输入,购票规则: 平日票价100元,
周末及节假日票价为平日票价20%,儿童票半价,65岁以上老人免费。购票时由电脑提示玩家输入具体人员情况,并经过计算输出最终价格)
print("------购票规则: 平日票价100元\n------周末及节假日票价为平日票价20%\n------儿童票半价\n------65岁以上老人免费。") gr = input("今天是否是周六或者周日") print("请分别输入成人、儿童、65岁老人的数量:") gw = int(input("成人:")) gw1 = int(input("儿童:")) gw2 = int(input("65岁老人:")) gchengren = 100 gertong = gchengren / 2 if (gr == "是"):print("今日票价超出平日120%")gchengrenjia = gchengren * 1.2 * gwgertongjia = gertong * 1.2 * gw1glaoren = gchengren * 0z = gchengrenjia + gertongjia + glaorenprint("成人单价为:", gchengren * 1.2, "元", "\n成人总价格为:", gchengrenjia, "元")print("儿童单价为:", gertong * 1.2, "元", "\n成人总价格为:", gertongjia, "元")print("本次购票总票价为:", z, "元") else:gchengrenjia1 = gchengren * gwgertong1 = gertong * gw1glaoren1 = gchengren * 0z1 = gchengrenjia1 + gertong1 + glaoren1print("成人单价为:", gchengren, "元", "\n成人总价格为:", gchengrenjia1, "元")print("儿童单价为:", gertong, "元", "\n成人总价格为:", gertong1, "元")print("本次购票总票价为:", z1, "元")
五、21点(开局由电脑和玩家分别抽取两张牌,包括2、3、4、5、6、7、8、9、10、J(算10)、Q(算10)《(算10)、A(算1或11分两种情况),每次抽牌后,
由电脑计算出手中牌总数,并根据自身需求选择是否继续抽牌。PS:当手中牌点数总和大于21则失败。提示:可给电脑机器人玩家添加条件判断,
当其手中牌总和在某个区间时选择停止抽牌。当双方都停止抽牌时,开牌,并判断双方点数大小,输出最终结果)
import randomimport timecards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]def player_if_ace(player_cards):if 11 in player_cards:if sum(player_cards) > 21:location = player_cards.index(11)player_cards[location] = 1return player_cardselse:return player_cardselse:return player_cardsdef dealer_control(dealer_cards, player_cards):print('Dealer翻牌了,总和是' + str(sum(dealer_cards)))time.sleep(2)if sum(player_cards) > sum(dealer_cards):while sum(dealer_cards) < 17:dealer_cards.append(random.choice(cards))dealer_cards = player_if_ace(dealer_cards)print('Dealer加牌了,拿到一张' + str(dealer_cards[-1]) + '当前Dealer总和是' + str(sum(dealer_cards)))time.sleep(2)return dealer_cardselse:return dealer_cardsdef add_card(player_cards, dealer_cards):player_choice = input('是否要牌?Y/N')if 'y' in player_choice or 'Y' in player_choice:player_cards.append(random.choice(cards))player_cards = player_if_ace(player_cards)return player_cards, dealer_cards, player_choiceelse:dealer_control(dealer_cards, player_cards)return player_cards, dealer_cards, player_choicedef blackjack(w, l, t):won = wlost = ltie = tplayer_cards = [random.choice(cards), random.choice(cards)]player_cards = player_if_ace(player_cards)dealer_cards = [random.choice(cards), random.choice(cards)]dealer_cards = player_if_ace(dealer_cards)print('******* 游戏开始,你的牌是:' + str(player_cards[0]) + '和'+ str(player_cards[1]) + ' *******\n' + '总和是'+ str(sum(player_cards)) + '\n' + 'Dealer的牌有一张是'+ str(dealer_cards[0]) + '\n')player_cards, dealer_cards, player_choice = add_card(player_cards, dealer_cards)while player_choice == 'y' or player_choice == 'Y':if sum(player_cards) > 21:print('很遗憾,爆牌了,当前总和' + str(sum(player_cards)))lost += 1games_played = int(won) + int(lost) + int(tie)chance = won / games_played * 100time.sleep(2)print('一共进行了' + str(games_played) + '局' + '\n' + '赢的概率是' + str(chance) + '%')return won, lost, tieelse:print('你拿到了一张' + str(player_cards[-1]) + ',当前总和' + str(sum(player_cards)))player_cards, dealer_cards, player_choice = add_card(player_cards, dealer_cards)time.sleep(2)if sum(dealer_cards) > 21:print('Dealer爆牌,恭喜,你赢了')won += 1else:if sum(dealer_cards) > sum(player_cards):print('对不起,你输了')lost += 1elif sum(dealer_cards) == sum(player_cards):print('平局')tie += 1else:print('恭喜,你赢了')won += 1time.sleep(2)games_played = int(won) + int(lost) + int(tie)chance = won / games_played * 100print('一共进行了' + str(games_played) + '局' + '\n' + '赢的概率是' + str(chance) + '%')return won, lost, tiew = 0l = 0t = 0while True:w, l, t = blackjack(w, l, t) else:print("没有该选项,请重新输入")
这篇关于石头剪刀布游戏摇骰子游戏数字炸弹游戏购票模拟游戏二十一点(21点)游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!