Can you provide some advice on how I can simplify this code?
I am a beginner with Python, and I'm pretty sure that my code can be much simpler. I think the check()
function is far too complex. But I have no idea how to simplify it, and still check which player has won the game.
def check():
win = 0
if gameTable[0][0] == gameTable[0][1] == gameTable[0][2] == 'X':
who(1)
return True
elif gameTable[1][0] == gameTable[1][1] == gameTable[1][2] == 'X':
who(1)
return True
elif gameTable[2][0] == gameTable[2][1] == gameTable[2][2] == 'X':
who(1)
return True
elif gameTable[0][0] == gameTable[1][0] == gameTable[2][0] == 'X':
who(1)
return True
elif gameTable[0][1] == gameTable[1][1] == gameTable[2][1] == 'X':
who(1)
return True
elif gameTable[0][2] == gameTable[1][2] == gameTable[2][2] == 'X':
who(1)
return True
elif gameTable[0][0] == gameTable[1][1] == gameTable[2][2] == 'X':
who(1)
return True
elif gameTable[0][2] == gameTable[1][1] == gameTable[2][0] == 'X':
who(1)
return True
elif gameTable[0][0] == gameTable[0][1] == gameTable[0][2] == 'O':
who(2)
return True
elif gameTable[1][0] == gameTable[1][1] == gameTable[1][2] == 'O':
who(2)
return True
elif gameTable[2][0] == gameTable[2][1] == gameTable[2][2] == 'O':
who(2)
return True
elif gameTable[0][0] == gameTable[1][0] == gameTable[2][0] == 'O':
who(2)
return True
elif gameTable[0][1] == gameTable[1][1] == gameTable[2][1] == 'O':
who(2)
return True
elif gameTable[0][2] == gameTable[1][2] == gameTable[2][2] == 'O':
who(2)
return True
elif gameTable[0][0] == gameTable[1][1] == gameTable[2][2] == 'O':
who(2)
return True
elif gameTable[0][2] == gameTable[1][1] == gameTable[2][0] == 'O':
who(2)
return True
elif '-' not in gameTable[0] and '-' not in gameTable[1] and '-' not in gameTable[2]:
who(3)
return True
else:
return False
def who(win):
if win == 1:
print('\t---=== PLAYER 1 WINS ===---')
if win == 2:
print('\t---=== PLAYER 2 WINS ===---')
if win == 3:
print('\t---=== DRAW ===---')
else:
pass
def x_move():
while True:
x = input("Player {}\n Type coordinates of your move! (x, y):\n".format(p1))
tabx = x.split(',')
try:
tabx[0] = int(tabx[0])-1
tabx[1] = int(tabx[1])-1
except:
print("--==You've type invalid coordinates==--")
continue
try:
if gameTable[tabx[0]][tabx[1]] == '-':
gameTable[tabx[0]][tabx[1]] = 'X'
print('\t', gameTable[0], '\n\t', gameTable[1], '\n\t', gameTable[2])
break
else:
print("--==This place is already filled. Choose another.==--")
except:
print("--==You've type invalid coordinates==--")
continue
def y_move():
while True:
y = input("Player {}\n Type coordinates of your move! (x, y):\n".format(p2))
taby = y.split(',')
try:
taby[0] = int(taby[0])-1
taby[1] = int(taby[1])-1
except:
print("--==You've type invalid coordinates==--")
continue
try:
if gameTable[taby[0]][taby[1]] == '-':
gameTable[taby[0]][taby[1]] = 'O'
print('\t', gameTable[0], '\n\t', gameTable[1], '\n\t', gameTable[2])
break
else:
print("--==This place is already filled. Choose another.==--")
except:
print("--==You've type invalid coordinates==--")
continue
def game():
print('\t', gameTable[0], '\n\t', gameTable[1], '\n\t', gameTable[2])
while True:
x_move()
if check():
break
y_move()
if check():
break
p1 = input('First player name: ')
p2 = input('Second player name: ')
gameTable = [['-', '-', '-'],
['-', '-', '-'],
['-', '-', '-']]
game()
Ok, I changed that a little bit. I think now it looks better, but maybe is there anything more to make it simplier?
def check():
win = 0
if gameTable[0][0] == gameTable[0][1] == gameTable[0][2] != '-':
who(1, gameTable[0][1])
return True
elif gameTable[1][0] == gameTable[1][1] == gameTable[1][2] != '-':
who(1, gameTable[1][1])
return True
elif gameTable[2][0] == gameTable[2][1] == gameTable[2][2] != '-':
who(1, gameTable[2][1])
return True
elif gameTable[0][0] == gameTable[1][0] == gameTable[2][0] != '-':
who(1, gameTable[1][0])
return True
elif gameTable[0][1] == gameTable[1][1] == gameTable[2][1] != '-':
who(1, gameTable[1][1])
return True
elif gameTable[0][2] == gameTable[1][2] == gameTable[2][2] != '-':
who(1, gameTable[1][2])
return True
elif gameTable[0][0] == gameTable[1][1] == gameTable[2][2] != '-':
who(1, gameTable[1][1])
return True
elif gameTable[0][2] == gameTable[1][1] == gameTable[2][0] != '-':
who(1, gameTable[1][1])
return True
elif '-' not in gameTable[0] and '-' not in gameTable[1] and '-' not in gameTable[2]:
who(2, '-')
return True
else:
return False
def who(win, cords):
if win == 1:
if cords == 'X':
print('---=== {} WINS A GAME ===---'.format(p1))
elif cords == 'O':
print('---=== {} WINS A GAME ===---'.format(p2))
elif win == 2:
print('\t---=== DRAW ===---')
else:
pass