Python for Absolute Beginners Transcripts
Chapter: Problem solving techniques for writing software
Lecture: Demo: Divide and conquer in action

Login or purchase this course to watch this video and the rest of the course contents.
0:00 It's time to get started working on our tic-tac-toe game. More over here in our Github repository And then in CH08
0:07 tic-tac-toe is empty so we are going to drop this over into PyCharm Community Edition and get started with an empty project.
0:14 So, we are going to need a Python file let's go over here and create a call it game or something like that.
0:21 Now, I'd like to go ahead and just run it right away. That way afterwards it looks like we got to select the Python interpreter.
0:29 That way we can just click right here every time or just hit Control R. Well, here we are in that situation I described.
0:36 Blank file, blinking cursor, now what? Let's start by just thinking about what it is that we need to do to create this game?
0:47 What are the major steps of tic-tac-toe? Well, remember just imagine you're a kid sitting down at a table in a restaurant
0:54 and you're waiting for the food to come and you're bored and you say Hey, lets play tic-tac-toe. What do you do?
0:58 Well, first thing you do is you get a piece of paper and you write out the board. You write two vertical lines two horizontal lines intersected.
1:04 So, the first thing we got to do is create the board. This is going to help us think about the data structures
1:10 how we want to store that information and so on. We might want to validate the board is okay maybe it's fine. We could choose an initial player.
1:19 That happens all of the time, right? Who gets to go first? And, are you going to be X's or are you going to be O's and so on.
1:27 Then what you do is you go around and you start whoever's turn it is they put their symbol into the spot on the board.
1:34 And they do that over and over switching sides until somebody wins. So, let's say until someone wins
1:41 maybe we can indicate that with a little indentation or something like. Choose the location, if there is one, mark it.
1:51 We are going to toggle the active player. First, it's your turn, then it's my turn. Then, it's your turn, then it's my turn.
1:56 Until somebody wins and then it's game over. Alright, so this is actually a pretty good set of steps here for us. Instead of thinking about how am I
2:06 going to write tic-tac-toe? How am I going to do all of the stuff? All I have to do first is think about how are we going to create the board?
2:13 Things like that, I guess, maybe in here somewhere until someone wins. We want to, this is going to be check for a winner or something like that.
2:22 We're going to need to maybe think a little bit about how we're going to check for a winner. Depending on how we create the board.
2:28 Choosing an initial player this one should be pretty easy. We are going to have two players, just pick one. This until number while loops.
2:36 While a condition is true until someone wins that's awhile loops. You're just going to awhile there is no winner.
2:42 And then we have to get input from the user. Where you're going to pick? Maybe show them the board? That's not the way it works in the real world.
2:49 Because it's always physically there but in this maybe we will want to show the board as part of the choosing.
2:56 Here's the current board, what location do you want to get? Mark it, we also have to make sure that they don't pick a pre-played spot.
3:03 You know if they say, I want to play the middle. And there's already somebody who's chosen that perfect middle spot to start from.
3:09 They can't do that, right? So, we're going to have to do a little verification there. And then that's their round, their round is over.
3:14 It's the other player's turn. And then we're going to show that other player the board. Ask them for a location and so on. Somebody wins, guess what?
3:21 The active player won. This is what we got to do. Instead of trying to think of how, geez we got to do all of this stuff. How do we create the board?
3:31 That's it, that's all you have to think about and solve. And then the next one is how do we choose an initial player and indicate the players?
3:38 This what I was thinking about is trying to divide and conquer this problem. This where we are going to start from.

Talk Python's Mastodon Michael Kennedy's Mastodon