Lists & Tic Tac Toe Game - Python 3 Programming Tutorial p.3

sentdex · Beginner ·🛠️ AI Tools & Apps ·7y ago

Key Takeaways

The video demonstrates the creation of a text-based Tic Tac Toe game using Python 3, utilizing lists to represent the game board and process logic. It covers breaking down complex problems into smaller pieces, using programming to understand numbers, and representing the game map with zeros, ones, and twos, later converting to X's and O's and lines.

Full Transcript

what's going on everybody and welcome to part three of the Python 33.7 to be specific basics tutorial in this video what we're going to be doing is starting our overarching objective for the series which is to create a text-based version of tic-tac-toe which should allow us to cover all the basics we need before we can get the heck out of the basics and start doing things that you personally are interested in so with that let's go ahead and get started so first of all right out of the gate we we know what we want to do we want to make tic-tac-toe we're gonna simplify it by not making any sort of graphical user interface we're going to keep it just text-based so that the the graphical bit is so we can see the game and all that will just happen in the console basically so so that's all so we're gonna keep it there but we still have a lot of questions to answer like you know we know that we want to make tic-tac-toe I think most of us know going into tic-tac-toe how the game works and the rules but how do we make a program know the rules and how do we display it just right how do we take an input from the user to where they want to play all that stuff how do we how do we actually do that so the first thing and honestly this is a this is these questions or this is how you approach any problem obviously tic-tac-toe is a super basic thing but it forces there's actually a lot of complex things associated with even just something as simple as tic-tac-toe so hopefully this approach to solving the problem will make sense to you and you can start to kind of think this way about any problem so when you've got a problem you what you have to do is just break it down into just the mote the smallest pieces that you can just start ticking off those small pieces until finally you've got your finished product so the first thing we want to do is I'm just going to go ahead and clear everything out here and let's just run that clear it up so so with the game to play tic-tac-toe we need to play it on something we need some sort of game visual you know game map so to speak so to do that how do we want to actually visualize the game itself so we could do something like you know somehow visualize a bunch of emptiness and then you know or maybe make the grids like so obviously if you were to draw tic-tac-toe it's like two lines and then two lines that's gonna be kind of hard we would spend a lot of time with the whole ASCII stuff just getting everything to line up just right and depending on which editor someone uses or which consoles someone's using or what their font is that could get hairy really quickly so don't really think I want to do that necessarily so let's just keep it unbelievably basic so let's say let's just say we we're gonna work with zeros ones and twos so zeros will be just the game map so also just a thing to note later on when like we're gonna do things in terms of just using numbers but later if you want it and I totally if anybody is like man that's lame I wanted the two lines if that's you what you should do is when we're done here add the two lines to the representation because it's totally possible it's just not necessary but later you can convert everything you can convert zeros to be spaces then you could convert you know the ones and twos to actually be X's and O's and then you could convert every you know all the things in between you could use logic to create the two lines and stuff so I definitely encourage anybody who wants to to take that extra step so to start we're going to just use lists and we're going to use a list of lists to represent our game board and to to process all of our logic because at the end of the day programming likes to understand numbers not so much the visual representation as we go on we could we can learn to do things like image processing and image analysis and even machine learning and stuff to to learn what a you know visually a tic-tac-toe map means but chances are you're not looking to get that complex here we're just trying to learn the basics so so let's get started so let's just say we've got a game so I'm gonna do the game variable here and we're going to say game equals and then we could make it a tuple right so we could say zero zero zero so we could do something like that right and then maybe come down and then somehow like kind of cheat our way through here and zero zero zero and don't forget this comma that would be a syntax error yikes so we could try to do something like that but even that's gonna be a problem at least without our parentheses so we could continue like this so let's just copy in paste now what would be the issue with our game like this because we could print game now there's gonna be two major issues one you should know out of the gate immediately as we talked about in the last tutorial but there's actually two issues let's print it out and the second issue is going to be glaring here's the second issue the game isn't represented like this it's represented in just a flat because that newline is just white space it doesn't mean anything to our programming language unless it's a code block so so right now it's just a what we would call them programming just it's just a flat tuple here so so what could we do but also it's a tuple so it's immutable we can't change it over time so we don't want that so then what we'd have to do is like do some redefinition and stuff it would be just a huge mess so instead we're going to use lists so we can convert this to a list really simply from parentheses to square braces here boom now it's a list we can run that again we see okay it's a list we can even run type game and we'll see ah it's a list so but the problem is of course we have the issue of it's all flat when we try to display it an event we still want people to at least understand what's happening in the game so I'm gonna instead convert this to a list of lists so now we've got this first row and now we've got the second row and then we'll add a third row that's getting angry because we don't have a bracket here done now if we were to print game we can see here okay they're separated but they're still they're not they're not like our new line so what could we do to solve that problem what we learned it in the last tutorial we can just iterate over game to display it all pretty like so what we could say is four four row in game prints row and now let me remove game here since that made it kind of confusing and now that's starting to look like a 3x3 grid that we could begin to place things on and all that so so now we're starting to see okay the game map is coming together now let's say you're a user and you wanted to play on this game map the next thing that we have to do is figure out how can we get people to actually say okay I want to play this spot here because again like I said in the very beginning you know I always thought all programs were these graphical user interfaces but in reality they're not and so without getting some pretty complex code we're not gonna get like a mouse that we're gonna be like I want to move here and so we need to let the user input in some way where do they want to go but in order to do that the user has to know okay what do I call this so in general we might have like some numbers or some letters and those say I want to move to 1a or something like that and as long as we do something like that we can keep it totally text-based so in the next tutorial that's what we're gonna start working on is okay we know how to iterate over this but now how do we like as we iterate just represent and show you know 0 1 2 and ABC or something like that so the user could just easily type in okay I want to move to such-and-such location and then we could process that and actually you know make a mark there and begin to analyze did they make a you know fully diagonal or horizontal or vertical and all that so anyways that's what we're gonna do in the next tutorial questions comments concerns leaving below or join the discord otherwise I will see you guys in another video

Original Description

Learning about lists and beginning our Tic Tac Toe project! Playlist: https://www.youtube.com/playlist?list=PLQVvvaa0QuDeAams7fkdcwOGBpGdHpXln #python #programming #tutorial
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

This video teaches how to create a text-based Tic Tac Toe game using Python 3, covering lists, game development, and problem-solving. It matters because it helps beginners understand programming fundamentals and complexity reduction. By following the steps, viewers can create their own text-based games and improve their programming skills.

Key Takeaways
  1. Clear everything out
  2. Run a clear command
  3. Visualize the game on a game map
  4. Use a list of lists to represent the game board
  5. Process logic and store game state in lists
  6. Convert a tuple to a list
  7. Convert a list to a list of lists
  8. Iterate over a list to display a 3x3 grid
  9. Process user input to make a mark on the board
  10. Analyze the game state for diagonal, horizontal, or vertical wins
💡 Using lists to represent a game board and process logic is a fundamental concept in game development and programming.

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →