讀古今文學網 > 父與子的編程之旅:與小卡特一起學Python > 第26章 >

第26章

動手試一試

1. 這是一個可以打敗 CivcleAI 的機器人:

class AI:    def __init__(self):self.isFirstTurn = True    def turn(self):if self.isFirstTurn:    self.robot.turnLeft    self.isFirstTurn = Falseelif self.robot.lookInFront == "bot":    self.robot.attackelse:    self.robot.doNothing  

這個機器人的策略是等待 CircleAI 繞圈,等它在前方時攻擊它。我能寫出這個機器人,是因為我知道CircleAI的工作方式,它並不能打敗其他的機器人。如我在本章正文中所說,要創建一個必勝的機器人是非常非常困難的,尤其是在連對手是什麼機器人都不知道的情況下。