Python僵尸骰子机器人

Python编程快速上手——让繁琐工作自动化 第2版》

6.11 实践项目
6.11.2 僵尸骰子机器人输出

项目介绍详见书籍

代码

此代码部分为书中代码,部分为书中给出的要求自己编写机器人的代码(第1-5种情况对应书中的五种机器人)

import zombiedice
import random


class MyZombie:
    def __init__(self, name):
        # All zombies must have a name:
        self.name = name

    def turn(self, gameState):
        # gameState is a dict with info about the current state of the game.
        # You can choose to ignore it in your code.

        diceRollResults = zombiedice.roll()  # first roll
        # roll() returns a dictionary with keys 'brains', 'shotgun', and
        # 'footsteps' with how many rolls of each type there were.
        # The 'rolls' key is a list of (color, icon) tuples with the
        # exact roll result information.
        # Example of a roll() return value:
        # {'brains': 1, 'footsteps': 1, 'shotgun': 1,
        #  'rolls': [('yellow', 'brains'), ('red', 'footsteps'),
        #            ('green', 'shotgun')]}

        # REPLACE THIS ZOMBIE CODE WITH YOUR OWN:
        # 0.当脑子小于2时,才会继续丢骰子
        brains = 0
        while diceRollResults is not None:
            brains += diceRollResults['brains']

            if brains < 2:
                diceRollResults = zombiedice.roll() # roll again
            else:
                break

        # # 1.在第一掷之后会随机决定继续还是停止的机器人
        # # 和这行代码功能一致 zombiedice.examples.RandomCoinFlipZombie(name='Random'),
        # while diceRollResults and random.randint(0, 1) == 0:
        #     diceRollResults = zombiedice.roll()

        # # 2.在掷出两个大脑后停止掷骰子的机器人
        # # 当脑子大于等于2时,停止继续丢骰子
        # brains = 0
        # while diceRollResults is not None:
        #     brains += diceRollResults['brains']
        #
        #     if brains >= 2:
        #         break
        #     else:
        #         diceRollResults = zombiedice.roll() # roll again

        # # 3.在掷出两把霰弹枪后停止掷骰子的机器人
        # # 当霰弹枪大于等于2时,停止继续丢骰子
        # shotguns = 0
        # while diceRollResults is not None:
        #     shotguns += diceRollResults['shotgun']
        #
        #     if shotguns >= 2:
        #         break
        #     else:
        #         diceRollResults = zombiedice.roll() # roll again

        # # 4.开始就决定将骰子掷1~4次的机器人,但如果掷出两把霰弹枪,它将提前停止
        # # 开始随机将骰子掷1~4次,当霰弹枪大于等于2时,停止继续丢骰子
        # shotguns = 0
        # for i in range(random.randint(1, 1)):  # 将randint(1,4)改为 randint(1,1) 可以查看仅掷一次的结果
        #     shotguns += diceRollResults['shotgun']
        #
        #     if shotguns >= 2:
        #         break
        #     else:
        #         diceRollResults = zombiedice.roll()  # roll again
        #
        #     # 对比无限制,仅随机循环次数
        #     # diceRollResults = zombiedice.roll() # roll again

        # # 5.掷出的霰弹枪多于大脑后停止掷骰子的机器人
        # # 当霰弹枪大于大脑时,停止继续丢骰子
        # shotguns = 0
        # brains = 0
        # while diceRollResults is not None:
        #     shotguns += diceRollResults['shotgun']
        #     brains += diceRollResults['brains']
        # 
        #     if shotguns > brains:
        #         break
        #     else:
        #         diceRollResults = zombiedice.roll()  # roll again


zombies = (
    zombiedice.examples.RandomCoinFlipZombie(name='Random'),
    zombiedice.examples.RollsUntilInTheLeadZombie(name='Until Leading'),
    zombiedice.examples.MinNumShotgunsThenStopsZombie(name='Until 2 Shotguns', minShotguns=2),
    # 实现功能的替换可以像这样重写源代码的模块中的
    zombiedice.examples.MinNumShotgunsThenStopsZombie(name='Until 1 Shotgun', minShotguns=1),
    MyZombie(name='test5'),
    # # 添加name只是在调用此脚本的代码,当然可以修改可替换的部分
    # MyZombie(name='doubao'),
    # Add any other zombie players here.
)

# Uncomment one of the following lines to run in CLI or Web GUI mode:
# zombiedice.runTournament(zombies=zombies, numGames=1000)
zombiedice.runWebGui(zombies=zombies, numGames=1000)
运行结果

直接贴图

test0——书中原代码

Tournament of 1000 games started...
Tournament results:
Wins:
    Until 2 Shotguns  406
       Until Leading  227
     Until 1 Shotgun  174
               test0  101
              Random   76
Ties:
     Until 1 Shotgun   11
       Until Leading    9
    Until 2 Shotguns    8
               test0    4
              Random    0

test1——在第一掷之后会随机决定继续还是停止的机器人

Tournament of 1000 games started...
Tournament results:
Wins:
    Until 2 Shotguns  418
       Until Leading  218
     Until 1 Shotgun  173
               test1   94
              Random   87
Ties:
    Until 2 Shotguns    8
     Until 1 Shotgun    8
               test1    3
              Random    1
       Until Leading    1

 test2——在掷出两个大脑后停止掷骰子的机器人

Tournament of 1000 games started...
Tournament results:
Wins:
    Until 2 Shotguns  384
       Until Leading  210
     Until 1 Shotgun  197
               test2  123
              Random   81
Ties:
              Random    3
     Until 1 Shotgun    3
       Until Leading    2
    Until 2 Shotguns    1
               test2    1

test3—— 在掷出两把霰弹枪后停止掷骰子的机器人

Tournament of 1000 games started...
Tournament results:
Wins:
               test3  322
    Until 2 Shotguns  311
       Until Leading  184
     Until 1 Shotgun  120
              Random   54
Ties:
    Until 2 Shotguns    6
               test3    6
     Until 1 Shotgun    3
       Until Leading    2
              Random    1

test4—— 开始就决定将骰子掷1~4次的机器人,但如果掷出两把霰弹枪,它将提前停止

Tournament of 1000 games started...
Tournament results:
Wins:
    Until 2 Shotguns  375
       Until Leading  212
               test4  194
     Until 1 Shotgun  145
              Random   69
Ties:
               test4    5
              Random    2
    Until 2 Shotguns    2
     Until 1 Shotgun    1
       Until Leading    0

test5—— 掷出的霰弹枪多于大脑后停止掷骰子的机器人

Tournament of 1000 games started...
Tournament results:
Wins:
    Until 2 Shotguns  461
       Until Leading  250
     Until 1 Shotgun  191
              Random   95
               test5    0
Ties:
    Until 2 Shotguns    3
       Until Leading    2
     Until 1 Shotgun    1
              Random    0
               test5    0

总结

这个游戏的规则没太看懂,但是代码照葫芦画瓢地写出来了,第一种情况对应

zombiedice.examples.RandomCoinFlipZombie(name='Random') 代码

第三种情况对应

zombiedice.examples.MinNumShotgunsThenStopsZombie(name='Until 2 Shotguns', minShotguns=2)代码

其他几种情况未找到正确的运行代码或者结果,如果代码有问题或老师们有想法欢迎留言讨论。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值