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

preview_player
Показать описание
Hey everyone,

In today's video, we talk about how to code a Tic Tac Toe game in Python. Tic Tac Toe is a simple game that everyone knows how to play. Because the rules are easy in real life, it's easy to translate to the world of coding as well!
We set up 3 methods to operate the game and a while loop to run the game logic. Hopefully this is an easy tutorial to follow and you all learn something. Enjoy!

Thanks for watching! :)

This project is available on GitHub at
--------------------------------------------------------------------------------------

Add Me On Discord!
--------------------------------------------------------------------------------------
Username: Shaun(Hashtag)5626

Software Engineering / Programming for beginners / variables types / computer science /compsci / coding for beginners / learn how to program / learn how to code / python variables / python for beginners / coding tutorial / programming tutorial

Want to see more? Click on this spicy link :D

Assets in the thumbnail were provided by artists on

All art and copyright ownership belongs to the artists on Canva and is under the protection of the Canva pro membership.

#programming #tutorial #learntocode
Рекомендации по теме
Комментарии
Автор

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!

Hex-Scholar
Автор

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

UlyssesOfOmaha
Автор

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

hamamunashe
Автор

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

hana-wnbt
Автор

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

_TechandSport_
Автор

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"....

You can just replace it with one:

ebbewertz
Автор

where did you call the check for winner function?

speedster
Автор

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?

malakaiponce
Автор

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

bipintimilsina
Автор

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

veilie
Автор

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<s a good beginner project? Do you have any recommandation?

jojombo
Автор

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

Thomas-quyw
Автор

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'

joanoboarekeh
Автор

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

YepSmoof
Автор

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

nathanc
Автор

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?

bamise
Автор

mistake on 104 line
if(numberPicked >= 1 or numberPicked <= 9):. should be 'and' except 'or'

valeriiastartseva
Автор

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

jayzfly
Автор

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

rayan
Автор

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 <=9) in the if statement always evaluates to True, regardless of the value of numberPicked. This is because you're using the logical operator or instead of and. So, the code inside the if block gets executed even if the numberPicked is outside the range of 1 to 9.

To fix this issue, you should use the and operator instead of or in your if condition. Here's the corrected line:

python
Copy code
if numberPicked >= 1 and numberPicked <= 9:
With this change, the if block will only be executed if numberPicked is both greater than or equal to 1 and less than or equal to 9.

Additionally, you might want to add some conditions to check for a winner after each move and to check for a tie if there are no more available moves.

Here's the modified while loop with the corrected condition:

python
Copy code
while not leaveLoop:
if turnCounter % 2 == 1:
printGameboard()
numberPicked = int(input("\nChoose a number [1-9]: "))
if 1 <= numberPicked <= 9:
modifyArray(numberPicked, "X")

turnCounter += 1
else:
print("Invalid input. Please try again!")
else:
while True:
cpuChoice =
print("\nCPU choice:", cpuChoice)
if cpuChoice in possibleNumbers:
modifyArray(cpuChoice, "O")

turnCounter += 1
break
Make sure to add the logic for checking for a winner and a tie condition as well. This will complete your Tic Tac Toe game. If you have any further questions or need additional assistance, feel free to ask!

deskatatona
visit shbcf.ru