Howdy! I am in a beginners Python class and our current assignment is creating a Tic-Tac-Toe board that can determine a WIN state and that can have a "random play" in which the computer actively tries to beat you. However, I'm unsure of how to do this, as I haven't found much on EZ graphics and Python and it is my first time using these. Do I make functions for those? If so, I'm also unsure of placement, as I have found that order matters.

This is what I have so far:

from ezgraphics import GraphicsWindow

win = GraphicsWindow()

row = [None, None, None]

row[0] = [0, 1, 2]

row[1] = [3, 4, 5]

row[2] = [6, 7, 8]

class TicTacToeBoard:

    def __init__(self, lengthWidth):

        self.size = lengthWidth

        self.win = GraphicsWindow(self.size, self.size)

        self.canvas = self.win.canvas()

        self.game = [ None, None, None, None, None, None, None, None, None ]

    def winTTT(self):

        for i in range(3):

            print(type(self.game[row[i][0]]))

            if (type(self.game[row[i][0]]) == type(self.game[row[i][1]]) ==

type(self.game[row[i][2]])) and (self.game[row[i][0]] != None):

                return True

        return False

    def draw(self):

        self.canvas.clear()

        self.game = [ None, None, None, None, None, None, None, None, None ]

        self.canvas.setOutline("black")

        linebegin = (0, self.size/3)

        lineend = (self.size, self.size/3)

        self.canvas.drawLine(linebegin[0], linebegin[1], lineend[0], lineend[1])

        linebegin = (0, self.size*2/3)

        lineend = (self.size, self.size*2/3)

        self.canvas.drawLine(linebegin[0], linebegin[1], lineend[0], lineend[1])

        linebegin = (self.size/3, 0)

        lineend = (self.size/3, self.size, )

        self.canvas.drawLine(linebegin[0], linebegin[1], lineend[0], lineend[1])

        linebegin = (self.size*2/3, 0)

        lineend = (self.size*2/3, self.size, )

        self.canvas.drawLine(linebegin[0], linebegin[1], lineend[0], lineend[1])

class TTTCrossX:

    def __init__(self, board, nthBox):

        if nthBox == 0:

            self.x1 = board.size/12

            self.y1 = board.size/12

            self.x2 = board.size/6 + board.size/12

            self.y2 = board.size/6 + board.size/12

        if nthBox == 1:

            self.x1 = board.size/3 + board.size/12

            self.y1 = board.size/12

            self.x2 = board.size/3 + board.size/6 + board.size/12

            self.y2 = board.size/6 + board.size/12

        if nthBox == 2:

            self.x1 = board.size/3 + board.size/3 

            self.y1 = board.size/12

            self.x2 = board.size/3 + board.size/3 + board.size/6 + board.size/12

            self.y2 = board.size/6 + board.size/12

        if nthBox == 3:
            self.x1 = board.size/12

            self.y1 = board.size/3 + board.size/12

            self.x2 = board.size/6 + board.size/12

            self.y2 = board.size/3 + board.size/6 + board.size/12

        if nthBox == 4:
            self.x1 = board.size/3 + board.size/12

            self.y1 = board.size/3 + board.size/12

            self.x2 = board.size/3 + board.size/6 + board.size/12

            self.y2 = board.size/3 + board.size/6 + board.size/12

        if nthBox == 5:

            self.x1 = board.size/3 + board.size/3 + board.size/12

            self.y1 = board.size/3 + board.size/12

            self.x2 = board.size/3 + board.size/3 + board.size/6 + board.size/12

            self.y2 = board.size/3 + board.size/6 + board.size/12

        if nthBox == 6:

            self.x1 = board.size/12

            self.y1 = board.size/3 + board.size/3 + board.size/12

            self.x2 = board.size/6 + board.size/12

            self.y2 = board.size/3 + board.size/3 + board.size/6 + board.size/12

        if nthBox == 7:

            self.x1 = board.size/3 + board.size/12

            self.y1 = board.size/3 + board.size/3 + board.size/12

            self.x2 = board.size/3 + board.size/6 + board.size/12

            self.y2 = board.size/3 + board.size/3 + board.size/6 + board.size/12

        if nthBox == 8:

            self.x1 = board.size/3 + board.size/3 + board.size/12

            self.y1 = board.size/3 + board.size/3 + board.size/12

            self.x2 = board.size/3 + board.size/3 + board.size/6 + board.size/12

            self.y2 = board.size/3 + board.size/3 + board.size/6 + board.size/12

        board.canvas.drawLine(self.x1,self.y1, self.x2, self.y2)

        board.canvas.drawLine(self.x2,self.y1, self.x1, self.y2)

class TTTCircle:

   def __init__(self, board, nthBox):

        self.radius = board.size/6

        if nthBox == 0:

           self.center = (board.size/12, board.size/12)

        if nthBox == 1:

           self.center = (board.size/3 + board.size/12, board.size/12)

        if nthBox == 2:

           self.center = (board.size/3 + board.size/3 + board.size/12, board.size/12)

        if nthBox == 3:

            self.center = (board.size/12, board.size/3 + board.size/12 )

        if nthBox == 4:

            self.center = (board.size/3 + board.size/12, board.size/3  = board.size/12)

        if nthBox == 5:

           self.center = (board.size/3 + board.size/3 + board.size/12, board.size/3 + board.size/12)

        if nthBox == 6:

            self.center = (board.size/12, board.size/3 + board.size/3 + board.size/12 )

    if nthBox == 7:

        self.center = (board.size/3 + board.size/12, board.size/3 + board.size/3 + 
board.size/12 )   

    if nthBox == 8:

        self.center = (board.size/3 + board.size/3 + board.size/12, board.size/3 + board.size/3 + board.size/12 )

    board.canvas.drawOval(self.center[0],self.center[1], self.radius, self.radius)

newBoard = TicTacToeBoard(600)

w3 = newBoard.size/3

newBoard.draw()

def whichBox(x, y):

    for n in range(9):

        if (x > (n%3)*w3) and ( x < ((n%3)+1)*w3 ) and (y > int(n/3)*w3) and (y < 
(int(n/3)+1)*w3) :

        return n

i = 0

while i<9:

    pt = newBoard.win.getMouse()

    print(pt, whichBox(pt[0], pt[1]))

    nBox = whichBox(pt[0], pt[1])

    if newBoard.game[nBox] is None:

        if (i % 2 == 0):

            newBoard.game[nBox] = TTTCircle(newBoard, nBox)

        else:

            newBoard.game[nBox] = TTTCrossX(newBoard, nBox)

        i += 1

        if (newBoard.winTTT()):

            break

pt = newBoard.win.getMouse()

newBoard.draw()

print (i)

Did you try to buy the answer on Chegg?

Long link here.

Imagine if your instructor or Professor found that!

Anyhow can you break down the one problem you are having and tackle that?

You might want to adopt a leaner style regarding use of whitespace. Doublespacing your code makes it harder to read, not easier.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.