HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Connect Four in Python

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
fourconnectpython

Problem

I've made a working version of Connect Four in Python. I'm pretty new to Python so it's decently primitive. Please take a look and let me know what you think!

```
import random

board = [["." for x in range(7)] for x in range(6)]
symbol = ""
winCondition = ""
fullCols = []

def boardLogic(pick, user):
if user == "user":
symbol = "0"
else:
symbol = "X"
if board[0][pick] != ".":
if board[1][pick] != ".":
if board[2][pick] != ".":
if board[3][pick] != ".":
if board[4][pick] != ".":
if board[5][pick] != ".":
print("That column is full.")
if pick not in fullCols:
fullCols.append(pick)
fullCols.sort()
print fullCols
else:
board[5][pick] = symbol
else:
board[4][pick] = symbol
else:
board[3][pick] = symbol
else:
board[2][pick] = symbol
else:
board[1][pick] = symbol
else:
board[0][pick] = symbol

def printBoard():
for row in board:
for val in row:
print '{:4}'.format(val),
print

def does_square_contain_win(i, j):
#right_diag: [[i,j], [i-1,j+1], [i-2,j+2], [i-3,j+3]]
if i-3 in range(0, 6) and j+3 in range(0,5):
if board[i][j] == "0" and board[i-1][j+1] == "0" and board[i-2][j+2] == "0" and board[i-3][j+3] == "0":
print("\nPlayer Wins\n")
quit()
if i-3 in range(0, 6) and j+3 in range(0,5):
if board[i][j] == "X" and board[i-1][j+1] == "X" and board[i-2][j+2] == "X" and board[i-3][j+3] == "X":
print("\nComputer Wins\n")

Solution

Raise the abstraction

if board[0][pick] != ".":
    if board[1][pick] != ".":
        if board[2][pick] != ".":
            if board[3][pick] != ".":
                if board[4][pick] != ".":
                    if board[5][pick] != ".":
                        print("That column is full.")
                        if pick not in fullCols:
                            fullCols.append(pick)
                        fullCols.sort()
                        print fullCols
                    else:
                        board[5][pick] = symbol
                else:
                    board[4][pick] = symbol
            else:
                board[3][pick] = symbol
        else:
            board[2][pick] = symbol
    else:
        board[1][pick] = symbol
else:
    board[0][pick] = symbol


So if the first is empty you assign the first, if the second is empty you assign the second ...

In other words you assign the first empty, let me write some pseudocode:

def boardLogic(pick, user):
    # ... Logic before (this should maybe be moved elsewhere)

    try:
        first_empty = first(lambda i: board[i][pick] == EMPTY, range(5+1))
        board[first_empty][pick] = symbol
    except StopIteration:
         print("That column is full.")
         # ... No empty space


Where

EMPTY = '.'


And first can very easily be found on StackOverflow

In high level languages you should try to write code that resembles the high level description of the problem, and avoid a lot of chained conditionals.

Code Snippets

if board[0][pick] != ".":
    if board[1][pick] != ".":
        if board[2][pick] != ".":
            if board[3][pick] != ".":
                if board[4][pick] != ".":
                    if board[5][pick] != ".":
                        print("That column is full.")
                        if pick not in fullCols:
                            fullCols.append(pick)
                        fullCols.sort()
                        print fullCols
                    else:
                        board[5][pick] = symbol
                else:
                    board[4][pick] = symbol
            else:
                board[3][pick] = symbol
        else:
            board[2][pick] = symbol
    else:
        board[1][pick] = symbol
else:
    board[0][pick] = symbol
def boardLogic(pick, user):
    # ... Logic before (this should maybe be moved elsewhere)

    try:
        first_empty = first(lambda i: board[i][pick] == EMPTY, range(5+1))
        board[first_empty][pick] = symbol
    except StopIteration:
         print("That column is full.")
         # ... No empty space
EMPTY = '.'

Context

StackExchange Code Review Q#120561, answer score: 2

Revisions (0)

No revisions yet.