Next: 7.3 Defining user setups Up: 7.2 Programming user strategies Previous: 7.2.1 Preface Contents

### 7.2.2 The Strategy class

Any strategy in the game must be derived from class Strategies.Strategy. This class defines the two methods firstMove(self) and nextMove(self, myMoves, opMoves). Both methods must return either 0 or 1, where 0 means ``defection'' and 1 stands for ``cooperation''. firstMove does not take any parameters except ``self'', while nextMove gets the list of its own moves during the previous rounds of the current match as well as a list of its opponents previous moves. Both lists are list of zeros and ones. Implementing your own user strategies is pretty easy now: Simply derive a class from class Strategies.Strategy and define the methods firstMove and nextMove, nothing else is needed, neither a constructor, nor is it necessary to worry about a name for the strategy, because the class name is automatically used as the strategies' name. All state saving variables of your class must be reset in method firstmove! This is necessary, because the same strategy object is used in all matches of the tournament. Here is a simple example of a custom strategy class:

    class LesserTFT(Strategy):
        """Retailiate only when not having retailiated in
        the last round already.
        """
        def firstMove(self):
            return 1  # start friendly
        def nextMove(self, myMoves, opMoves):
            if opMoves[-1] == 0 and myMoves[-1] != 0:
                return 0  # retailiate
            else:
                return 1  # cooperate

    # Do not forget to instantiate your class, otherwise
    # it will not appear among the available strategies!

    lesser_tft = LesserTFT()

Next: 7.3 Defining user setups Up: 7.2 Programming user strategies Previous: 7.2.1 Preface Contents