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******************************")
quit()
#left_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******************************")
quit()
#right: [[i,j], [i,j+1], [i,j+2], [i,j+3]]
if j+3 in range(0, 5):
if board[i][j] == "0" and board[i][j+1] == "0" and board[i][j+2] == "0" and board[i][j+3] == "0":
print("******************************\nPlayer Wins\n******************************")
quit()
if j+3 in range(0, 5):
if board[i][j] == "X" and board[i][j+1] == "X" and board[i][j+2] == "X" and board[i][j+3] == "X":
print("******************************\nComputer Wins\n******************************")
quit()
#down: [[i,j], [i-1,j], [i-2,j], [i-3,j]]
if i-3 in range(0, 6):
if board[i][j] == "0" and board[i-1][j] == "0" and board[i-2][j] == "0" and board[i-3][j] == "0":
print("******************************\nPlayer Wins\n******************************")
quit()
if i-3 in range(0, 6):
if board[i][j] == "X" and board[i-1][j] == "X" and board[i-2][j] == "X" and board[i-3][j] == "X":
print("******************************\nComputer Wins\n******************************")
quit()
while True:
printBoard()
#pick a column
while True:
try:
yourPick = int(raw_input('Pick a column 0 - 6: '))
except ValueError:
print 'That\'s not a number!'
else:
if 0 <= yourPick < 6 and yourPick not in fullCols:
break
else:
print 'Out of range or column full. Try again.'
print(yourPick)
#column logic
boardLogic(yourPick, "user")
#opponent randomly picks a column
while True:
try:
theirPick = random.randint(0, 6)
except ValueError:
print 'That\'s not a number!'
else:
if theirPick not in fullCols:
break
else:
print 'Column full!'
print(theirPick)
#column logic
boardLogic(theirPick, "notUser")
#check win conditions
for i in range(0, 6):
for j in range(0, 5):
does_square_contain_win(i, j)