How To Code Tic Tac Toe With Python | Programming Tutorial For Beginners

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 ธ.ค. 2024

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

  • @idahosprings2224
    @idahosprings2224 17 วันที่ผ่านมา +1

    I know for a fact that I don’t have the aptitude to be a programmer, but I watch these videos occasionally to see how it’s done.

  • @Hex-Scholar
    @Hex-Scholar ปีที่แล้ว +5

    Hey Man! Nice Tutorial easy & quick!
    I personally attempted it first and then viewed your video, just so I could compare & improve!
    I took the following approach:
    - I made a single string for the board and to update the board, I simply used .replace() method.
    (This had nothing to do with the computation, just for representation purpose)
    - To keep track of all changes, I basically had 4 lists:
    A. One for the player (your options): []
    B. One for the second player or the computer (based on the option chosen, also if you play against computer you can choose between Easy | Hard )
    C. One for all moves used: List in A + List in B
    D. Last was a list comprehension, available options = [option for option in options (C) if option not in C]
    - Using conditions, was able to:
    A. Take Inputs (also had Try & Except, incase a alphabet was entered mistakenly, don't want it to crash :P), Change Turns on Each Game and Update Scores!
    - Computer Logic was as follows:
    A. Attempt to go for the win if possible (used Nested Loops) | B. If A fails, go for the block if possible | C. If both (A & B) fails go for a random number from the available options,
    all of this was done in as few as 15 Lines!
    Looking forward to improving it using Tkinter (by giving it a GUI)!
    Thanks Again!

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

    Thank you for doing this! I'm just starting on my journey to learn Python, and this was awesome!

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

    Nicely done. One day i will be so fast and error free. For now still learning

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

      me too

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

      How good are you now? I just started learning myself

  • @hana-wn4bt
    @hana-wn4bt 8 หลายเดือนก่อน +9

    this was a great video, until there was no part two 😢

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

    For determining what row and column the user chose, you can just devide the (-1 shifted) number by 3. Then the integer or math.floor of that would be the row. The culumn is then simply the number minus the row number times 3.
    input-=1
    row = int(input/3)
    col = input-row*3
    That way you dont have that gigantic row of if/elif statements.
    Also in the win checker, you can half ur code. Just make an array candidates = ["X","O"]
    Then just put the code inside a for candidate in candidates. Then instaed of each code being double like:
    If ...."X"....
    if....."O"....
    You can just replace it with one:
    If....candidate.....

  • @speedster6890
    @speedster6890 6 หลายเดือนก่อน +2

    where did you call the check for winner function?

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

    Thanks man! Now I need to find a way to make the checkForWinner function work.

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

    Is the coordinates on the"wins" area plotted not in (x,y) format because we are using "stacked" arrays on top of each other instead of a graph?

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

    It just keeps on playing, even if someone has gotten 3 in a row, is there a fix to this?

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

      Just create a reset() function and call it when a win happens.
      (Maybe after an input, so the user can wait to when he is ready to replay). The reset function must then just fill both possible numbers and gameboard to what they where at the top of the code (in the video line 6 and 7). Then all possible numbers can be guessed again and all the "X"'s and "O"s are removed from the board. Then just off course call the printgameboard function again to make the changes visible.
      This is if u want to be able to reset. If you just want to stop the game after winning, you can simply put that leaveloop to true. That will make the while loop's condition false which will end the loop and make the program stop.

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

    This is a nice tutorial, my problem is that when I ran the code, it showed me nothing. I don't know if it have something to do with 'import random'

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

    What's a fix for the CPU only picking one number

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

    mistake on 104 line
    if(numberPicked >= 1 or numberPicked

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

    after trying it the safe code to protected from invalid inputs didn't work also winning function did not work idk what happened

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

    wow man thanks for this great video. such a great one.

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

    Hi when i ran it it shows me that numberpicked on line 90 is undefined even tho i defined it!

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

      Yessir, just put „and“ for „or“ -
      if(numberPicked>=1 AND numberPicked

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

    Thank you Shaun, this cool.

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

    Hey! If we wanted to add an ending of the game that resulted in a tie how would we do that?

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

    I just started learning Python and I am very confused.
    I am trying to get into AI but there's not such thing as "majoring in AI" in college so I am learning through videos like yours.
    I am grateful that I found this channel and I am sure it will help me improve. Thank You!
    To learn more parcticily, I am setting myself an objective : Make a Tic Tac Toe game where computer "moves" are not random. Aiming to code every possible way a Tic Tac Toe game could be played so the cpu always makes the best "move" possible.
    Do you thing it

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

      Just checked, there's 255 168 possible playable games. 😬

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

      @@jojombo3459 I'm making the same thing, while there are a massive amount of possibilities you can narrow it down since many possibilities actually just lead to the same thing. Creating a logic tree of every similar possibility doesn't take too long and then it comes down to simplifying methods, which leads you to two big ones: only picking moves that may lead to a victory, i.e. not cornering yourself, not falling into traps, and creating the possibility of 3's in a row. The other is checking to see if either you or your opponent have 2 in a row with an open spot and either blocking or winning respectively.

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

      @@katsisvi8097 very logic, thanks for the advice

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

    why did it end instantly with no warning

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

    i had an error saying random was not defined what should i do?
    random.choice(possibleNumbers)

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

      import random

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

    Thank you. Very insightful. I got it set up but it doesn't show any winner, it just continues the game. Can you make another video for this?

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

      check the full code link in the description. You have to call the checkforwinner function at the end of your code.

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

    Am not sure why but didn’t work with me keep putting true instead of x or o

  • @Thomas-qu1yw
    @Thomas-qu1yw ปีที่แล้ว

    Thank you for helping me, i loved your video at first sight!

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

    14:20
    just did some coding off camera

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

    was this your work?

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

    Nice Video, but the code is not complete! Here is what chat gpt is saying:
    It seems like you're building a Tic Tac Toe game in Python. However, there's a small issue in your code related to the conditions inside the while loop.
    The condition (numberPicked >= 1 or numberPicked = 1 and numberPicked

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

    good stuff

  • @Aadi-zz3ne
    @Aadi-zz3ne 4 หลายเดือนก่อน +1

    why is the cpu so dumb

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

    Did you ever finish this tutorial or no?

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

    if(turnCounter % 2 == 0):
    printGameBoard()
    numberPicked = int(input("
    Choose a number [1-9]: "))
    if(numberPicked >= 1 or numberPicked

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

      I added While%
      hile leaveLoop == False:
      # turn of player
      if turnCounter % 2 == 1:
      printGameBoard()
      while True:
      numberPicked = int(input('
      Choose a number [1=9]: '))
      if 1

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

      @@valeriiastartseva1612 Do you know how you would add a tie?

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

      Yessir, just put „and“ for „or“ -
      if(numberPicked>=1 AND numberPicked

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

      Please can you describe this part of the code?

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

      ​@@jayzfly8416A tie happens if there is no win, but you also can not play anymore. So:
      There is no win in a tie, so the code for that must me in an else statement at the end of all the if and elifs inside the winner checker. So if no "normal" win is recognised, it goes to check for a tie. So that is the first condition of the tie: no normal win. The second conditon is that no one can play anymore, aka the board is completely full. Since the code removes numbers from the possible numbers array when those numbers are played, you can just simply check if the board is filled by checking if there is nothing left anymore inside the possible numbers array. So in that else statement I talked about above, you put an if that checks if len(possiblenumbers)==0. (And then print("it's a tie") or something).
      len in python means the length of an array, so if Len(that array) equals 0, that means that the array is empty, which is what we were checking for.
      Hope this helps.

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

    "if all(...)" statements inside a for loop do the trick.

  • @SaurabhSingh-ct7ls
    @SaurabhSingh-ct7ls 2 ปีที่แล้ว

    Awesome

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


    dont work for me

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

    please make it oop

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

    i love you my lifesaver

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

    too small

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

    it doesn't work and no source code
    waste of time