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

preview_player
Показать описание
Learn data skills with hands-on exercises & tutorials at Datacamp!

In this video we finish the command line version of our connect four game. We implement a way to check for a four in a row and talk a little bit about good design practices.

Next Video:

Previous Video (First Part):

Full Game Source Code:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

PYTHON TUTORIAL SERIES:

-------------------------
Follow me on social media!

-------------------------

Learn data skills with hands-on exercises & tutorials at Datacamp!

*I use affiliate links on the products that I recommend. I may earn a purchase commission or a referral bonus from the usage of these links.
Рекомендации по теме
Комментарии
Автор

Congrats, your channel has some pretty nice content, subscribed.

walterwhite
Автор

Thanks for this video now it's helping in my python class thank you.

Dhanush
Автор

If we take WIN_NO as the number of pieces we need to arrive to win (traditionally 4), the code can be manipulated as -
def winning_move(board, piece):
for c in range(COLUMN_COUNT+1-WIN_NO):
for r in range(ROW_COUNT):
ctr = False
for i in range(WIN_NO):
if board[r][c+i] == piece:
ctr = True
else :
ctr = False
break
if ctr == True:
break
if ctr == True:
break
if ctr == True:
return True
This one is for horizontal rows, vertical and diagonal can be done similarly.
Using this you can make connect 4, connect 5, connect 8, connect 3 etc as long as the no. of rows and columns are appropriate; for eg, you can't have connect 10 in 7X8 grid.

devanshukumar
Автор

super easy to follow along and the explanations help without bombarding me with too more information

just having a problem with some edge cases...whenever 2 of the same coloured piece are placed at the top of the board (after being stacked) it is resulting in a win for that player...i've gone through this video and my code multiple times...and it doesnt make any sense, when i # out the vertical section of the winning move it doesn't happen, so i'm guess its something in that?

anybody got any ideas?

RJLVideography
Автор

Is there others way to code winning moves?

samliew
Автор

How would you go about highlighting the winning move?

nilanpindoria
Автор

What if the winning ship was placed in between chips. In this case it is not an endpoint and the algo with not work?

miguelacevedo
Автор

You only need to check the row and column of the spot just played.

Kanglese
Автор

Hey little confused at 7:33. I don't see why remove 3 from a column of 7, and remove 3 from a row of 6?
It works, but I figured you would remove 1 more from the longer side?

miscany
Автор

Awesome video, although I find the np.flip does not do anything, any advice? I am using verson 3.7

tobywebb
Автор

hi how do i implement wins if my board is 7 rows by 6 columns? im confused thank you..

kohntv
Автор

Dude, just iterate over a sequence of vectors and scan along those vectors starting where the last piece was placed. It makes a very compact and readable function. I do something similar in my Othello game to test whether a move is valid, make a move, and simulate a move in my player bot.

kyleeames
Автор

How many possible wins are there in connect 4 ?

rollergamer
Автор

After I start the game P1 plays and then after only P2 plays, help how do I correct that???

TheLolobag
Автор

def winning_move(board, piece):
p = re.compile("(" + str(piece) + ".( )?){4}")
# vertical
for c in range(COLUMN_COUNT):
if p.search(str(board[:, c])) is not None:
return True

# horizontal
for r in range(ROW_COUNT):
if p.search(str(board[r, :])) is not None:
return True

# diagonal
for offset in range(-ROW_COUNT+4, COLUMN_COUNT-3):
if (p.search(str(np.diagonal(board, offset))) is not None or
p.search(str(np.diagonal(np.flip(board, 1), offset))) is not None):
return True

thisistotalfck