Hello all, here’s a link to my Connect 4 terminal game, let me know what you think:
Hey!
When I tried running your game using python 3, the code didn’t work (I got an indentation error at line 43). After looking at the code, I figured it out pretty quickly. When you used ‘’’ to write your comments, you accidentally created a text block instead of a comment block (at least for my command line). I think you should quickly go thru and comment all of those lines out.
Thanks for the heads up! Definitely going to adjust that ASAP.
- Dan
Serious question, and also curious: why is Connect_4 a class?
In is_winning_move(self, board, row, col, player):
you even seem to forget why you made a class in the first place and pass an instance value as well, ignoring self
.
Perhaps try:
COLS, ROWS = 7, 8
def create_board():
# your code here
def print_board(board):
# your code here
# this one is a freebie, you don't even need state
def is_valid_move(col):
return not (col <= 0 or col > COLS or not isinstance(col, int))
def drop_piece(board, player, col):
# your code here
def is_winning_move(board, row, col, player):
# your code here
def is_board_full(board):
# your code here
def main():
board = create_board()
# your code here
if __name__ == "__main__":
main()
That player swap is interesting player = 3 - player
. I’d actually declare players as constants as well. PLAYER_NONE, PLAYER_X, PLAYER_O = ' ', 'X', 'O'
Your swap is then player = PLAYER_O if player == PLAYER_X else PLAYER_X
.
Also, if cell = ' ':
is a bug. And, weirdly, you’ve declared the bug’s host, is_board_full
, twice.
Play with it and have fun. Next challenge, computer player?
I read that the triple quotes (‘’’ or “”") should have worked for commenting out the code. I did some more investigating to see what might fix this and found a solution. If you indent all the commented to the same level as the class, then the comments won’t affect your code anymore.
After I did get the code running, I found one error on the function “is_board_full”. There’s an if statement with a single equals. Update that to a double equals and it ran perfectly.
I did not examine your code any further so see other comments on how to make it better.