How to set turns playing tictactoe? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: How to set turns playing tictactoe? (/thread-17438.html) |
How to set turns playing tictactoe? - Mariano - Apr-10-2019 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() RE: How to set turns playing tictactoe? - joe_momma - Apr-11-2019 You can Use: while True: if gameWon(Boolean): break # or popup dialog to restart else: #players turn #computers turnAl Swiegart has written many free books on python and games. In some linux distributions his games are installed by default and the code is open sourced available to read and tweek. Making Games book can be found here: https://inventwithpython.com/makinggames.pdf at the end of the book he has several human vs computer games with code. You are on the right track keep going, Joe |