Coding

How to Program Connect 4 in Python! (part 2) – Check for Winning Move

  • 00:00:00 what's up guys and welcome to the second
  • 00:00:02 video in the programming connect for in
  • 00:00:04 Python series at the end of the last
  • 00:00:07 video we had something that looked like
  • 00:00:08 this it's just a simple command line
  • 00:00:12 based connect for game and the one
  • 00:00:16 problem with this is that if I actually
  • 00:00:18 won this game so as you can see I just
  • 00:00:21 won with player one down the bottom the
  • 00:00:24 game doesn't let me know at all that I
  • 00:00:25 want so we need to implement that
  • 00:00:27 functionality so we're gonna define a
  • 00:00:29 function called winning move and we're
  • 00:00:36 gonna have that taking the board and
  • 00:00:38 then the last piece that was dropped so
  • 00:00:43 winning move there's a bunch of
  • 00:00:44 different ways to implement this and the
  • 00:00:47 way we're gonna do is not the most
  • 00:00:49 efficient method but it's probably the
  • 00:00:51 easiest described in a video so that's
  • 00:00:53 kind of why I'm selecting it but we're
  • 00:00:55 gonna manually check all the different
  • 00:00:58 possible places you could win in connect
  • 00:01:01 for so all the horizontals all the
  • 00:01:03 verticals and all the diagonals and
  • 00:01:05 check to see if there is a winning
  • 00:01:08 combination on the board and because
  • 00:01:11 we're doing this after every turn we're
  • 00:01:12 gonna pick up the first instance of that
  • 00:01:14 winning combination so we'll know who
  • 00:01:16 won probably the more practical way
  • 00:01:19 would be to just check around the spaces
  • 00:01:23 where the last piece was dropped but
  • 00:01:25 that code got a little bit Messier when
  • 00:01:27 I was doing it before so I'm gonna just
  • 00:01:29 do it this way so we're gonna first
  • 00:01:31 check all the horizontal locations
  • 00:01:38 and to do that we'll have a loop that
  • 00:01:41 iterates over the columns so for C in
  • 00:01:45 range column count and so we defined a
  • 00:01:49 column count up here of in that seven
  • 00:01:52 and one thing I think would be a cool
  • 00:01:54 challenge for you guys to try that I
  • 00:01:56 don't think my game is going to be able
  • 00:01:58 to do at this point in time is a very
  • 00:02:00 well-designed like connect for game you
  • 00:02:02 would be able to change these numbers so
  • 00:02:05 like I could change it to say like 12 or
  • 00:02:07 something and this to like eight and the
  • 00:02:10 game would still be able to work you
  • 00:02:12 still have like a connect for game but
  • 00:02:14 just a bigger board and so that's like
  • 00:02:16 kind of a cool way to make sure like
  • 00:02:18 you've designed things in a smart way
  • 00:02:20 where you don't have just these random
  • 00:02:22 like magic numbers popping up all over
  • 00:02:24 the place and I'm actually gonna change
  • 00:02:25 this real quick row count and column
  • 00:02:30 count
  • 00:02:31 just because I'm going to try to
  • 00:02:32 eliminate the magic numbers where I can
  • 00:02:34 and we'll clean this up to row count
  • 00:02:38 minus one so this is the last row a lot
  • 00:02:42 of index with last row okay getting back
  • 00:02:44 to winning move so we want to check
  • 00:02:47 horizontal locations for the win so
  • 00:02:49 we're gonna first loop over all the
  • 00:02:51 columns and then we're also gonna have a
  • 00:02:53 loop over all of the rows and what this
  • 00:02:58 loop is going to be over is not every
  • 00:03:01 single position but over all the
  • 00:03:03 possible starting positions for a
  • 00:03:05 horizontal wind so if we think about our
  • 00:03:08 board a horizontal win can start this
  • 00:03:12 spot this bought this but this but it
  • 00:03:15 can't start here because you can't go
  • 00:03:17 for over from this location so this is
  • 00:03:21 our last column the third column in
  • 00:03:24 terms of indices and then upwards we can
  • 00:03:27 go all the way to the top vertically
  • 00:03:29 because it can so this half-sari you
  • 00:03:33 can't really see but we're I'm making a
  • 00:03:35 box around that half is where our
  • 00:03:37 starting locations can be for this first
  • 00:03:41 this first check
  • 00:03:43 so we're iterating over the columns so
  • 00:03:46 it can only be the number of columns and
  • 00:03:48 then we have to subtract three because
  • 00:03:50 three of those columns
  • 00:03:52 couldn't actually work so we're
  • 00:03:55 subtracting three off of that one and
  • 00:03:56 then all the rows could work so that's
  • 00:03:58 totally fine so now we're going to check
  • 00:04:00 all the four in a row locations so this
  • 00:04:05 is going to look like something like
  • 00:04:06 that so if board row column so this is
  • 00:04:14 just indexing the matrix equals equals
  • 00:04:18 piece and board row c plus one so this
  • 00:04:26 is taking the next one to the right so
  • 00:04:29 we're checking horizontally so we're
  • 00:04:30 only changing the columns equals equals
  • 00:04:33 piece and board our c plus two equals
  • 00:04:41 equals piece and finally and board this
  • 00:04:45 is the fourth piece in a row board c
  • 00:04:49 plus three equals equals piece then
  • 00:04:57 we're going to return true so we want to
  • 00:05:02 return true the first time this happens
  • 00:05:04 we're not going to return this
  • 00:05:06 expression because that would return
  • 00:05:08 that would break out of this loop before
  • 00:05:10 we wanted it to okay and so what right
  • 00:05:13 right again this is not the moment the
  • 00:05:15 best way probably to write it you know
  • 00:05:17 we have these magic numbers popping up
  • 00:05:18 but it's just a very simple way to check
  • 00:05:21 for all those horizontal locations all
  • 00:05:23 right so let's do the same thing with
  • 00:05:24 vertical locations locations for wind
  • 00:05:32 and actually before I do that let's just
  • 00:05:34 check to see if we can get some
  • 00:05:36 functionality on this so if valid
  • 00:05:39 education we're gonna also write a if
  • 00:05:41 statement that says if winning move and
  • 00:05:48 so we're gonna say board and then the
  • 00:05:54 piece is 1 so if winning move we're
  • 00:05:57 going to just print out
  • 00:06:01 player 1 wins Congrats yay okay let's
  • 00:06:11 just see if that works with the
  • 00:06:12 horizontal real quick and then we're
  • 00:06:14 also gonna want to set this game over
  • 00:06:16 variable to true because the game is in
  • 00:06:20 fact over if the move is winning so
  • 00:06:22 let's just run that real quick Python on
  • 00:06:26 the current file ok player 1 make your
  • 00:06:28 selection free yeah it says player 1
  • 00:06:38 wins and we I guess accidentally printed
  • 00:06:40 the board out one extra time but you
  • 00:06:42 know we did get that functionality it
  • 00:06:44 did check that horizontal win so I think
  • 00:06:45 that looks good
  • 00:06:47 obviously you'd want to test these like
  • 00:06:51 very thoroughly but for the sake of the
  • 00:06:54 time of this video I'm going to just
  • 00:06:57 start going to the horizontal the
  • 00:06:58 vertical location so I think about where
  • 00:07:01 vertical locations can start can start
  • 00:07:05 we need for up so I can start it's
  • 00:07:07 pretty much the opposite of the very
  • 00:07:09 horizontal so vertical could start all
  • 00:07:12 this bottom section because we need for
  • 00:07:15 up but it can be in any column so it's
  • 00:07:18 gonna be pretty similar to the last loop
  • 00:07:20 but we're gonna have to change the row
  • 00:07:24 so you'll see in a sec shift we're gonna
  • 00:07:31 have to change instead of the column
  • 00:07:33 count doing for seeing column count –
  • 00:07:36 they're gonna have to do row count – 3
  • 00:07:38 because we can't start at the top row
  • 00:07:40 and then we're gonna have to add these
  • 00:07:43 pluses to the rows actually row plus 1
  • 00:07:49 Rho plus 2 and the last one is row plus
  • 00:07:57 3 and once again like see if you could
  • 00:08:01 figure out how to manipulate this these
  • 00:08:04 equations to work for any length of a
  • 00:08:08 board and also let's say maybe we want
  • 00:08:10 to make a game those like connect 6 like
  • 00:08:13 could be a cool challenge for you guys
  • 00:08:14 to try to
  • 00:08:15 build these functions so that they could
  • 00:08:17 be any number of columns any number of
  • 00:08:21 rows and any number of pieces for the
  • 00:08:23 winning length okay so let's check to
  • 00:08:26 see if the vertical locations now work
  • 00:08:27 we're gonna run this again real quick so
  • 00:08:30 tools Python concurrent file
  • 00:08:35 three cool it works again so we see have
  • 00:08:44 the four in a row with the ones right
  • 00:08:46 here and it says player 1 wins congrats
  • 00:08:48 so that looks good now we have to get to
  • 00:08:51 the little bit of the trickier things
  • 00:08:54 we're not the check for the first we'll
  • 00:08:58 check for the positively sloped
  • 00:09:04 diagonals and we're also going to make a
  • 00:09:09 separate method to check for the
  • 00:09:12 negatively sloped and you'll see why we
  • 00:09:17 need to do this in a sec diagonals
  • 00:09:20 alright so let's think about it we're
  • 00:09:23 gonna think about it the same way where
  • 00:09:24 our possible starting locations that we
  • 00:09:28 could have a positively sloped diagonal
  • 00:09:30 well we can start here and go up we
  • 00:09:34 start here and go up here and go up so I
  • 00:09:37 think the last piece we can start here
  • 00:09:40 and go up so this is gonna be the top
  • 00:09:42 row and we can't go any farther than
  • 00:09:45 this piece right here so that looks like
  • 00:09:47 we're doing row count minus 3 and column
  • 00:09:50 count minus 3 so right so we're gonna
  • 00:09:58 we'll just copy this code see oh no what
  • 00:10:02 happened see and I'll paste that in
  • 00:10:05 right here alright so we're gonna have
  • 00:10:07 to subtract 3 from column count and from
  • 00:10:09 row count and then how this is gonna
  • 00:10:11 increment as you start with that initial
  • 00:10:13 starting location and then we need to +1
  • 00:10:16 to both things because it's a slope it's
  • 00:10:20 not just a horizontal or vertical now so
  • 00:10:23 we have to do the addition step for both
  • 00:10:26 of these
  • 00:10:27 okay cool and as we've been doing let's
  • 00:10:30 just check to see if we can get a
  • 00:10:32 diagonal win here so player one make
  • 00:10:37 your selection I'll just start in the
  • 00:10:48 all right we're almost there
  • 00:10:50 oh so close
  • 00:10:57 don't screw this up now Keith okay and
  • 00:11:01 we'll check here cool the diagonal there
  • 00:11:04 worked so that looks like it's working
  • 00:11:06 too and then finally we need to do the
  • 00:11:09 negatively sloped diagonals and so if we
  • 00:11:15 think about negatively sloped diagonals
  • 00:11:17 they can start at the top and go
  • 00:11:18 downwards but they couldn't start any
  • 00:11:21 lower than this right here this that's
  • 00:11:24 the last one they could start at because
  • 00:11:26 they have to go down four so that looks
  • 00:11:30 like it would be the top rows and then
  • 00:11:34 also the columns minus three so we're
  • 00:11:39 going to start the rows at three and
  • 00:11:41 we're gonna have to start the columns in
  • 00:11:44 the normal normal location so you're
  • 00:11:47 gonna start from three to the row count
  • 00:11:52 and so it's three because if you think
  • 00:11:56 about it this is the zeroth row this is
  • 00:11:59 the first row second row third row so
  • 00:12:02 even though it's actually the fourth up
  • 00:12:03 its third index so that's why we're
  • 00:12:06 starting at three and that's going to go
  • 00:12:08 all the way to the top and the column
  • 00:12:10 counts actually good as it is and so
  • 00:12:12 this is gonna have to now go it's gonna
  • 00:12:15 go positively over in the column
  • 00:12:18 direction but it's going to go down a
  • 00:12:19 row so negative one here negative two
  • 00:12:24 here and this is just a negative slope
  • 00:12:27 you you know you have one direction
  • 00:12:29 positive one direction negative and this
  • 00:12:31 will be negative three so let's test to
  • 00:12:34 see if this works
  • 00:12:35 come on
  • 00:12:40 right file okay I'll just build up the
  • 00:12:45 left side first okay okay okay okay okay
  • 00:13:01 we're almost into the negative diagonal
  • 00:13:03 there so player 1 you can go 3 then
  • 00:13:06 player 2 2 then 2 cool the negative
  • 00:13:11 diagonals work now – okay so that looks
  • 00:13:13 like we have all the possible winning
  • 00:13:15 directions you should North the early
  • 00:13:17 test to make sure the all the edge cases
  • 00:13:18 work but to me that looks pretty good so
  • 00:13:21 we're also going to implement the
  • 00:13:24 functionality for player 2 in this main
  • 00:13:27 game loop okay so this is player 2
  • 00:13:33 player 2 and some grads game over true
  • 00:13:37 okay and if you wanted to you could just
  • 00:13:41 break out of the loop if you didn't want
  • 00:13:45 to see this board and the turns changing
  • 00:13:47 at the end okay so that's we've now
  • 00:13:50 finished the command line version of the
  • 00:13:54 game so we'll start building out
  • 00:13:55 probably the next video we might clean
  • 00:13:57 up a code a little bit and then we'll
  • 00:13:59 build out the graphics okay
  • 00:14:02 see you in a bit