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