I've just started a self-learning "course" on Python practical programming for beginners. I found this book online, and decided to go through the chapters.
The book had a half-complete implementation of a Tic-Tac-Toe game, and I was supposed to finish it as an exercise. This is my code so far:
theBoard = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ',
'mid-L': ' ', 'mid-M': ' ', 'mid-R': ' ',
'low-L': ' ', 'low-M': ' ', 'low-R': ' '}
def printBoard(board):
""" This function prints the board after every move """
print(board['top-L'] + '|' + board['top-M'] + '|' + board['top-R'])
print('-+-+-')
print(board['mid-L'] + '|' + board['mid-M'] + '|' + board['mid-R'])
print('-+-+-')
print(board['low-L'] + '|' + board['low-M'] + '|' + board['low-R'])
def checkWin(board):
"""
This functions checks if the win condition has been
reached by a player
"""
flag = False
possibleWins = [['top-L', 'top-M', 'top-R'],
['mid-L', 'mid-M', 'mid-R'],
['low-L', 'low-M', 'low-R'],
['top-L', 'mid-L', 'low-L'],
['top-M', 'mid-M', 'low-M'],
['top-R', 'mid-R', 'low-R'],
['top-L', 'mid-M', 'low-R'],
['top-R', 'mid-M', 'low-L']]
for row in range(len(possibleWins)):
temp = board[possibleWins[row][0]]
if temp != ' ':
for position in possibleWins[row]:
if board[position] != temp:
flag = False
break
else:
flag = True
if flag:
return True
return False
turn = 'X'
for i in range(9):
printBoard(theBoard)
print('Turn for ' + turn + '. Move on which space?')
while True:
move = input()
if move in theBoard:
if theBoard[move] != ' ':
print('Invalid move. Try again.')
else:
break
else:
print('Invalid move. Try again.')
theBoard[move] = turn
if checkWin(theBoard):
printBoard(theBoard)
print('Player ' + turn + ' wins!')
break
if turn == 'X':
turn = 'O'
else:
turn = 'X'
My checkWin
function is very "stupid", it detects a win based on predetermined scenarios, and may not be very efficient in that regard as well. What if the board was of an arbitrary size nxn? Is there an algorithm to determine the victory condition without having to rewrite the entire game?
return False
right in the middle that I'm not 100% sure how to resolve. Please review how your code is formatted, because as it is, it's not valid Python \$\endgroup\$