Python Forum
Python Connect 4 refactoring
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Connect 4 refactoring
#5
(Oct-28-2018, 01:47 AM)ichabod801 Wrote: I tried a couple times to get a diagonal win for player 2 with 3 pieces and couldn't do it. But the second for loop in diag_check is messed up. It's always pulling from the same square (grid[3][0]), so whoever can get a piece their wins. I would just use one for loop. Use it to gather two checks, one using grids[diag][diag], and one using grids[diag][3-diag]. That will get you both diagonals. And move the conditional outside the loop. It's not until the last iteration of the loop that you have four items in check, so you might as well check it once after the loop is done.

As for getting rid of the globals, take loop. The three check functions return loop, but only if it's False. If they returned True if the game was not over, then checks_def could be:

def checks_def():
    return check_def() and checkEmpty_def() and diagcheck_def()
As soon as a chain of and operators hits a False, it short circuits and returns False. So if check_def returns False, it won't waste time checking the other two. Note that at this point it's returning the value of loop you want, so in the main while loop you can just have loop = checks_def() (although you would need to rearrange it so that's at the end, or change the condition to picode]if not checks_def():[/icode])

As for grid, you would pass that down the function chain as a parameter (checks_def(grids) would call check_def(grids)).

Ah unfortunately I'm still stuck on it. Could you elaborate, please?
Reply


Messages In This Thread
Python Connect 4 refactoring - by Beginner_coder123 - Oct-27-2018, 03:53 PM
RE: Python Connect 4 refactoring - by ichabod801 - Oct-27-2018, 09:02 PM
RE: Python Connect 4 refactoring - by ichabod801 - Oct-28-2018, 01:47 AM
RE: Python Connect 4 refactoring - by Beginner_coder123 - Oct-28-2018, 09:25 PM
RE: Python Connect 4 refactoring - by ichabod801 - Oct-28-2018, 09:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  refactoring kerzol81 8 5,072 Nov-02-2019, 09:49 AM
Last Post: Gribouillis
  Generalized Dice refactoring Beginner_coder123 2 3,146 Feb-05-2019, 08:45 AM
Last Post: Beginner_coder123

Forum Jump:

User Panel Messages

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