Coding

How to Program Connect 4 in Python! (part 1) – Basic Structure & Game Loop

  • 00:00:00 what's up guys in this set of videos I'm
  • 00:00:03 going to show you how to program connect
  • 00:00:05 for in Python by the end of these videos
  • 00:00:08 we'll have built something that looks
  • 00:00:10 like this it's a simple two-player
  • 00:00:15 connect for game with graphics I think
  • 00:00:19 this is a really good exercise for your
  • 00:00:21 beginner to intermediate level Python
  • 00:00:23 programmers who want to build their
  • 00:00:24 skills I'll just finish up a game real
  • 00:00:27 quick just so you can see how this fully
  • 00:00:30 works as you can see I have the diagonal
  • 00:00:33 and it says player 1 wins cool all right
  • 00:00:35 so more specifically in this first video
  • 00:00:37 we're gonna do something a little bit
  • 00:00:38 simpler and then build up to that
  • 00:00:40 graphic base to connect for game so by
  • 00:00:42 the end of this cific video you'll have
  • 00:00:45 something that looks like this
  • 00:00:46 it's a command line connect for game
  • 00:00:49 where you actually manually type in
  • 00:00:51 numbers to drop your pieces so you see
  • 00:00:53 the one there the two cetera alright so
  • 00:00:56 let's start building that just quickly I
  • 00:00:58 want to mention that I'll be programming
  • 00:01:00 in Python 3 and then using sublime text
  • 00:01:02 2 as my editor I posted a link to a
  • 00:01:05 video on how to set up these things in
  • 00:01:07 the description so one of the first
  • 00:01:09 questions I ask myself when I was
  • 00:01:11 writing this game is what is the best
  • 00:01:12 way to represent the board so if I drag
  • 00:01:15 on a connect 4 board real quick you can
  • 00:01:16 see that it is six rows by seven columns
  • 00:01:20 and to me this looks like the perfect
  • 00:01:21 structure to represent as a matrix in
  • 00:01:24 Python so that's what we'll do so we'll
  • 00:01:26 define a function called create board
  • 00:01:28 and that's not gonna accept any inputs
  • 00:01:31 then we're gonna make a a matrix of all
  • 00:01:34 zeros with the dimensions 6 by 7 and to
  • 00:01:37 help us do this we're gonna import a
  • 00:01:39 package called numpy and if you haven't
  • 00:01:42 already installed this you can open up a
  • 00:01:45 terminal window and just type in pip
  • 00:01:46 install numpy and as you can see i
  • 00:01:50 already have it so we're gonna use this
  • 00:01:53 as followers we're gonna say board
  • 00:01:54 equals numpy zeros and that's gonna make
  • 00:01:58 a matrix of all zeros which is good for
  • 00:02:00 our initial state and then the
  • 00:02:02 dimensions six rows by seven columns
  • 00:02:04 cool and then we just need to return
  • 00:02:07 that forward and we can test to make
  • 00:02:09 sure that worked by doing the following
  • 00:02:11 create board
  • 00:02:12 and then prince board and as you can see
  • 00:02:17 if I know what happened as you can see
  • 00:02:19 if I run that we we do in fact have this
  • 00:02:23 6 by 7 matrix of all zeros so that looks
  • 00:02:26 good we can begin writing the mean game
  • 00:02:28 loop now so we're gonna write a loop
  • 00:02:31 that says while not game over
  • 00:02:34 and so our loop is gonna be running as
  • 00:02:38 long as this game over variable is false
  • 00:02:42 and the only way it becomes true as if
  • 00:02:45 someone has a foreign row so we need to
  • 00:02:46 initialize the game over to be false to
  • 00:02:48 begin with
  • 00:02:49 it goes false and the only way that's
  • 00:02:52 gonna switch to true is if someone got a
  • 00:02:54 four in a row so now we're gonna need to
  • 00:02:57 we're also going to want to real quick
  • 00:02:58 make our board initialize before we
  • 00:03:01 start the game just so we have something
  • 00:03:04 to put pieces into so we have our board
  • 00:03:06 we have our game over variable and now
  • 00:03:08 we're going to want to be able to ask
  • 00:03:10 for player 1 input and then we're also
  • 00:03:17 going to want to eventually be able to
  • 00:03:18 ask ask for player 2 input so this kind
  • 00:03:27 of leads us to the problem of oh how do
  • 00:03:28 we differentiate between whether it's
  • 00:03:30 player 1 or player 2's turn so we're
  • 00:03:32 always going to define another variable
  • 00:03:33 up here called turn that's just going to
  • 00:03:35 initialize to 0 so what we're going to
  • 00:03:37 say is if turn equals equals 0 then
  • 00:03:42 we're going to want to ask for player 1
  • 00:03:43 input alright so in Python there's a
  • 00:03:47 function a built-in function called
  • 00:03:49 input as you can see it highlights blue
  • 00:03:51 for me so we're gonna say selection
  • 00:03:56 equals input and then we're say player 1
  • 00:04:04 make your selection so this is what the
  • 00:04:07 player one's gonna say and then the way
  • 00:04:09 we're gonna initialize this game is
  • 00:04:11 we're gonna just say they can type in
  • 00:04:12 between 0 and 6 so this is what the
  • 00:04:15 player one should see so if I build this
  • 00:04:18 you see we run to this error so this is
  • 00:04:21 because sublime text doesn't accept this
  • 00:04:25 input
  • 00:04:26 function so my workaround for this was I
  • 00:04:29 installed a package in sublime called
  • 00:04:31 sublime repl I'll put a link in the
  • 00:04:33 description on how to install this and I
  • 00:04:36 can run the input function through that
  • 00:04:39 so now you can see player 1 make your
  • 00:04:40 selection 3 and doesn't do anything with
  • 00:04:44 that input right now but you can see
  • 00:04:45 that it's asking me that and I can type
  • 00:04:47 in hit enter all right cool
  • 00:04:49 so we also just want to make sure that
  • 00:04:52 it saves our selection properly so we're
  • 00:04:55 gonna print selection I also just want
  • 00:04:59 to see you know if I type in a number
  • 00:05:01 does it actually give me the number or
  • 00:05:03 does it give me a string that represents
  • 00:05:05 the number so I'm gonna also print in
  • 00:05:07 type of selection and I just want to see
  • 00:05:09 what we're getting so I'm gonna run that
  • 00:05:11 again and as you can see ever again but
  • 00:05:14 we're gonna go to sublime repl and the
  • 00:05:16 link is in the description as I just
  • 00:05:17 said run current files so – and as you
  • 00:05:21 can see it gets – but it says it's a
  • 00:05:24 string so we're gonna want to make sure
  • 00:05:25 that it's actually a integer so we can
  • 00:05:29 just surround this with int input and
  • 00:05:33 this should work so let's see run that
  • 00:05:35 again and you can also run this through
  • 00:05:38 the command line so if you run this in
  • 00:05:40 your terminal go to the directory that
  • 00:05:42 the file is in and type in Python and
  • 00:05:45 then filename it will also run just as
  • 00:05:47 I'm running it in this sublime repl
  • 00:05:51 player1 make your selection 3 cool and
  • 00:05:54 now you see it says 3 and it says the
  • 00:05:56 class of that is an int so that looks
  • 00:05:58 good to me so now you can save some sort
  • 00:06:02 of selection from the user and you can
  • 00:06:04 also edit this so that it requires you
  • 00:06:09 to make a specific number type in a
  • 00:06:11 specific number but we'll just leave it
  • 00:06:13 as like we're imagine our players are
  • 00:06:15 following the directions right now
  • 00:06:17 alright cool we have that and then we're
  • 00:06:20 also going to want to be able to ask for
  • 00:06:21 player two's input so that's going to
  • 00:06:23 happen else otherwise so if the turn is
  • 00:06:26 not equal to 0 then we want to ask for
  • 00:06:31 player 2 inputs or selection equals
  • 00:06:34 we'll just copy this from above
  • 00:06:38 and we'll just have the changes just to
  • 00:06:41 say player to make your selection
  • 00:06:43 alright cool and now at the end of the
  • 00:06:48 turn no matter whose turn it is we're
  • 00:06:50 gonna want to increase our turn by one
  • 00:06:54 so turn plus or plus equals one so I'll
  • 00:06:58 make it increment it by 1 and then we're
  • 00:07:00 also what we're gonna also want to do is
  • 00:07:02 we're going to just make this odd even
  • 00:07:04 so we're going to use mod division so
  • 00:07:07 it's going to be turn mod to turn equals
  • 00:07:11 turn mod 2 and this is just basically
  • 00:07:15 making it go take the remainder of
  • 00:07:18 whatever our turn is divided by 2 so
  • 00:07:22 this was just gonna alternate it between
  • 00:07:23 0 and 1 so it will alternate between
  • 00:07:26 player ones turn and player two's turn
  • 00:07:27 so let's just see if this works properly
  • 00:07:30 so we're gonna run this build Python
  • 00:07:36 current file player 1 make your
  • 00:07:38 selection to player to make your
  • 00:07:40 selection for player 1 make your
  • 00:07:42 selection – oh that looks good it's
  • 00:07:43 alternating between our player 1 and
  • 00:07:45 player 2 cool ok and now we're going to
  • 00:07:49 want to start building our actual
  • 00:07:50 functionality we're gonna want to be
  • 00:07:51 able to make this selection right here
  • 00:07:54 actually drop a piece into our board so
  • 00:07:57 I'm going to find another function up
  • 00:07:58 here called definition drop piece and we
  • 00:08:05 might define a couple functions so maybe
  • 00:08:07 have a little drop piece and I will just
  • 00:08:08 do pass for now maybe we'll also have
  • 00:08:10 like is valid location so that will
  • 00:08:14 check whatever number that the player
  • 00:08:18 typed in we'll check to see if that's a
  • 00:08:20 valid location we'll also define a
  • 00:08:24 function called get next open row and
  • 00:08:28 how all these functions are going to
  • 00:08:30 work together is as follows so it all
  • 00:08:32 begins with the player making a
  • 00:08:34 selection the 0 to 6 here represents the
  • 00:08:38 column that they want to drop their
  • 00:08:39 piece in so maybe I'm more appropriate
  • 00:08:41 title for this would be Co L
  • 00:08:44 representing column C well
  • 00:08:47 next we're going to take this column and
  • 00:08:49 the current board we have and pass it
  • 00:08:52 into this is valid location function so
  • 00:08:55 board calm and to visualize how we can
  • 00:08:58 check to see if the column is selected
  • 00:09:00 is a valid location we will print out
  • 00:09:05 the board real quick so as you can see
  • 00:09:13 imagine this is the zeroth row is the
  • 00:09:16 first row second row all the way up to
  • 00:09:19 the fifth row here and then this is the
  • 00:09:21 0th column first column second column
  • 00:09:24 etc we're starting at 0 as our first
  • 00:09:26 index so if we wanted to check if the
  • 00:09:30 column that the user selected let's just
  • 00:09:33 assume that the user always types in a
  • 00:09:35 valid number 0 to 6 if we wanted to
  • 00:09:37 check to see if it's valid all we need
  • 00:09:39 to do is make sure that this top row for
  • 00:09:42 that specific column is not been filled
  • 00:09:45 so let's imagine that we're putting ones
  • 00:09:49 in for player one going and twos in for
  • 00:09:51 player two going so if this top row is
  • 00:09:54 still 0 for the specific column we
  • 00:09:57 selected then we know that it's OK for
  • 00:09:59 us to drop a piece in that row or in
  • 00:10:02 that column it hasn't been filled all
  • 00:10:04 the way to the top so do that so this is
  • 00:10:07 the fifth row so all I'm gonna do is
  • 00:10:09 just check to see whether or not the
  • 00:10:12 position at the fifth row and let's say
  • 00:10:15 you know whatever column we select is 0
  • 00:10:19 and if it is that means the column is
  • 00:10:21 still free if it's not then that's not a
  • 00:10:24 validation we'll have to have the user
  • 00:10:26 try something else so to do that lets
  • 00:10:29 just do board so we're going to return
  • 00:10:31 board and then the the row is 5 and the
  • 00:10:38 column is whatever the user selected so
  • 00:10:40 here so and we're going to make sure
  • 00:10:44 that's equal equals 0 so if that's true
  • 00:10:46 then we're good – we're good to let them
  • 00:10:51 drop the piece there if that's not true
  • 00:10:54 that means the column has been filled up
  • 00:10:56 all the way and one thing I'll note that
  • 00:10:58 will probably change in a future video
  • 00:11:00 is
  • 00:11:00 it's kind of getting messy my code a
  • 00:11:02 little bit with just throwing these
  • 00:11:04 random numbers here all the way all over
  • 00:11:06 the place and programming these are
  • 00:11:07 called magic numbers you kind of don't
  • 00:11:08 know where they come from so I'll clean
  • 00:11:11 those up in some of the future videos in
  • 00:11:13 this series so return board five column
  • 00:11:17 equals equals zero and that is just
  • 00:11:19 checking to make sure that that column
  • 00:11:20 has an empty slot okay so now that we've
  • 00:11:22 gotten the valid location let's get the
  • 00:11:25 next open row in that column so if we
  • 00:11:29 think back to our board
  • 00:11:30 that's just checking to see when we drop
  • 00:11:33 it and let's say the 0th column here on
  • 00:11:36 the left it's checking to see which row
  • 00:11:40 of these if the piece will fall on so to
  • 00:11:44 do this we're going to write a quick
  • 00:11:45 little loop so we're going to do four
  • 00:11:52 and we're all set the pass in the board
  • 00:11:56 and the column into this one so the loop
  • 00:12:00 we're gonna write here is for R and
  • 00:12:02 range and I'm actually going to start
  • 00:12:05 getting rid of some of these magic
  • 00:12:07 numbers so I'm gonna initialize a global
  • 00:12:13 very old called row count and often in
  • 00:12:16 Global's are capitalized just two so
  • 00:12:18 that our static variables that don't
  • 00:12:21 change or capitalize just to show that
  • 00:12:22 they're a non changing variable so the
  • 00:12:24 number of rows we have a six and the
  • 00:12:26 number of columns we have is seven so
  • 00:12:31 I'm gonna do for R so for row in range
  • 00:12:35 so the number of rows we have so row
  • 00:12:37 count and we're gonna check the board
  • 00:12:42 position we're going to see if board our
  • 00:12:49 position R so the column equals equals
  • 00:12:54 zero then we want to return that row so
  • 00:12:58 basically if the row is equal to zero
  • 00:13:01 so imagine we're filling this whole
  • 00:13:03 board up with ones and twos
  • 00:13:05 if the slot is still zero that means
  • 00:13:08 it's empty still so we're gonna return
  • 00:13:10 the first instance that it's empty so
  • 00:13:13 our is gonna count from zero to row
  • 00:13:17 count minus one and we're gonna return
  • 00:13:20 in that first case where it equals zero
  • 00:13:23 okay so now we got the next open row and
  • 00:13:26 then finally what we're gonna want to do
  • 00:13:28 is drop the piece so this is going to
  • 00:13:30 take in the board the row and the column
  • 00:13:33 and then we'll also add something we'll
  • 00:13:36 just call it the piece so whether it's
  • 00:13:39 player one or two going so all this is
  • 00:13:42 gonna do is go board row column equals
  • 00:13:49 equals piece so we're gonna make it fill
  • 00:13:52 in the board with whatever piece that
  • 00:13:55 the player just dropped so let's put
  • 00:13:58 this all together in our loop so first
  • 00:14:00 we got the column now we're gonna check
  • 00:14:03 if is valid location of the board in the
  • 00:14:07 column and then we can start getting the
  • 00:14:10 next empty row so row equals get next
  • 00:14:14 open row and that takes in the board and
  • 00:14:16 column as well so get next in pit in row
  • 00:14:19 board column and then we're gonna
  • 00:14:23 finally drop piece and we're gonna take
  • 00:14:27 in the board the row the column and then
  • 00:14:30 for player one the piece is just the one
  • 00:14:32 and we're gonna copy this in to player
  • 00:14:38 two as well because player 1 and player
  • 00:14:40 two the functionality of what they're
  • 00:14:43 doing is the same but we'll just have to
  • 00:14:44 change the piece here to – so now and
  • 00:14:50 then finally probably after the player
  • 00:14:51 goes we'll probably want to print out
  • 00:14:55 the board so we'll print the board here
  • 00:14:58 and let's see what happens now
  • 00:15:01 so tools build I'll shoot sorry sublime
  • 00:15:06 repl Python run card file so player one
  • 00:15:11 selection we're gonna drop our piece
  • 00:15:12 right in the middle so at 0 1 2 3 this
  • 00:15:15 is the third column so 3 what happened
  • 00:15:20 why did it not fill in let's figure out
  • 00:15:22 what exactly went wrong there so
  • 00:15:26 should have updated the board but for
  • 00:15:28 some reason it did not so let's see okay
  • 00:15:35 I see what I did wrong here so I
  • 00:15:39 accidentally put in two equal sign here
  • 00:15:42 and what we're really trying to do is an
  • 00:15:44 assignment we're not trying to set see
  • 00:15:46 if the board row column position is
  • 00:15:48 equal to that so I didn't assign it so
  • 00:15:50 now it should work so we'll rerun this
  • 00:15:53 so tools I'm repl Python on current file
  • 00:15:59 player 1 make your selection
  • 00:16:01 we'll say go to the middle 3 player to
  • 00:16:05 make your selection we'll do 3 as well
  • 00:16:07 so I wanna make your selection and as
  • 00:16:09 you can see one issue we're having right
  • 00:16:11 now is that the pieces are starting at
  • 00:16:13 the top and going downwards and so the
  • 00:16:15 reason for this is our convention was to
  • 00:16:18 say that this was the zero zero index
  • 00:16:20 but in the actual numpy they represent
  • 00:16:24 this right here as the zero zero index
  • 00:16:27 so I'm just gonna add an additional
  • 00:16:29 function real quick called print board
  • 00:16:33 and all that's gonna do is change the
  • 00:16:36 orientation so that what we're seeing is
  • 00:16:39 actually the they flipped over the way
  • 00:16:42 we're expecting to see a connect 4 board
  • 00:16:44 kind of be building from bottom up so
  • 00:16:47 print board and we just need to pass in
  • 00:16:49 the board and all we're gonna do is
  • 00:16:50 there's a command in dumpee so the
  • 00:16:56 command is let me just remember it it is
  • 00:17:00 n P dot flip I'm gonna do board and then
  • 00:17:07 you have to flip it over you have to
  • 00:17:09 specify the axis of the zeros of the
  • 00:17:10 axis we're gonna flip the board over the
  • 00:17:15 x-axis so that should get it up side the
  • 00:17:18 right side up again so instead of doing
  • 00:17:21 print board here we're gonna now call
  • 00:17:23 our new print function so print board
  • 00:17:26 and then board and we can also do that
  • 00:17:31 down here if we want to print
  • 00:17:34 I doesn't matter yes to start but
  • 00:17:38 whatever
  • 00:17:39 okay now let's run it again tools
  • 00:17:42 primary pl Python on current file Claire
  • 00:17:46 will make your selection three three
  • 00:17:48 three and as you can see now it's
  • 00:17:51 building up from with ones and twos just
  • 00:17:54 how we expect okay that's all I'm gonna
  • 00:17:57 do for this video in the next video I'm
  • 00:17:58 going to kind of build in the
  • 00:18:00 functionality of actually checking for
  • 00:18:01 wins with these ones and twos and then
  • 00:18:04 in the next video probably after that
  • 00:18:05 we're gonna actually add the graphics so
  • 00:18:07 make sure you stick tuned for those
  • 00:18:09 videos thanks for watching this one guys
  • 00:18:12 seen a bit