All of the games in Free Python Games are implemented using Python and its. 100% Test Coverage; Developed on Python 3.4; Tested on CPython 2.7, 3.4, 3.5,. Connect – Connect 4 game. Click a row to drop a disc.
Active3 years, 7 months ago
$begingroup$![Connect Connect](https://files.realpython.com/media/Python-Sockets-Tutorial_Watermarked.815ce6c85789.jpg)
This Connect 4 game will be used for implementing game-playing AI. Sample players are supplied. One takes user input, and the other plays randomly. Right now it's set for a human player to play against a randomly playing agent, but in the future I plan to try out algorithms such as Minimax and have AI agents play against themselves.
Some more features (such as notifying the players when the game is over, instead of just printing to
stdout
) need to be added for later work, but I'm showing this simply as a working base. I'm not so much looking for feature suggestions, since those will come later. I will be adding docstrings and comments, but for now it should be straight forward.I'm looking for tips on: program correctness, code structure, and best practices (whether specific to Python or not).
![Connect 4 program python 2 numbers Connect 4 program python 2 numbers](/uploads/1/2/5/8/125825336/443195615.png)
(Python 3.2.3)
200_success136k21 gold badges175 silver badges448 bronze badges
redmintredmint
$endgroup$2 Answers
$begingroup$I would advise using slightly longer names (
rind
should be row_ind
, etc.), adding comments (while you are writing the code, not after) and avoiding counterintuitive variable names such as four
. It is quite confusing when you are writing statements such as four 2
.In addition, I would replace
with
to future-proof your code in case you switch to using lists or something else entirely.
Also, to simplify your structure, I would suggest splitting up your longer functions into other smaller functions, to make your code more digestible and reusable.
Also, instead of initializing players with tuples of seemingly random numbers, add some constants in your code for further increased readability.
Jamal♦32k12 gold badges123 silver badges231 bronze badges
mleyfmanmleyfman4,8751 gold badge16 silver badges46 bronze badges
$endgroup$$begingroup$- Having
self.player1
andself.player2
is an early indication of a problem. Programmers can't count to two; we know just 'zero', 'one' and 'many'. Anything appearing more than once shall be packed into a container (think of adapting the same code to more than 2 players). A list would do well. - An
if/else' clause in
move` seems unnecessary:Having a player list it is justwith noif
s whatsoever (in your casenext
meansopponent
). Again, a multiple-player game is not that different from a two-player one. - In a turn-oriented games, a standard practice is to explicitly validate moves: provide a
game.get_move(player)
method to repeatedly prompt player for a move until a valid one is submitted. This also implies separate move validation and move execution methods.
32k12 gold badges123 silver badges231 bronze badges
vnpvnp43.6k2 gold badges37 silver badges112 bronze badges
$endgroup$Not the answer you're looking for? Browse other questions tagged pythonpython-3.xnumpyconnect-four or ask your own question.
Active4 years, 9 months ago
So I'm trying to make a simple Python connnect 4 game on Processing 2, and now I'm trying to make the critical algorithm to check for the winner of a single round. It's not like tic-tac-toe, so I'm not sure where to start. If I should ask somewhere else, tell me as well.
Basically, every time a player places a piece, after the board refreshes the game checks if there are any four consecutive pieces in any direction (horizontally, vertically, diagonally). If not, the game continues. If SO, the game switches to the winning screen.
This is my code so far (remember, this is Python, and # is comment):
SqreerqS
SqreerqSSqreerqS
1 Answer
Here, I believe, is the function I think you're missing (needs to be checked out more carefully, maybe with
print
statements &c, but though there may be off-by-one's &c I think it's the right general idea):Once made correct in off-by-one terms, it can be improved by removing repetition -- those for/for/if/return blocks of four lines are very repetitious and should be factored our into a nested function...
Alex MartelliAlex Martelli666k136 gold badges1067 silver badges1297 bronze badges