Python Forum
How to set turns playing tictactoe?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to set turns playing tictactoe?
#1
I don't know how to set turns: human Vs computer. Based on my code: the computer's movement needs the human to click on any button.


# -*- coding: utf-8 -*-

from tkinter import *
import random

root = Tk()
root.title("Tres en raya")
root.resizable(0, 0)

board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']


def lucky_guess():
    movement = random.randint(0, 8)
    while board[movement] == 'X' or board[movement] == 'O':
        movement = random.randint(0, 8)
    print("The computer has chosen the tile number: ", movement)
    board[movement] = "O"
    if movement == 0:
        tile_0["text"] = "O"
    elif movement == 1:
        tile_1["text"] = "O"
    elif movement == 2:
        tile_2["text"] = "O"
    elif movement == 3:
        tile_3["text"] = "O"
    elif movement == 4:
        tile_4["text"] = "O"
    elif movement == 5:
        tile_5["text"] = "O"
    elif movement == 6:
        tile_6["text"] = "O"
    elif movement == 7:
        tile_7["text"] = "O"
    elif movement == 8:
        tile_8["text"] = "O"


def check_winner():
    if board[0] == 'X' and board[1] == 'X' and board[2] == 'X':
        print("Player's won. Game over.")
        # sys.exit()
    elif board[3] == 'X' and board[4] == 'X' and board[5] == 'X':
        print("Player's won. Game over.")
        # sys.exit()
    elif board[6] == 'X' and board[7] == 'X' and board[8] == 'X':
        print("Player's won. Game over.")
        # sys.exit()
    elif board[0] == 'X' and board[3] == 'X' and board[6] == 'X':
        print("Player's won. Game over.")
        # sys.exit()
    elif board[1] == 'X' and board[4] == 'X' and board[7] == 'X':
        print("Player's won. Game over.")
        # sys.exit()
    elif board[2] == 'X' and board[5] == 'X' and board[8] == 'X':
        print("Player's won. Game over.")
        # sys.exit()
    elif board[0] == 'X' and board[4] == 'X' and board[8] == 'X':
        print("Player's won. Game over.")
        # sys.exit()
    elif board[2] == 'X' and board[4] == 'X' and board[6] == 'X':
        print("Player's won. Game over.")
        # sys.exit()
    if board[0] == 'O' and board[1] == 'O' and board[2] == 'O':
        print("Computer's won. Game over.")
        # sys.exit()
    elif board[3] == 'O' and board[4] == 'O' and board[5] == 'O':
        print("Computer's won. Game over.")
        # sys.exit()
    elif board[6] == 'O' and board[7] == 'O' and board[8] == 'O':
        print("Computer's won. Game over.")
        # sys.exit()
    elif board[0] == 'O' and board[3] == 'O' and board[6] == 'O':
        print("Computer's won. Game over.")
        # sys.exit()
    elif board[1] == 'O' and board[4] == 'O' and board[7] == 'O':
        print("Computer's won. Game over.")
        # sys.exit()
    elif board[2] == 'O' and board[5] == 'O' and board[8] == 'O':
        print("Computer's won. Game over.")
        # sys.exit()
    elif board[0] == 'O' and board[4] == 'O' and board[8] == 'O':
        print("Computer's won. Game over.")
        # sys.exit()
    elif board[2] == 'O' and board[4] == 'O' and board[6] == 'O':
        print("Computer's won. Game over.")
        # sys.exit()


def set_movement(square, n):
    global turn
    if board[n] == " " and turn == 'X':
        square["text"] = "X"
        board[n] = "X"
        check_winner()
        messages.config(text="Computer's turn")
        turn = 'O'
    elif turn == 'O':
        lucky_guess()
        check_winner()
        messages.config(text="Player's turn")
        turn = 'X'
    print(board)


tile_6 = Button(root, text="6", bg='gray', fg='white', height=4, width=8,
                command=lambda: set_movement(tile_6, 6))
tile_6.grid(row=0, column=0)

tile_7 = Button(root, text="7", bg='gray', fg='white', height=4, width=8,
                command=lambda: set_movement(tile_7, 7))
tile_7.grid(row=0, column=1)

tile_8 = Button(root, text="8", bg='gray', fg='white', height=4, width=8,
                command=lambda: set_movement(tile_8, 8))
tile_8.grid(row=0, column=2)

tile_3 = Button(root, text="3", bg='gray', fg='white', height=4, width=8,
                command=lambda: set_movement(tile_3, 3))
tile_3.grid(row=1, column=0)

tile_4 = Button(root, text="4", bg='gray', fg='white', height=4, width=8,
                command=lambda: set_movement(tile_4, 4))
tile_4.grid(row=1, column=1)

tile_5 = Button(root, text="5", bg='gray', fg='white', height=4, width=8,
                command=lambda: set_movement(tile_5, 5))
tile_5.grid(row=1, column=2)

tile_0 = Button(root, text="0", bg='gray', fg='white', height=4, width=8,
                command=lambda: set_movement(tile_0, 0))
tile_0.grid(row=2, column=0)

tile_1 = Button(root, text="1", bg='gray', fg='white', height=4, width=8,
                command=lambda: set_movement(tile_1, 1))
tile_1.grid(row=2, column=1)

tile_2 = Button(root, text="2", bg='gray', fg='white', height=4, width=8,
                command=lambda: set_movement(tile_2, 2))
tile_2.grid(row=2, column=2)

messages = Label(root, text="--------------")
messages.grid(row=3, columnspan=4)


choice = random.choice('XO')
beginning = choice, "'s turn."
messages.config(text=beginning)
turn = choice

root.mainloop()
Reply


Messages In This Thread
How to set turns playing tictactoe? - by Mariano - Apr-10-2019, 10:41 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020