Ejemplo n.º 1
0
 def draw():
     liste = master.winfo_children()
     count = 0
     for i in liste:
         if i['text'] == 'O' or i['text'] == 'X':
             count += 1
     if count == n**2 and ch.checkIfSomeoneWon(Board) == None:
         return 'draw'
Ejemplo n.º 2
0
    def buttonCommand(event):

        global t
        global turned
        global flag
        global Round
        global winners
        global scoreboard

        def clearBoard(b):
            for x in range(len(b)):
                for t in range(len(b[x])):
                    b[x][t] = '?'

        winner = ch.checkIfSomeoneWon(Board)

        def update():
            global Round
            if cTf() != 1:
                Round += 1
            clearBoard(Board)
            destroyLabels()
            createLabels()
            scoreboard = Label(
                master,
                bg='black',
                fg='white',
                text=('Round ' + str(Round) + '--> X: ' + str(winners['X']) +
                      ' , O: ' + str(winners['O'])))
            scoreboard.grid(row=0, column=n + 1, rowspan=1)

        if winner != None:
            winners[winner] += 1
            update()

        info = event.widget.grid_info()
        dicti = {'X': 'red', 'O': 'blue'}
        coordinates = (info['row'], info['column'])
        x, y = coordinates
        liste = master.winfo_children()
        count = 0

        for widget in liste:
            if widget['text'] == 'X' and widget['bg'] == 'red' or widget[
                    'text'] == 'O' and widget['bg'] == 'blue':
                count += 1
            if (x, y) not in turned:
                turned.update({(x, y): 0})
        if cTf() == 1:
            master.unbind('<Button 1>', buttonCommand)

        if turned[(x, y)] == 0:
            if t == 1:
                flag = 'X'
                t = 0
                Label(master,
                      bg='red',
                      text='X',
                      borderwidth=10,
                      relief="groove",
                      width=30,
                      height=10).grid(row=x, column=y)
                Board[x][y] = 'X'

            else:
                flag = 'O'
                t = 1
                Label(master,
                      bg='blue',
                      text='O',
                      borderwidth=10,
                      relief="groove",
                      width=30,
                      height=10).grid(row=x, column=y)
                Board[x][y] = 'O'
            turned[(x, y)] = 1

        winner = ch.checkIfSomeoneWon(Board)

        if winner != None:
            winners[winner] += 1
            update()

        if draw() == 'draw':
            winners['O'] += 1
            winners['X'] += 1
            update()
Ejemplo n.º 3
0
    def buttonCommand(event):

        global t
        global turned
        global flag
        global Round
        global winners
        global scoreboard
        global moveList

        def clearBoard(b):
            for x in range(len(b)):
                for t in range(len(b[x])):
                    b[x][t] = '?'

        def update():
            global Round
            Round += 1
            clearBoard(Board)
            destroyLabels()
            createLabels()
            scoreboard = Label(
                master,
                bg='black',
                fg='white',
                text=('Round ' + str(Round) + '--> X: ' + str(winners['X']) +
                      ' , O: ' + str(winners['O'])))
            scoreboard.grid(row=0, column=n + 1, rowspan=1)

        winner = ch.checkIfSomeoneWon(Board)

        if winner != None:
            print(1)
            winners[winner] += 1
            liste = []
            for i in copy:
                liste.append(copy[i])
            moveList = liste
            update()

        info = event.widget.grid_info()
        coordinates = (info['row'], info['column'])
        x, y = coordinates
        liste = master.winfo_children()
        count = 0

        for widget in liste:
            if widget['text'] == 'X' and widget['bg'] == 'red' or widget[
                    'text'] == 'O' and widget['bg'] == 'blue':
                count += 1
            if (x, y) not in turned:
                turned.update({(x, y): 0})

        if cTf() == 1:
            master.unbind('<Button 1>', buttonCommand)

        if draw() == 'draw':
            print('draw')
            moveList = copy
            winners['O'] += 1
            winners['X'] += 1
            update()

        if turned[(x, y)] == 0:
            if person == 'X':
                Label(master,
                      bg='red',
                      text='X',
                      borderwidth=10,
                      relief="groove",
                      width=30,
                      height=10).grid(row=x, column=y)
                Board[x][y] = 'X'

                moveList.remove((x, y))
                makeAutoMove(moveList, pc)

            if person == 'O':
                Label(master,
                      bg='blue',
                      text='O',
                      borderwidth=10,
                      relief="groove",
                      width=30,
                      height=10).grid(row=x, column=y)

                Board[x][y] = 'O'
                moveList.remove((x, y))
                makeAutoMove(moveList, pc)

            turned[(x, y)] = 1

        if draw() == 'draw':
            moveList = copy
            winners['O'] += 1
            winners['X'] += 1
            update()

        winner = ch.checkIfSomeoneWon(Board)
        if winner != None:
            print(2)
            winners[winner] += 1
            liste = []
            for i in copy:
                liste.append(copy[i])
            moveList = liste
            update()

        if cTf() == 1:
            master.unbind('<Button 1>', buttonCommand)
Ejemplo n.º 4
0
    if Xinput == 'quit':
        break

    try:
        rowloc, columnloc = int(Xinput[0]), int(Xinput[2])
    except:
        print('Invalid input, you wasted your turn')
    try:
        if board[rowloc][columnloc] == '?':
            board[rowloc][columnloc] = 'X'
        else:
            print('That location is already full, you wasted your turn.')
    except:
        print('That location is not in the board, you wasted your turn.')

    a = ch.checkIfSomeoneWon(board, numberOfRounds)
    if a != '?':
        break
    #O plays
    Oinput = (
        input('It is your turn o, enter your location in format of 1,2: '))
    if Oinput == 'quit':
        break
    try:
        rowloc, columnloc = int(Oinput[0]), int(Oinput[2])
    except:
        print('Invalid input, you wasted your turn')
    try:
        if board[rowloc][columnloc] == '?':
            board[rowloc][columnloc] = 'O'
        else: