PRE-INSTALLATION CHECK LIST FOR HELP-1.800.929.3667 OR OVERHEADDOOR.COM. 5 CONNECT OPERATOR TO POWER. Console easy to find in dark. Controls door operator from inside garage.

  1. Python 2 Garage Door Opener Programming
  1. This is like my 4th week using Python and I'm still not familiar with it so I might need some help from you guys. I gotta write a little Connect 4 game in Python, I just got started and boom - I'm stuck! I tried to create a board for the game using two lists. Here is how far I got: def board.
  2. Fun With Python: Connect 4!!! This is an attempt at making a console connect4 game in Python - as a hobby. If you're interested, I need help writing the AI. Edit: Here is an update on the code. I have a general layout of what I want the AI to behave like, but I'm still in the planning phase. ยป Programming & Scripting.
Active4 years, 1 month ago

Python 2 Garage Door Opener Programming

I'm still pretty green working with python but I figured making my own game from scratch with what I know would be good practice. I've got this connect four game together and it works in so far as switching between players and 'dropping' their respective pieces. Now I need a win condition, though honestly, I don't know where to start. I'd prefer some guidance as opposed to straight code for the sake of learning, but of course, any help would be greatly appreciated. Thanks. Quick note, the current while loop is simply for debugging. I was thinking about setting a variable 'winner' to False and doing the loop 'while winner False:' and have the win condition set this variable to True.

user2669412user2669412

2 Answers

You have a 7x6 board, and you need to see if a line of 4 adjacent or diagonal spaces contain the same symbol. This 7x6 board is being stored in a two dimensional array.

For a piece placed in the board, you'll have 4 sets of checks you must do to see if it was a winning piece:

  1. Check to the left/right for 3 of your pieces.
  2. Check to the upper left/lower right for 3 of your pieces.
  3. Check to the lower left/upper right for 3 of your pieces.
  4. Check above and below for 3 of your pieces.

If we consider X and Y your current board coordinate, wherever I place a piece, these checks correspond to:

Python garage door opener sensorsOverhead garage door troubleshooting
  1. X increases/decreases, Y is constant.
  2. As X increases, Y decreases, and vice versa.
  3. As X increases, Y increases, and vice versa.
  4. X is constant, Y increases/decreases.

You should do a lazy check, checking each direction and going the other way as soon as you find something that isn't your piece. If both directions are complete and your total pieces are not 4, that wasn't a winning vector and go on to the next check.

EDIT: Formatting and clarity.

JRodge01JRodge01
Connect 4 Program Python Garage

This sounds like a disjoint sets problem but your graph is so small that it would probably be easier to just check all four directions each time like @John Rodgers was talking about. Disjoint sets would probably be a useful thing to learn though. Here's a wikipedia page if you just want to read about it https://en.wikipedia.org/wiki/Disjoint-set_data_structure

SirParselotSirParselot
2,1191 gold badge11 silver badges24 bronze badges

Not the answer you're looking for? Browse other questions tagged python or ask your own question.