Python Tic Tac Toe game ⭕

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ค. 2024
  • 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]
    label.config(text=(players[1]+" turn"))
    elif check_winner() is True:
    label.config(text=(players[0]+" wins"))
    elif check_winner() == "Tie":
    label.config(text="Tie!")
    else:
    buttons[row][column]['text'] = player
    if check_winner() is False:
    player = players[0]
    label.config(text=(players[0]+" turn"))
    elif check_winner() is True:
    label.config(text=(players[1]+" 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'] != "":
    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
    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)
    reset_button.pack(side="top")
    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()
    ********************************************************
    Bro Code merch store 👟 :
    ===========================================================
    teespring.com/stores/bro-code-5
    ===========================================================
    music credits 🎼 :
    ===========================================================
    Up In My Jam (All Of A Sudden) by - Kubbi / kubbi
    Creative Commons - Attribution-ShareAlike 3.0 Unported- CC BY-SA 3.0
    Free Download / Stream: bit.ly/2JnDfCE
    Music promoted by Audio Library • Up In My Jam (All Of A...
    ===========================================================
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 168

  • @BroCodez
    @BroCodez  3 ปีที่แล้ว +81

    # ********************************************************
    # 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]
    label.config(text=(players[1]+" turn"))
    elif check_winner() is True:
    label.config(text=(players[0]+" wins"))
    elif check_winner() == "Tie":
    label.config(text="Tie!")
    else:
    buttons[row][column]['text'] = player
    if check_winner() is False:
    player = players[0]
    label.config(text=(players[0]+" turn"))
    elif check_winner() is True:
    label.config(text=(players[1]+" 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'] != "":
    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
    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)
    reset_button.pack(side="top")
    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()
    # ********************************************************

    • @Alex-ur3vt
      @Alex-ur3vt 3 ปีที่แล้ว +1

      epic video bro

    • @hackerstech4025
      @hackerstech4025 3 ปีที่แล้ว

      Love you

    • @justairman
      @justairman 2 ปีที่แล้ว

      @Bro code how do we save the data of the wins

    • @rockyelk169
      @rockyelk169 ปีที่แล้ว

      @@justairman hey man not sure if you are still in need of this but you can use the command "global"

    • @S_M_HABIBUR_RAHMAN
      @S_M_HABIBUR_RAHMAN ปีที่แล้ว

      Op video bro code

  • @adaboykrisiboy36
    @adaboykrisiboy36 ปีที่แล้ว +32

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

    • @user-eq1bl3lt3r
      @user-eq1bl3lt3r 9 หลายเดือนก่อน +2

      dude you have the same profile pic like me

  • @saifsyed590
    @saifsyed590 2 ปีที่แล้ว +9

    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)

  • @rafailivanov814
    @rafailivanov814 2 ปีที่แล้ว +9

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

  • @kevinka9670
    @kevinka9670 3 ปีที่แล้ว +4

    nice work. Truly inspirational, keep up the good work

  • @celestinkembe3331
    @celestinkembe3331 ปีที่แล้ว +3

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

  • @navyadesai67
    @navyadesai67 9 หลายเดือนก่อน +1

    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!

  • @bishnudeyvivekanonda7028
    @bishnudeyvivekanonda7028 11 หลายเดือนก่อน

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

  • @jainendrarout8167
    @jainendrarout8167 2 ปีที่แล้ว +2

    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

  • @monwil3296
    @monwil3296 3 ปีที่แล้ว +10

    Hyo Bro ❤️💯. We do appreciate your hard work. Young programmers are building their mental Database thanks to you 👏👏👏👏

  • @amenkalai8442
    @amenkalai8442 10 หลายเดือนก่อน

    wow very interesting and educative thank you

  • @nickarkhipov3684
    @nickarkhipov3684 ปีที่แล้ว +1

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

  • @eduardodelgado3973
    @eduardodelgado3973 2 ปีที่แล้ว +1

    Excelent explanation, thank you.

  • @davidbandini3484
    @davidbandini3484 ปีที่แล้ว +1

    Thank you so much for the video

  • @tubacego6893
    @tubacego6893 3 หลายเดือนก่อน +1

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

  • @sirikondavenkat5541
    @sirikondavenkat5541 3 ปีที่แล้ว +2

    This is Awsome!

  • @MFaiqVaince
    @MFaiqVaince หลายเดือนก่อน +1

    great video

  • @d.ashley879
    @d.ashley879 ปีที่แล้ว

    Fantastic video!

  • @user-ql5be3pz3f
    @user-ql5be3pz3f 3 ปีที่แล้ว +2

    you are best!!!

  • @yos5770
    @yos5770 ปีที่แล้ว

    you re very good teacher thanks a lot 👏👏😘

  • @Nothing..
    @Nothing.. 2 ปีที่แล้ว

    Thank you bro about this good video
    (:

  • @saifulislamishad1222
    @saifulislamishad1222 2 ปีที่แล้ว +1

    Big fan 🖤

  • @panoskoutsogiannis799
    @panoskoutsogiannis799 10 หลายเดือนก่อน

    very nice video!!

  • @K-tf5ph
    @K-tf5ph ปีที่แล้ว

    Thanks my dear Bro 💯👑 Could you also do the minesweeper?

  • @luxury8884
    @luxury8884 11 หลายเดือนก่อน

    thx for the game Bro Code always the best ;)

  • @user-gd4ip1bn2y
    @user-gd4ip1bn2y 6 หลายเดือนก่อน

    Thank You! Explained well

  • @imadiman528
    @imadiman528 10 หลายเดือนก่อน

    Thanks a bunch you're great 👏

  • @vtWub
    @vtWub ปีที่แล้ว +2

    Great video, very easy to go with and I understood most of what went over, and just searched whatever I didn't know. But 1 question, I am currently using pycharm on my macbook and everything works except the part where it could show the winner, meaning the background does not change to green, or yellow when a tie. Is there a reason to why this is happening, and if there is a work around to this?

  • @TestDetails
    @TestDetails 9 หลายเดือนก่อน

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

  • @AaronProgZ
    @AaronProgZ 8 หลายเดือนก่อน

    Thanks for making this video!

  • @shahzodsayfiddinov9510
    @shahzodsayfiddinov9510 3 ปีที่แล้ว +2

    Thank you Bro!

  • @t34bravo
    @t34bravo 3 หลายเดือนก่อน

    This is an awesome tutorial. Thanks!

  • @davidcalebpaterson7101
    @davidcalebpaterson7101 2 ปีที่แล้ว +4

    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.

  • @hbngamer5776
    @hbngamer5776 หลายเดือนก่อน

    Good video taught me alot!

  • @Hema_99
    @Hema_99 ปีที่แล้ว +1

    Hi, thanks for the video, it really helped so thank you so much for that and also I have a doubt, what platform should I use if I want to convert this script into a mobile application.

  • @kanime9535
    @kanime9535 ปีที่แล้ว

    Thank you for the video bro!

  • @hassanfiras3225
    @hassanfiras3225 9 หลายเดือนก่อน

    a real great tutorial ty bro

  • @Philocalyleena
    @Philocalyleena ปีที่แล้ว

    Thank you so much sir.

  • @Shadoww0
    @Shadoww0 11 หลายเดือนก่อน

    thank you for the vid

  • @sjthedarkcreation9744
    @sjthedarkcreation9744 2 ปีที่แล้ว

    Thanks sir

  • @THEMINECRAFTWARDEN4279
    @THEMINECRAFTWARDEN4279 ปีที่แล้ว

    Woah! That’s good video😂😂😂

  • @imslim85
    @imslim85 11 หลายเดือนก่อน

    Thank you very much !

  • @user-mq6ju8hl1r
    @user-mq6ju8hl1r 9 หลายเดือนก่อน

    this helped me thank you :)

  • @Ehsan.Amirii
    @Ehsan.Amirii 2 ปีที่แล้ว

    great

  • @ee-vi9xs
    @ee-vi9xs ปีที่แล้ว

    nice and gg

  • @ipsxofficial
    @ipsxofficial หลายเดือนก่อน

    Cool

  • @GarnetTheif
    @GarnetTheif ปีที่แล้ว

    awesome he's a great teacher

  • @tips9296
    @tips9296 2 ปีที่แล้ว

    nice

  • @masoodrezaei5999
    @masoodrezaei5999 หลายเดือนก่อน

    My first Python project , Thanks a lot.

  • @mirshodoripov1035
    @mirshodoripov1035 ปีที่แล้ว

    Thank you bro

  • @hackerstech4025
    @hackerstech4025 3 ปีที่แล้ว +2

    😍 you

  • @jaimeescoto2009
    @jaimeescoto2009 ปีที่แล้ว

    bro very excellent video, but just one question if I wanted to play against the machine, what function can I use

  • @aijounohankyou
    @aijounohankyou ปีที่แล้ว

    thank you very much

  • @rawenrebaz7057
    @rawenrebaz7057 ปีที่แล้ว

    thank you bro

  • @mythyfox
    @mythyfox 5 หลายเดือนก่อน

    THX SO MUCH

  • @cherishkansara7851
    @cherishkansara7851 10 หลายเดือนก่อน +1

    I am a fellow bro from now on ❤

  • @logeshwaran3016
    @logeshwaran3016 3 ปีที่แล้ว +2

    Hello bro

  • @albatrozeditz
    @albatrozeditz ปีที่แล้ว

    thank you

  • @oceangamer7875
    @oceangamer7875 2 หลายเดือนก่อน +1

    winner winner chicken dinner

  • @souravsarkar6107
    @souravsarkar6107 2 ปีที่แล้ว

    learn new thing thank you

  • @murilosilvestre7736
    @murilosilvestre7736 3 ปีที่แล้ว +2

    great job, bro, thank you!

  • @ccelsiuss482
    @ccelsiuss482 ปีที่แล้ว

    underrated af

  • @basic-info-
    @basic-info- 10 หลายเดือนก่อน

    thx man true gigachad

  • @akshayak5466
    @akshayak5466 9 หลายเดือนก่อน

    bro you are genius

  • @amadeo5813
    @amadeo5813 9 หลายเดือนก่อน +1

    Hello, What can I do to make the squares stay painted when I hover the mouse over them?

  • @isagovfx
    @isagovfx 2 ปีที่แล้ว +3

    im on mac using sublime but i cant seem to figure out why the bg colors arent appearing for me. i get no errors however i get no colors froms the inning or ties

    • @yatinkundra
      @yatinkundra 4 หลายเดือนก่อน

      from tkmacosx import Button as BT
      use this and then replace Button with BT

  • @obiarne
    @obiarne 6 หลายเดือนก่อน

    Very nice tutorial, but is there a way to add CSS to this project? I'm more familiar with that language for styling of the buttons, background color etc. compared to (bg = "orange")

  • @ianbarddal7294
    @ianbarddal7294 29 วันที่ผ่านมา

    You're a true CHAD!

  • @eyobyishak5233
    @eyobyishak5233 6 หลายเดือนก่อน

    thank you it work correctly

  • @eddyedutz
    @eddyedutz 2 ปีที่แล้ว +1

    I see some guys around trying to implement AIs already and here I come with the question to pass the exam lol:
    How can I add a score to this? I've managed to add a label for the score and I assume I need some variables where we would need to add the wins.
    Can someone drop a comment if you figured it out?

  • @rajeevkumarjha8748
    @rajeevkumarjha8748 หลายเดือนก่อน

    genius

  • @S_M_HABIBUR_RAHMAN
    @S_M_HABIBUR_RAHMAN ปีที่แล้ว

    Op video bro code please relply

  • @Friday_42
    @Friday_42 ปีที่แล้ว +1

    I couldn't get this to work. Very curious, I wonder what went wrong?

  • @arshiaa104
    @arshiaa104 ปีที่แล้ว

    good old days when bro was 21 years old

  • @rowlet8385
    @rowlet8385 17 วันที่ผ่านมา

    my brain... ahhhh

  • @user-hn4ye7dn1r
    @user-hn4ye7dn1r ปีที่แล้ว

    I like this video, a very good tutorial, thanks.😄
    why in this line "reset_button = Button(text="restart",font=('Aharoni',20),command=new_game)" you didn't put () after new_game?

    • @Karim-zu7zg
      @Karim-zu7zg ปีที่แล้ว

      You dont need too

    • @MishaZib86
      @MishaZib86 ปีที่แล้ว

      I used () as in video, and function "new game" didn't work.... but when removed it, all is perfect!

  • @oMqngo
    @oMqngo 2 ปีที่แล้ว

    What if i want it to be solo and multiplayer (so you can press a button to choose if you want to play against someone or against the comuter) how would you do that?

    • @vythir4245
      @vythir4245 2 ปีที่แล้ว

      You would need an AI for that, which would be even harder to code than the game itself...

  • @Amir_Plays_non_stop
    @Amir_Plays_non_stop 3 ปีที่แล้ว +2

    1 more video to finish

  • @DisturbeD802
    @DisturbeD802 2 ปีที่แล้ว +1

    If it took you 100 lines of code , i will do it in 300 lines and still have bugs in it and finish it in a month

  • @eceabhishekKumarsharma
    @eceabhishekKumarsharma ปีที่แล้ว

    thanku for the source code

  • @mdmufidalam3757
    @mdmufidalam3757 3 หลายเดือนก่อน

    tHANKS aLOT

  • @bigbodyboy2193
    @bigbodyboy2193 ปีที่แล้ว

    what code editor do you use?

  • @edwardplayzzz2675
    @edwardplayzzz2675 2 หลายเดือนก่อน

    is it different for mac? colours are not changing after win

  • @BMF_20953
    @BMF_20953 ปีที่แล้ว

    11:46

  • @bangpink2392
    @bangpink2392 ปีที่แล้ว

    😅

  • @sidtheepic1103
    @sidtheepic1103 2 หลายเดือนก่อน

    Can someone explain the for loops in this. I understood everything except the for loops😅😅

  • @erinasyasya947
    @erinasyasya947 ปีที่แล้ว

    could you please show how to the scoring? i’ve tried since two days ago and still wrong.

  • @WooGad0Xo
    @WooGad0Xo 2 ปีที่แล้ว +1

    when this :
    buttons[row][column]["text"]
    i know [row][column] get row and column but what is ["text"] does?

    • @braek2766
      @braek2766 2 ปีที่แล้ว +1

      calls all the text from the button.

  • @tridra5714
    @tridra5714 ปีที่แล้ว

    I have dropped a comment down below

  • @user-fv7hd8qs4l
    @user-fv7hd8qs4l 5 หลายเดือนก่อน

    Great video! but I have a question, I don't know why when is time for "o" to play and I select a button to put it and is not working only with "x". Does anyone know why?

  • @stewartk5952
    @stewartk5952 3 ปีที่แล้ว +2

    what is your coding app

  • @samadshead6069
    @samadshead6069 ปีที่แล้ว +1

    hi ive copied the code from the comments but the buttons don't change colour when I run it. its worth noting I'm using a Mac. anyone know why this is?

  • @beldindragon
    @beldindragon ปีที่แล้ว

    Didn't understand what the lambda command did, can you explain it quickly?

  • @Shadoww0
    @Shadoww0 11 หลายเดือนก่อน +1

    bro can you explain why buttons is declared this way
    buttons=[ [0,0,0],
    [0,0,0],
    [0,0,0] ]

    • @Unenough_cabin
      @Unenough_cabin 10 หลายเดือนก่อน

      So, that it is easy to read for US I think

  • @Dr.Penn1
    @Dr.Penn1 2 ปีที่แล้ว

    step 1 to defeating the algorithm complete.

    • @lhalden
      @lhalden 2 ปีที่แล้ว

      i am a hater of you

    • @lhalden
      @lhalden 2 ปีที่แล้ว

      i defeated the algortithm first

    • @thegreatape5774
      @thegreatape5774 2 ปีที่แล้ว +2

      Don’t care + didn’t ask + cry about it + stay mad + get real + L + mald seethe cope harder + ho mad + basic + skill issue + ratio + you fell off + the audacity + triggered + any askers + repelled + get a life + ok + and? + cringe + touch grass + donowalled + not based + your a (insert stereotype) + not funny didn’t laugh + you “re” + grammar issues + go outside + get good + reported + ad hominem + GG! + ask deez + ez clap + straight cash + ratio agian + final ratio + problematic

  • @bisakhabandyopadhyay5003
    @bisakhabandyopadhyay5003 3 หลายเดือนก่อน

    No offense but, whenever I tried coding the game, I ran it to test and the top where it says like, “x turn”, it does not have a space and it is not changing to, “o”. Which is why I had to delete the whole program.😔😔😔

  • @Rubiqs
    @Rubiqs 2 ปีที่แล้ว +1

    comment

  • @hungjcc
    @hungjcc 10 วันที่ผ่านมา

    can use [0,0] instead of [0,0,0]

  • @sportfreund-ub2mz
    @sportfreund-ub2mz ปีที่แล้ว

    Is it possible to get a copy of your tic-tac-toe-python-code?

    • @gorgustus
      @gorgustus ปีที่แล้ว

      check description and pinned comment

  • @NguyenMinhPhu664
    @NguyenMinhPhu664 ปีที่แล้ว

    omg 888 likes