Let's code a TIC TAC TOE game in python! ⭕

preview_player
Показать описание
python tic tac toe game tutorial example explained

#python #tictactoe #game

# ********************************************************
# Python Tic Tac Toe game
# ********************************************************

from tkinter import *
import random

def next_turn(row, column):

global player

if buttons[row][column]['text'] == "" and check_winner() is False:

if player == players[0]:

buttons[row][column]['text'] = player

if check_winner() is False:
player = players[1]

elif check_winner() is True:

elif check_winner() == "Tie":

else:

buttons[row][column]['text'] = player

if check_winner() is False:
player = players[0]

elif check_winner() is True:

elif check_winner() == "Tie":

def check_winner():

for row in range(3):
if buttons[row][0]['text'] == buttons[row][1]['text'] == buttons[row][2]['text'] != "":
buttons[row][0].config(bg="green")
buttons[row][1].config(bg="green")
buttons[row][2].config(bg="green")
return True

for column in range(3):
if buttons[0][column]['text'] == buttons[1][column]['text'] == buttons[2][column]['text'] != "":
buttons[0][column].config(bg="green")
buttons[1][column].config(bg="green")
buttons[2][column].config(bg="green")
return True

if buttons[0][0]['text'] == buttons[1][1]['text'] == buttons[2][2]['text'] != "":
buttons[0][0].config(bg="green")
buttons[1][1].config(bg="green")
buttons[2][2].config(bg="green")
return True

elif buttons[0][2]['text'] == buttons[1][1]['text'] == buttons[2][0]['text'] != "":
buttons[0][2].config(bg="green")
buttons[1][1].config(bg="green")
buttons[2][0].config(bg="green")
return True

elif empty_spaces() is False:

for row in range(3):
for column in range(3):
buttons[row][column].config(bg="yellow")
return "Tie"

else:
return False

def empty_spaces():

spaces = 9

for row in range(3):
for column in range(3):
if buttons[row][column]['text'] != "":
spaces -= 1

if spaces == 0:
return False
else:
return True

def new_game():

global player

for row in range(3):
for column in range(3):
buttons[row][column].config(text="",bg="#F0F0F0")

window = Tk()
players = ["x","o"]
buttons = [[0,0,0],
[0,0,0],
[0,0,0]]

label = Label(text=player + " turn", font=('consolas',40))

reset_button = Button(text="restart", font=('consolas',20), command=new_game)

frame = Frame(window)

for row in range(3):
for column in range(3):
buttons[row][column] = Button(frame, text="",font=('consolas',40), width=5, height=2,
command= lambda row=row, column=column: next_turn(row,column))
buttons[row][column].grid(row=row,column=column)

# ********************************************************

Bro Code merch store 👟 :
===========================================================
===========================================================

music credits 🎼 :
===========================================================
Creative Commons — Attribution-ShareAlike 3.0 Unported— CC BY-SA 3.0
===========================================================
Рекомендации по теме
Комментарии
Автор

#
# Python Tic Tac Toe game
#

from tkinter import *
import random

def next_turn(row, column):

global player

if buttons[row][column]['text'] == "" and check_winner() is False:

if player == players[0]:

buttons[row][column]['text'] = player

if check_winner() is False:
player = players[1]
turn"))

elif check_winner() is True:
wins"))

elif check_winner() == "Tie":
label.config(text="Tie!")

else:

buttons[row][column]['text'] = player

if check_winner() is False:
player = players[0]
turn"))

elif check_winner() is True:
wins"))

elif check_winner() == "Tie":
label.config(text="Tie!")

def check_winner():

for row in range(3):
if buttons[row][0]['text'] == buttons[row][1]['text'] == buttons[row][2]['text'] != "":



return True

for column in range(3):
if buttons[0][column]['text'] == buttons[1][column]['text'] == buttons[2][column]['text'] != "":



return True

if buttons[0][0]['text'] == buttons[1][1]['text'] == buttons[2][2]['text'] != "":



return True

elif buttons[0][2]['text'] == buttons[1][1]['text'] == buttons[2][0]['text'] != "":



return True

elif empty_spaces() is False:

for row in range(3):
for column in range(3):

return "Tie"

else:
return False


def empty_spaces():

spaces = 9

for row in range(3):
for column in range(3):
if buttons[row][column]['text'] != "":
spaces -= 1

if spaces == 0:
return False
else:
return True

def new_game():

global player

player = random.choice(players)

label.config(text=player+" turn")

for row in range(3):
for column in range(3):
buttons[row][column].config(text="", bg="#F0F0F0")


window = Tk()
window.title("Tic-Tac-Toe")
players = ["x", "o"]
player = random.choice(players)
buttons = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]

label = Label(text=player + " turn", font=('consolas', 40))
label.pack(side="top")

reset_button = Button(text="restart", font=('consolas', 20), command=new_game)


frame = Frame(window)
frame.pack()

for row in range(3):
for column in range(3):
buttons[row][column] = Button(frame, text="", font=('consolas', 40), width=5, height=2,
command= lambda row=row, column=column: next_turn(row, column))
buttons[row][column].grid(row=row, column=column)

window.mainloop()

#

BroCodez
Автор

Bro you just explained tic tac toe in 21 min to a complete beginner. Hats off! mate

adaboykrisiboy
Автор

wow very interesting and educative thank you

amenkalai
Автор

My first Python project, Thanks a lot.

masoodrezaei
Автор

Very entertaining and also quite understandable. Quality content! Keep it up!

rafailivanov
Автор

Outstanding explanation given by you bro, easy to understand. And have successfully made this TicTacToe game. Thanks to you again... (HOPE TO SEE MORE AMAZING VIDEOS FROM YOU)

saifsyed
Автор

I love this channel it helps me so much learning python and other stuff

tubacego
Автор

Rad vid bro! Keep up the cool work. :)

bishnudeyvivekanonda
Автор

thx for the game Bro Code always the best ;)

luxury
Автор

nice work. Truly inspirational, keep up the good work

kevinka
Автор

I can't thank you enough you are the best :)

vescle
Автор

Bro Code thanks You are a great man I know I am late but you just made my carrier! I am really happy to get this course for free and not paying that stupid 199$ for python course now I can really apply for jobs but of-course after some practice

jainendrarout
Автор

I really like the way you explained : Simple, clean and beautiful.

celestinkembe
Автор

Thank you bro about this good video
(:

Nothing..
Автор

Bro, you're a such great and benevolent programmer!!! God good you are!!!

LevitateTheCore
Автор

Thank you so much Bro Code! I learned so much new information from this tutorial!

nickarkhipov
Автор

only missing the computer player AI trying to win against us. or alternatively adding 2 different controller inputs like mouse and keyboard for each player. example one key to chose which button position and Enter to push it, the the mouse thing will remain the same. I will try to implement this myself.

davidcalebpaterson
Автор

Thanks, bro code. I really appreciate it.

oops
Автор

Wow! You actually encouraged me to do python even when I was not that into it❤ Also got a lot of videos left to go!

navyadesai
Автор

you re very good teacher thanks a lot 👏👏😘

yos