Search code examples
pythontic-tac-toe

Error in Unbeatable Tic Tac Toe


While going through the code for Unbeatable Tic Tac Toe Bot I found this code inside a function.

def __init__(self,other=None):
    self.player = 'X'
    self.opponent = 'O'
    self.empty = '.'
    self.size = 3
    self.fields = {} # A dictionary
    for y in range(self.size):
        for x in range(self.size):
            self.fields[x,y] = self.empty

The fields attribute represents a dictionary. What does self.fields[x,y] represent? Does x represent a column and y represent a row?


Solution

  • In this context, x,y is a tuple, so it's the same as:

    self.fields[(x,y)] = self.empty
    

    It's equivalent to:

    k = (x, y)
    self.fields[k] = self.empty