I'm still pretty new to programming, only been doing it a week. I'm just wondering if I made any glaring mistakes or anything that could be improved code wise?
import random
the_board = []
players = [{'type': 'player', 'piece': 'X'},
{'type': 'player', 'level': 'smart', 'piece': 'O'}]
def print_board(board):
for i in range(3):
print(board[i * 3] + '|' + board[i * 3 + 1] + '|' + board[i * 3 + 2], end='')
print(' ' + str(i * 3) + '|' + str(i * 3 + 1) + '|' + str(i * 3 + 2))
if i < 2:
print('-+-+-' + ' ' + '-+-+-')
print()
def is_win(bo, player):
# 0 1 2
# 3 4 5
# 6 7 8
for i in range(3):
return (bo[i * 3] == player and bo[i * 3 + 1] == player and bo[i * 3 + 2] == player) or \
(bo[i] == player and bo[i+3] == player and bo[i+6] == player) or \
(i <= 1 and (bo[i*2] == player and bo[4] == player and bo[8-(i*2)] == player))
def check_board(move, player):
global the_board
board_copy = the_board.copy()
board_copy[move] = players[player]['piece']
print('win')
return is_win(board_copy, players[player]['piece'])
def ai():
global players, the_board
possible_moves = []
final_move = None
for i in range(9):
if the_board[i] == ' ':
possible_moves.append(i)
if the_board[4] == ' ' and players[1]['level'] == 'smart':
the_board[4] = players[1]['piece']
return
for i in range(len(possible_moves)):
board_copy = the_board.copy()
board_copy[possible_moves[i]] = players[1]['piece']
if check_board(possible_moves[i], 1):
the_board[possible_moves[i]] = players[1]['piece']
print('win')
return
elif check_board(possible_moves[i], 0):
print('lose')
final_move = possible_moves[i]
elif players[1]['level'] == 'smart' and final_move is None:
if possible_moves[i] in [0, 2, 6, 8]:
print('random')
final_move = possible_moves[i]
else:
if final_move is not None:
print('other')
the_board[final_move] = players[1]['piece']
return
print('final')
the_board[possible_moves[random.randint(0, len(possible_moves))]] = players[1]['piece']
def input_validation(option_type, options, choice):
global the_board
if option_type == 'number_range':
if not str.isdigit(choice):
print('It has to be a number')
return False
else:
choice = int(choice)
if choice < options[0] or choice > options[1]:
print('You have to choose a number between 0 and 8')
return False
elif (the_board[choice] == 'X' or the_board[choice] == 'O'):
print('There is already a move there, please try again.')
return False
else:
return True
if option_type == 'string':
for i in range(len(options)):
if choice == options[i]:
return True
else:
print('That is not a valid option, your choices are:')
for i in range(len(options)):
print(options[i])
else:
print()
return False
def player_options():
valid = False
global players
while not valid:
print('Do you want to play the cpu or player?')
choice = input()
choice = choice.lower()
if input_validation('string', ['cpu', 'player'], choice):
players[1]['type'] = choice
break
while not valid and players[1]['type'] == 'cpu':
print('Do you want smart or dumb ai?:')
choice = input()
choice = choice.lower()
if input_validation('string', ['smart', 'dumb'], choice):
if choice == 'dumb':
players[1]['level'] = choice
break
# while not valid:
# print('Player 1, do you want to be X or O?:')
# choice = input()
# choice = choice.upper()
# if input_validation('string', ['X', 'O'], choice):
# if choice == 'O':
# players[1]['piece'] = choice
# break
def game():
move = 0
turn = random.choice([True, False])
for i in range(9):
the_board.insert(i, ' ')
for i in range(9):
valid = False
print_board(the_board)
if is_win(the_board, players[turn]['piece']):
print_board(the_board)
print('Player ' + players[turn]['piece'] + ' is the winner!')
return
turn ^= True
if turn == 0 or (players[1]['type'] == 'player' and turn == 1):
while not valid:
move = (input('Turn for ' + players[turn]['piece'] + '. Move on which space?: '))
if input_validation('number_range', [0, 8], move):
move = int(move)
valid = True
the_board[move] = players[turn]['piece']
elif players[1]['type'] == 'cpu' and turn == 1:
ai()
print_board(the_board)
print('It is a tie!')
def retry():
valid = False
while not valid:
print('Do you want to play again with the same settings? Y or N')
choice = input()
choice = choice.upper()
if input_validation('string', ['Y', 'N'], choice):
if choice == 'Y':
game()
else:
player_options()
game()
player_options()
game()
retry()
```