I am creating a connect 4 game in python and I have managed to write the code so that it runs. However I have a lot of duplicated data in it and was just wondering if anyone could help me to refine it to minimise this?
Here is my code so far:
def CreateBoard(r, c, b, n):
for i in range(1,(r+1)):
n.append(str(i))
b.append(n)
for i in range(c):
b.append(['*']*(r))
return(b)
"""The Following PrintBoard function takes the parameter board(b), prints
the board and then returns the board."""
def printBoard(b):
for lst in b:
print(" ".join(lst))
return b
"""The following Check function takes the parameter board. Within this """
def check(board, n):
n = []
for i in range(1,len(board[1])+1):
if board[1][i-1] == '*':
n.append(i)
print(n)
user = (input('Enter column: '))
if(user.isdigit() == True):
user = int(user)
if (user in n):
return(user)
print('Invalid input')
return check(board, n)
def WinningCon(b, r, u, c):
loop1 = True
rowCon = ""
colCon = ""
for i in range(0,len(b[1])):
rowCon += b[r][i]
if('X'*4 in rowCon) or('O'*4 in rowCon):
return(True)
for i in range(1,c+1):
colCon += b[i][u-1]
if('X'*4 in colCon) or('O'*4 in colCon):
return(True)
def Diag2(u, r, b, column, row):
utemp1 = u-1
utemp2 = u-1
rtemp1 = r
rtemp2 = r
end = ""
beg = ""
while(True):
beg += b[rtemp1][utemp1]
rtemp1 -= 1
utemp1 += 1
if(rtemp1 == 1):
break
elif(utemp1 >= column - 1):
break
while(True):
end += b[rtemp2][utemp2]
rtemp2 +=1
utemp2 -=1
if(rtemp2 >= row):
break
elif(utemp2 == 0):
break
beg = beg[::-1]
fullstring = beg+end
if('X'*4 in fullstring) or('O'*4 in fullstring):
return(True)
def Diag1(u, r, b, column, row):
utemp1 = u-1
utemp2 = u-1
rtemp1 = r
rtemp2 = r
end = ""
beg = ""
while(True):
beg += b[rtemp1][utemp1]
rtemp1 -= 1
utemp1 -= 1
if(rtemp1 == 1):
break
elif(utemp1 == 0):
break
while(True):
end += b[rtemp2][utemp2]
rtemp2 +=1
utemp2 +=1
if(rtemp2 >= row):
break
elif(utemp2 >= column-1):
break
beg = beg[::-1]
fullstring = beg+end
if('X'*4 in fullstring) or('O'*4 in fullstring):
return(True)
def ProcessInput(u, s, b, r):
rowNum = r
u = u-1
while(not b[rowNum][u] == '*'):
rowNum -= 1
b[rowNum][u] = s
return(rowNum)
def EndGame(g, b, p):
printBoard(b)
print("Congrats %s, you've won!" %p)
replayGame = input('Would you like to play again? Yes or No?\n').lower()
if replayGame == 'yes':
MainGame()
else:
g = False
return(g)
def MainGame():
column = int(input('Enter number of columns'))
row = int(input('Enter number of rows'))
board = []
nums = []
board = CreateBoard(column, row, board, nums)
player1 = 'K'#input('Enter name: ')
player2 = 'A'#input('Enter name: ')
turn = 1
GameOn = True
while(GameOn):
board = printBoard(board)
user = check(board, nums)
if(turn%2 != 0):
print("It's %s's turn" %player1)
rc = ProcessInput(user, "X", board, row)
else:
print("It's %s's turn" %player2)
rc = ProcessInput(user, "O", board, row)
if(WinningCon(board, rc , user, row) == True):
GameOn = EndGame(GameOn, board, player1)
elif(Diag1(user, rc, board, column, row) == True):
GameOn = EndGame(GameOn, board, player1)
elif(Diag2(user, rc, board, column, row) == True):
GameOn = EndGame(GameOn, board, player1)
turn +=1
MainGame()