Python Forum
Connect Four Win Check Function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Connect Four Win Check Function
#3
Quote:My board is stored as a 2d array, so the function will use that.
Your board is a list of strings; that's different than a 2-d array. I may sound like a pedantic ass, but it's important to be precise when talking about code. It also explains why you have to calculate the position with this pos=(4*x)-2.

Putting that aside, when someone adds a checker start with the position of the new checker and see if it makes a vertical, horizontal, or diagonal line. I would break the code up into individual functions for each check:

def check_for_win(board, starting_pos):
    if check_vertical(board, start_pos):
        return True
    if check_horizontal(board, starting_pos):
        return True
    if check_diagonal(board, starting_pos):
        return True
    return False
You could, of course, combine those with an or but I think this is easier to read. Each check_* function then just has a loop that changes the appropriate index to check of the counter is the same as the one in starting_pos.
Reply


Messages In This Thread
RE: Connect Four Win Check Function - by mpd - Jan-04-2018, 01:59 PM
RE: Connect Four Win Check Function - by buran - Jan-04-2018, 02:11 PM
RE: Connect Four Win Check Function - by mpd - Jan-04-2018, 04:14 PM
RE: Connect Four Win Check Function - by mpd - Jan-04-2018, 05:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Date format and past date check function Turtle 5 4,576 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  Is there any way to check if a function is user-defined? dullboy 11 10,659 Oct-06-2016, 03:42 AM
Last Post: dullboy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020