How to code tic tac toe in python | Beginner friendly programming tutorial project

  Рет қаралды 66,954

Garrett Codes

Garrett Codes

Күн бұрын

Пікірлер: 131
@santhoshkumarsekar8311
@santhoshkumarsekar8311 3 жыл бұрын
Never expected I will make a game with just functions and conditional statements alone. Thanks for the informative content.
@GarrettCodes
@GarrettCodes 3 жыл бұрын
No problem! Thank you!
@evergreen_5
@evergreen_5 2 жыл бұрын
It been 6month learning python but Iam only able to code simple program not a game but this helped me .
@piyushbhagwat3119
@piyushbhagwat3119 3 жыл бұрын
Thanks.. this was my first ever project after getting the basic done.. now i understood how too implement the things that i have learnt
@GarrettCodes
@GarrettCodes 3 жыл бұрын
That’s great! Glad you liked it
@abroadstateofmind5571
@abroadstateofmind5571 3 жыл бұрын
Thanks a lot I really enjoyed following you step by step. IT was a bit complex for me but practical work makes me learn more concepts rather than just reading books. Subscribed!
@JJTradess
@JJTradess 10 ай бұрын
Incredible tutorial this was my first ever project with python loved it im going to add a Easter egg to this Tic Tac Toe where you can auto win if your input a specific word
@Xeroc524
@Xeroc524 2 жыл бұрын
Great tutorial, I just recently coded a game of tic tac toe using the python turtle module and figured it would be interesting to see how others did it
@GarrettCodes
@GarrettCodes 2 жыл бұрын
That is great! Keep it up
@brennanb3919
@brennanb3919 2 жыл бұрын
Thank you for braking down the logic before we start programming!
@GarrettCodes
@GarrettCodes 2 жыл бұрын
No problem just glad to help
@soy_blug
@soy_blug 2 жыл бұрын
POV: you have a computer science assignment due in 2 hours
@Fearlessgaming920
@Fearlessgaming920 2 жыл бұрын
he deserves more subscribers so good for beginners
@GarrettCodes
@GarrettCodes 2 жыл бұрын
Thank you!!
@Kacper-db7qx
@Kacper-db7qx 3 жыл бұрын
At 30:56 add the function add_to_board(coords,board) to the end of the while loop and then run the code. :)
@ansongoh8185
@ansongoh8185 3 жыл бұрын
Thank you so much!
@personperson2059
@personperson2059 2 жыл бұрын
Thank you I've been stuck on that😭
@lexscher
@lexscher 4 жыл бұрын
Lmao that intro is next level🔥 thanks for creating this tutorial!
@GarrettCodes
@GarrettCodes 4 жыл бұрын
Thank you. I am trying. Really appreciate it Alex
@rajatkhatua6396
@rajatkhatua6396 3 жыл бұрын
why does while True: print_board(board) not work it says something about indented blocks
@GarrettCodes
@GarrettCodes 3 жыл бұрын
Python’s interpreter cares about how your blocks of code are indented. Most likely its either doubled indented for print or a random amount of spaces it doesn’t know. I recommend using a text editor like vscode
@codewarrior4458
@codewarrior4458 3 жыл бұрын
It's all clean and easy to understand
@GarrettCodes
@GarrettCodes 3 жыл бұрын
Thank you! I am glad you liked it 😊
@omkhare1514
@omkhare1514 3 жыл бұрын
This is so beginner friendly...loved it!
@GarrettCodes
@GarrettCodes 3 жыл бұрын
Thank you! I am happy you liked it
@aliemarah
@aliemarah 3 жыл бұрын
Thanks a lot boss! You really gave me the confidence and know how to face other challenges.
@GarrettCodes
@GarrettCodes 3 жыл бұрын
Awesome! I am glad you liked the video
@tahoegreg661
@tahoegreg661 3 жыл бұрын
Thank you my brother... great video appreciate bigly
@bensonostler5906
@bensonostler5906 3 жыл бұрын
Great video, thank you so much!!! great explanation, and editing.
@GarrettCodes
@GarrettCodes 3 жыл бұрын
Thank you so much! I really appreciate it
@IndianGamer40uwu
@IndianGamer40uwu 3 жыл бұрын
Thank you soo much I liked and subscribed to your channel. We need this kind of vids :)
@GarrettCodes
@GarrettCodes 3 жыл бұрын
Thank you!!!!
@matteoscalas4084
@matteoscalas4084 3 жыл бұрын
it may be naive for a beginner to ask this but, why did you write the program in so many small function?Like check_num and is_num could be easily be one function, "quit" is not really necessary as it could simply be a if condition in the while loop etc...what am I missing?
@igorpannis
@igorpannis 2 жыл бұрын
Hey man, i want to learn Some Python. But i have a question. Are you just using python or a you using a special version? Thanks!
@GarrettCodes
@GarrettCodes 2 жыл бұрын
Im using python 3. Its probably the most prolific version of python today. Setting up dev environments is always annoying. If you want get straight to the code try using repl.it or download ide like pycharm if you want to develop locally
@rajpatil9658
@rajpatil9658 2 жыл бұрын
Thank u soo much!
@beef2244
@beef2244 2 жыл бұрын
This is great. How would you suggest changing the code if I don't want the terminal to print "Please enter a position 1-9 or enter \"q\" to quit:" when someone wins or if there is a tie?
@joniugvaro3977
@joniugvaro3977 2 жыл бұрын
22:20, you don't show us def bounds code, you just put code in def check_input, I was confused at first but then I realized that you didn't show us the meaning of def bounds and errors kept showing up, btw I am writing in great program for beginners repl.it it shows you errors even if you don't run it and shows you the definition of every single function.
@GarrettCodes
@GarrettCodes 2 жыл бұрын
I’m sorry about that must have skipped the impl there. The explanation for it is here though: 21:40. Please check with the github code for full impl though. Replit is amazing! I highly recommend beginners to use it so you dont have to worry about setting up a whole environment
@vegaarcturus509
@vegaarcturus509 3 жыл бұрын
Im just a nwecomer to pyhton but i dont understand why you need to put return true and false in def block
@stcgocrazy
@stcgocrazy 2 жыл бұрын
I don't know if it's just me, but I follow everything step by step and it's just not working. When I mean it's not working, im talking about the winning feature . add_to_board(coords, board, active_user) if iswin(active_user, board): print(f"{active_user.upper()} won!") break I put this down and but in rows, col, and diagonal and it just not working. Help if you can
@GarrettCodes
@GarrettCodes 2 жыл бұрын
Here’s the code: github.com/Ghalstein/tictactoe/blob/main/tictactoe.py In debugging it I would check and make sure each function is working as expected
@stcgocrazy
@stcgocrazy 2 жыл бұрын
@@GarrettCodes Thank you so much, I was so worried about this since it's my first project. Also thank you for coming back to your old vids; it shocks me you still answer questions.
@GarrettCodes
@GarrettCodes 2 жыл бұрын
@@stcgocrazy Always here to help!
@okletsdoitt
@okletsdoitt 4 жыл бұрын
Awesome man!
@GarrettCodes
@GarrettCodes 4 жыл бұрын
Thank Muazzam! Much appreciated
@randax8518
@randax8518 3 жыл бұрын
Hey there, I'm not sure whether you still respond to comments but may I know why you did row = coords[0] col = coords[1] Thank you
@jadabrown8236
@jadabrown8236 2 жыл бұрын
I got to the middle of the video about the def bounds and now I’m getting 3 rows of lines going across instead of any dialogue like before. This is my code: def print_board(board): for row in board: for slot in row: print(f"{slot} ", end="") print() print_board(board) def quit(user_input): if user_input == "q": print("Thanks for playing") return True else: return False def check_input(user_input): # check if its a number if not isnum(user_input): return False user_input = int(user_input) # check if its 1 - 9 if not bounds(user_input): return False return True def isnum(user_input): if not user_input.isnumeric(): print("This is not a valid number") return False else: return True def bounds(user_input): while True: print_board(board) user_input = input("Please enter a positon 1 through 9 or enter \"q\"to quit:") if quit(user_input): break if not check_input(user_input): print("Please check again") continue
@Slay42069
@Slay42069 2 жыл бұрын
yeah for some reason he cut the part where he wrote def bounds at 23:10 it is fully written with no explanation.
@ibraheemkhaleel9607
@ibraheemkhaleel9607 3 жыл бұрын
Nice tutorial. Thank you
@beautifulmind8313
@beautifulmind8313 3 жыл бұрын
quick question: It seems like my continue is not working. o you know why? Here is my code: while True: print_board (board) user_input = input("Please enter a position 1 through 9 or enter \"q\" to quit:") if quit(user_input):break if not check_input(user_input): print("Please try again.") continue user_input = int(user_input) - 1 board[0][0] = "x" coords = coordinates(user_input) if istaken(coords, board): print("Please try again.") continue add_to_board(coords, board)
@TheLegalLens1
@TheLegalLens1 2 жыл бұрын
I'm new to Python and coding in general - I'm at 23:55 , when I run my program its not going through some of the checks. I'm not sure what exactly im missing, but its like if those ' def ' statements arent there at all. Does anyone have any tips or advice? Thanks!!
@suganyaperumal2150
@suganyaperumal2150 2 жыл бұрын
can you share me the app which you installed for coding this program?
@sumnipandey5035
@sumnipandey5035 2 жыл бұрын
can you explain to me this part def istaken(coords, board): row = coords[0] col = coords[1] if board[row][col] != "-": print("This position is already taken.") return True else: return False why is coords[0] and [1] thanks
@GarrettCodes
@GarrettCodes 2 жыл бұрын
Good question! So we are taking in the coordinates (coords). If we picture the 2d array like: [[-,-,-],[-,-,-],[-,-,-]] you see that the row is each inner array and the column actually specifies the element inside the inner array. So in order to specify a specific part of the tic tac toe board we need to first specify which inner array and then which element. I called it row and col because when you draw it as a board thats exactly how it works.
@sumnipandey5035
@sumnipandey5035 2 жыл бұрын
So you mean 0 represent the row? And 1 represent the column? Thank you for your reply appreaciate it.. only this part is so confusing to me:)
@GarrettCodes
@GarrettCodes 2 жыл бұрын
Exactly! No problem. Familiarizing yourself with 2D arrays is definitely the hardest part of this problem
@mingutimmir6129
@mingutimmir6129 2 жыл бұрын
Can someone explain me why check_input function was always returning False?
@GarrettCodes
@GarrettCodes 2 жыл бұрын
Every function returns a value whether you explicitly are doing it or not. The default value if nothing is returned is None, which converges to False. In check_input we were initially evaluating its return value when we were not returning anything. This made it return None, which is False every time. After we explicitly return the value it gets fixed. Here is some more info on it: realpython.com/python-return-statement/#:~:text=That%20default%20return%20value%20will%20always%20be%20None%20.&text=If%20you%20don't%20supply,None%20as%20a%20return%20value.
@mingutimmir6129
@mingutimmir6129 2 жыл бұрын
Thanks
@Tactiducky
@Tactiducky 3 жыл бұрын
I’m using atom text editor and def print_board(board): Has a syntax error apparently, how should I fix this?
@Standboard
@Standboard 3 жыл бұрын
i have a question when i try to run the code it gives a weak warning of shadow name from outer scope(for words like user_input,board etc) can you reason why is it appearing
@python5248
@python5248 2 жыл бұрын
He sounds so much like Sapnap!
@ammadirshad2888
@ammadirshad2888 2 жыл бұрын
Respect ❤️
@GarrettCodes
@GarrettCodes 2 жыл бұрын
Thanks!
@atonfoabdoul-aziz7834
@atonfoabdoul-aziz7834 3 жыл бұрын
Got eject at the 'coordinates' function... KO my brain
@misty2984
@misty2984 3 жыл бұрын
how do you make it into a actual game like not on the thing
@GarrettCodes
@GarrettCodes 3 жыл бұрын
Good question. We would have to connect this logic to a ui via js or some other language that can modify the view. Will have to work on this tutorial soon
@nachiketpushkarna3295
@nachiketpushkarna3295 3 жыл бұрын
i am not able to understand the row and col part.Pls help me
@GarrettCodes
@GarrettCodes 3 жыл бұрын
Its because we are using a 2 dimensional array to represent the tic tac toe board. So a 1d array look like this: arr = [1,2,3] A 2d array looks like this: arr2 = [ [1,2,3], [4,5,6], [7,8,9] ] For the 1d array we access the first element like this arr[0]. That would give us 1. However, the first element in the 2d array is an array itself. arr2[0] gives us [1,2,3]. To get the actual first element in arr2 we call arr2[0][0]. That gives us 1. So now back to rows and columns. We consider the first index the row since its referring to the array and the column is the second index referring to the column of the element in the array.
@RohanzxEditzShorts
@RohanzxEditzShorts 3 жыл бұрын
Thanks for making this tutorial. Do you know how to run the code?
@GarrettCodes
@GarrettCodes 3 жыл бұрын
Sure you should be able to run the code with the following syntax: ‘python3 tictactoe.py’ where tictactoe.py is the name of the file. Make sure you have python 3 installed on your machine first though.
@RohanzxEditzShorts
@RohanzxEditzShorts 3 жыл бұрын
@@GarrettCodes Thanks! Do I type it onto the terminal?
@GarrettCodes
@GarrettCodes 3 жыл бұрын
@@RohanzxEditzShorts yeah in there in the same directory as the python code. You might like my python in 15 minutes video as well to get things started. But keep going at it you are doing great!
@raniad936
@raniad936 3 жыл бұрын
This is an awesome tutorial! I've only started learning Python a few weeks ago and have been stumped doing my tic-tac-toe assignment for weeks, and this really helped a lot! I have a question, though, if you do not mind: What is the difference between creating the board using the two-dimensional array you did in the video: board = [ ["-", "-", "-"], ["-", "-", "-"], ["-", "-", "-"], ] versus doing it in this manner: board = [["-" for r in range(3)] for c in range(3)] ?
@GarrettCodes
@GarrettCodes 3 жыл бұрын
There is no difference. Actually yours is a better way because its not hard coded like mine. We both just made a 2 d array
@raniad936
@raniad936 3 жыл бұрын
@@GarrettCodes Thank you very much. There is so much to learn and I have a long way to go, but it's been very exciting. I also liked how you listed the things that need to be done at the start, and explained the method of executing them step by step. Hope to see more videos soon!
@sebskills9181
@sebskills9181 3 жыл бұрын
Damn, how are you doing now and how the heck did you learn coding so fast and fully grasp the knowledge in less than a month?
@johnysins69696
@johnysins69696 Жыл бұрын
hello brother where are you now?
@theBrilliant1o1
@theBrilliant1o1 3 жыл бұрын
col = int(user_input % 3) is enough actually
@justme5169
@justme5169 4 жыл бұрын
Thanks for this tutorial. Can you please help me with an advice? I am in the last year at university and i tries to lean c/c++ but recently i found that i don t like it. So i switched to web dev(python+css+html+javascript) and i try to learn those languages mentioned above. Do you think it is a good choice? How much time would take to learn that languages? Or what do you recommand?
@GarrettCodes
@GarrettCodes 4 жыл бұрын
I think you should do what you like and get good at what you like. You can find success in whatever you choose as long as you focus on it. I recommend finding a web dev course online. Try cs50 web dev section its free and very informative. From there you just need to work on your own projects. Might be worth supplementing more knowledge with web dev bootcamp courses on udemy, such as colt steele's. The only other thing you would need to work on is data structures and algorithms (interview prep). Hope this helps, feel free to browse my channel for more advice and if you have any specific questions just drop another comment
@wollongar2014
@wollongar2014 2 жыл бұрын
its real a great video, but am stuck, when you defined coordinates and istaken fuction. it didn't work for me. I repeated the video several times, but still cant get it right
@GarrettCodes
@GarrettCodes 2 жыл бұрын
Sorry to hear. What error did you get?
@wollongar2014
@wollongar2014 2 жыл бұрын
@@GarrettCodes it's not an error, but when I enter 1 to check if it's taken like u did the video, it jump back to (please try again) as it is expecting the (check-input) function.
@wollongar2014
@wollongar2014 2 жыл бұрын
@@GarrettCodes I tried the source code and it's working fine am gonna to try understand the logic in careful and give it a try again.
@GarrettCodes
@GarrettCodes 2 жыл бұрын
@@wollongar2014 ok it might be very subtle. I recommend pasting the source code and your code into a diff checker. I feel it might even be an indent somewhere. Paste them into here: www.diffchecker.com/
@nikolaborska7537
@nikolaborska7537 3 жыл бұрын
Great tutorial. I have just one question. When I have the part with “def istaken(coords, board): ..... The VSC tells me: IndentationError: expected an indented block an I really don’t know what to do with it. Do you have any recommendations?
@GarrettCodes
@GarrettCodes 3 жыл бұрын
Hey Nikola! So python’s code is interpreted and parsed by indentations so if you are missing or have an extra indent lying around in your code it could break it
@BreezyMC-rg7os
@BreezyMC-rg7os 3 жыл бұрын
16:18 dont mind me im just coming back
@agachaworld4192
@agachaworld4192 2 жыл бұрын
3:56 it's to not too
@King_Crimsn
@King_Crimsn 3 жыл бұрын
I'm hitting an attribute error with the "insum" function. Any ideas on how to adjust it?
@NiCk83831
@NiCk83831 3 жыл бұрын
dude same here
@xwdesignproductions1421
@xwdesignproductions1421 3 жыл бұрын
if u input 1 2 3 4 5 then the code says X won, and i have no clue how to fix it
@GarrettCodes
@GarrettCodes 3 жыл бұрын
Tough bug to crack sometimes when there are different possibilities causing it. However since the code is mostly encapsulated into different functions you can walk through function and try to comment out the actual logic in a function one by one until the bug stops showing up. It is mostly related to check col, row, diag though
@xwdesignproductions1421
@xwdesignproductions1421 3 жыл бұрын
@@GarrettCodes thanks dude, do you think I can get the source code and compare it to the one i wrote as it is a bit hard looking at the screen trying to compare them . Thanks !
@GarrettCodes
@GarrettCodes 3 жыл бұрын
@@xwdesignproductions1421 definitely. I meant to add to the description but O guess I forgot: github.com/Ghalstein/tictactoe
@xwdesignproductions1421
@xwdesignproductions1421 3 жыл бұрын
@@GarrettCodes thanks
@xwdesignproductions1421
@xwdesignproductions1421 3 жыл бұрын
@@GarrettCodes ye you were right it was the check row function. I want to take this project a bit further by making it possible to play against the computer but I don't really know where to start, do you think you can help me out a little ? btw thank you for the replies as well, great feedback
@brandongarza1366
@brandongarza1366 3 жыл бұрын
Great video Garret! When i'm trying to test updating the board with an 'X' my program, i'll enter a number but get an error saying "int type object is not subscriptable" and it points to the isTaken function. Any advice? i've been troubleshooting for an hour with no luck.
@brandongarza1366
@brandongarza1366 3 жыл бұрын
Figured it out. I was passing in user_input into the istaken argument instead of 'coords', and it didn't work with the string.
@GarrettCodes
@GarrettCodes 3 жыл бұрын
Nice debugging!
@pimpirisnais
@pimpirisnais 3 жыл бұрын
Thanks!
@worldofscience4049
@worldofscience4049 3 жыл бұрын
how to make minecraft with python
@siddharthtanwar1529
@siddharthtanwar1529 3 жыл бұрын
you spelled 'successful' wrong at the start in the docstring comments.
@GarrettCodes
@GarrettCodes 3 жыл бұрын
Good catch mate
@gregaustin7733
@gregaustin7733 3 жыл бұрын
I don't know if I messed up somewhere, but the quit function doesn't seem to work properly. Could be my fault entirely but did you encounter this problem at all? Thanks! Otherwise great video, I really learned a lot!
@GarrettCodes
@GarrettCodes 3 жыл бұрын
Thank you! See if you can debug the situation. Check what your quit returns bu printing info before the return statements and then in your main method check how you are handling the quit function
@coder100funny
@coder100funny 2 жыл бұрын
is this runnable in google colab
@GarrettCodes
@GarrettCodes 2 жыл бұрын
Not familiar with it but if it has python on it should be fine
@coder100funny
@coder100funny 2 жыл бұрын
@@GarrettCodes o thanks
@tennesseetoombs7516
@tennesseetoombs7516 2 жыл бұрын
20:45
@coder100funny
@coder100funny 2 жыл бұрын
exactly while did you say 1 through 9
@GarrettCodes
@GarrettCodes 2 жыл бұрын
Theres 9 spots on the board
@coder100funny
@coder100funny 2 жыл бұрын
@@GarrettCodes i see
@ruthpille585
@ruthpille585 3 жыл бұрын
🇷🇺for the motherland
@cherokeexxx1
@cherokeexxx1 3 жыл бұрын
Hi garret, i follow the tutorial down to the last detail, but when i run the program it doesn't update the matrix. input_user = int(input_user) - 1 coord = coordinates(input_user) board[0][0] = "x" if isTaken(coord, board): print("Try again") continue out of this it validates conditionals correctly. Any suggest for me ? :(
@Mohammed_Albaqer
@Mohammed_Albaqer 2 жыл бұрын
I know I'm late same happened to me probably bcz the indices of the board in istaken function aren't recognized as integers so just convert them to int : board[int(row)][int(col)] in the if function. same for the add to board function :)
Python TIC TAC TOE Tutorial | Beginner Friendly Tutorial
25:15
Code Coach
Рет қаралды 204 М.
Python for absolute beginners 2019 - TIC TAC TOE project (+Special Appearance!)
1:05:04
Disrespect or Respect 💔❤️
00:27
Thiago Productions
Рет қаралды 37 МЛН
БУ, ИСПУГАЛСЯ?? #shorts
00:22
Паша Осадчий
Рет қаралды 2 МЛН
Каха и лужа  #непосредственнокаха
00:15
ROSÉ & Bruno Mars - APT. (Official Music Video)
02:54
ROSÉ
Рет қаралды 328 МЛН
CODE WITH ME | Tic-Tac-Toe Game in Python *beginner friendly*
21:12
Running "Hello World!" in 10 FORBIDDEN Programming Languages
18:07
Code Tic Tac Toe in Python
33:28
Kenny Yip Coding
Рет қаралды 15 М.
Tic-Tac-Toe Game in Python - Unbeatable Minimax AI
42:36
NeuralNine
Рет қаралды 13 М.
How To Code Tic Tac Toe With Python | Programming Tutorial For Beginners
15:11
Игра Крестики-нолики на Python
42:36
Захаров Андрей
Рет қаралды 42 М.
programming projects that taught me how to code
9:49
isak
Рет қаралды 305 М.
C Programming Tutorial for Beginners
3:46:13
freeCodeCamp.org
Рет қаралды 14 МЛН
10 Important Python Concepts In 20 Minutes
18:49
Indently
Рет қаралды 269 М.
Disrespect or Respect 💔❤️
00:27
Thiago Productions
Рет қаралды 37 МЛН