I have created a simple Tic-Tac-Toe program. Can someone please review it and let me know where I can improve.
Here we can play against computer. Program start by putting 'X' at the center that is at the position 5. Then ask user to enter number from available position and then assign 'O' on selected position. There are four possible verdicts: the game should continue, or the game ends with a tie, your win, or the computer's win;
import random
def board():
"Creating Board dictionary"
dic_board = {}
v = 1
for i in range(3):
for j in range(3):
k = str(i) + str(j)
dic_board[k] = v
v += 1
update_board(dic_board)
def draw_board(board):
"""Draw board"""
for i in range(3):
print('+' + '-' * 5 + '+' + '-' * 5 + '+' + '-' * 5 + '+', end='')
print()
print('|' + ' ' * 5 + '|' + ' ' * 5 + '|' + ' ' * 5 + '|')
for j in range(3):
k = str(i) + str(j)
print('| ' + str(board[k]) + ' ' * 3, end='')
print('|')
print('|' + ' ' * 5 + '|' + ' ' * 5 + '|' + ' ' * 5 + '|')
print('+' + '-' * 5 + '+' + '-' * 5 + '+' + '-' * 5 + '+', end='')
print()
def update_board(board):
"""the first move belongs to the computer -
it always puts its first 'X' in the middle of the board that is at 5;"""
total_moves = 0
flag = True #Flag = True for computer's move and False for user's move
while total_moves < 9 :
available_num = [v for k, v in board.items() if v not in ('X','O')]
if flag :
if total_moves == 0:
update_moves(board, 'Comp', 5)
flag = False
sign = 'X'
draw_board(board)
else:
num = random.choice(available_num)
update_moves(board, 'Comp', num)
flag = False
sign = 'X'
draw_board(board)
else:
try:
s = str(available_num)
val = int(input('Select Number '+ s + ': '))
update_moves(board, 'User', val)
flag = True
sign = 'O'
except:
print('Invalid Input!!')
continue
total_moves += 1
v = victory(board,sign)
if v:
print(v)
if sign == 'O' :
draw_board(board)
break
else :
print('Tie !!')
print('Game Over!!')
def update_moves(board, moves,val):
"Updating borad as per input"
key_to_update = [k for k, v in board.items() if v == val]
if moves == 'Comp':
board[key_to_update[0]] = 'X'
elif moves == 'User':
board[key_to_update[0]] = 'O'
return board
def victory(board,sign):
message = ''
message = row_status(board,sign,message)
message = column_status(board,sign,message)
message = digonals_status(board,sign,message)
return message
def row_status(board,sign,message):
"""checking for Horizontal row"""
if board['00'] == board['01'] == board['02'] == sign or \
board['10'] == board['11'] == board['12'] == sign or \
board['20'] == board['21'] == board['22'] == sign:
if sign == 'X':
message = 'computer win !!'
elif sign == 'O':
message = 'Your win !!'
return message
def column_status(board,sign,message):
"""checking for verticle column """
if board['00'] == board['10'] == board['20'] == sign or \
board['01'] == board['11'] == board['21'] == sign or \
board['02'] == board['12'] == board['22'] == sign:
if sign == 'X':
message = 'computer win !!'
elif sign == 'O':
message = 'Your win !!'
return message
def digonals_status(board,sign,message):
"""checking for digonals """
if board['00'] == board['11'] == board['22'] == sign or \
board['02'] == board['11'] == board['20'] == sign :
if sign == 'X':
message = 'computer win !!'
elif sign == 'O':
message = 'Your win !!'
return message
print(board())