python比大小游戏_python 抽牌比大小游戏代码

这是上周老师布置的作业,在索求各种网络资源无果的情况下,我们几个小白一起自己打出了代码,交了作业,老师表扬了我们的代码,说最简单易懂,最便捷,比班里其他几个大神的code还要好一些,还是蛮有成就感的。贴出来给大家参考。

首先,是题目:

Consider a modified version of the card game “War”. In this version, there are two players who are dealt 10 cards each, numbered 1 through 10. In each round of the game, player A and player B play a random card from their deck. The player who plays the higher card takes back their own card, as well as the card of their opponent. In the event of a tie, each player gets their card back and it goes back into their pile randomly. The game is over when one player has no cards left. 就是每个人随机抽取1-10中的一个数字,比大小,大的那一方有权利收回自己的牌,并且把对方的牌据为己有。平局的情况下用flip coin扔硬币的方式来角逐。直到谁的牌先没有了,谁就输了。

问题:

a. Write a series of functions that simulates the game and calculates the number of rounds each game takes. Run your program 10,000 times and record each result. Time how long the simulation takes. 要设计代码来让他自动运行10000次,并记录时间。

b. Plot a histogram of the distribution of the number of rounds played. Make sure there is a

title, legend, and labeled axes. 把运行的10000次plot出来。

c.Re-run the program for a game with 25 distinct cards. How long did this simulation take?

Does it vary linearly with respect to the 10 card game? 这次换成从1-25中抽牌,做同样的plot,看区别。

代码如下:

##########################a

import numpy as np

import matplotlib.pyplot as plt

import random

from random import randint

from timeit import timeit

import time

start_time = time.time()

game_round_list = []

def comparison():

playerA=list(range(1,11))

playerB=list(range(1,11))

game_round=0

while playerA and playerB:

a=random.choice(playerA)

b=random.choice(playerB)

if a==b:

coin_flip=random.randint(0,1)

if coin_flip==1:

playerB.remove(b)

playerA.append(b)

elif coin_flip==0:

playerA.remove(a)

playerB.append(a)

elif a>b:

playerB.remove(b)

playerA.append(b)

elif a

playerA.remove(a)

playerB.append(a)

game_round+=1

game_round_list.append(game_round)

print('The Number of Rounds is:',game_round)

for i in range (10000):

comparison()

end_time = time.time()

print("Total execution time: {}".format(end_time - start_time))

print(game_round_list)

##########################b

import numpy as np

import matplotlib.pyplot as plt

plt.hist(game_round_list,bins=10)

plt.ylabel('Times')

plt.xlabel('Rounds')

plt.title('WAR')

##########################c

import time

start_time = time.time()

game_round_list = []

def comparison():

playerA=list(range(1,26))

playerB=list(range(1,26))

game_round=0

while playerA and playerB:

a=random.choice(playerA)

b=random.choice(playerB)

if a==b:

coin_flip=random.randint(0,1)

if coin_flip==1:

playerB.remove(b)

playerA.append(b)

elif coin_flip==0:

playerA.remove(a)

playerB.append(a)

elif a>b:

playerB.remove(b)

playerA.append(b)

elif a

playerA.remove(a)

playerB.append(a)

game_round+=1

game_round_list.append(game_round)

print('The Number of Rounds is:',game_round)

for i in range (10000):

comparison()

end_time = time.time()

print("Total execution time: {}".format(end_time - start_time))

print(game_round_list)

import numpy as np

import matplotlib.pyplot as plt

plt.hist(game_round_list,bins=10)

plt.ylabel('Times')

plt.xlabel('Rounds')

plt.title('WAR')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值