''' Auf diese Art kann die Klasse verwendet werden: >>> ttt = TicTacToe() >>> ttt.stones {} >>> ttt.place_x_at(1,1) >>> ttt.place_o_at(0,0) >>> ttt.stones {(1, 1): 'x', (0, 0): 'o'} >>> ttt.x_wins() False >>> ttt.o_wins() False >>> ttt.place_x_at(0,1) >>> ttt.place_o_at(1,0) >>> ttt.place_x_at(2,1) >>> ttt.x_wins() True >>> ttt.o_wins() False >>> ttt.stones {(1, 1): 'x', (0, 0): 'o', (0, 1): 'x', (1, 0): 'o', (2, 1): 'x'} ''' import doctest class TicTacToe: 'A game of tic tac toe.' def __init__(self): # mapping (x,y) to 'x' or 'o' self.stones = {} def place_x_at(self, x, y): assert 0 <= x <= 2 and 0 <= y <= 2 self.stones[(x, y)] = "x" def place_o_at(self, x, y): assert 0 <= x <= 2 and 0 <= y <= 2 self.stones[(x, y)] = "o" def x_wins(self): 'Return whether player x has three in a row.' def o_wins(self): 'Return whether player o has three in a row.' ttt = TicTacToe() ''' o.x xxo x.o ''' for x, y in [(2, 0), (1, 0), (1, 1), (0, 2)]: ttt.place_x_at(x, y) for x, y in [(0, 0), (2, 1), (2, 2)]: ttt.place_o_at(x, y) assert len(ttt.stones) == 7 assert ttt.x_wins() assert not ttt.o_wins() doctest.testmod() # running tests from class documentation