Tic-Tac-Toe from scratch. Code Ugliness check before I implement the win-condition and maybe get set in to bad habits. Questions:
As implemented:
Better to pass board between functions or set as a global variable?
OK to use same variable name within different functions?
OOP:
Would you recommend using Object Orientated Programming for this?
import sys
import copy
import random
#this function accepts a user input if it's contained in a list
def get_user_input(input_array,prompt):
valid_input = False
#the purpose of this is to convert a range() to list
acceptable_input_array = list(copy.copy(input_array))
for i,element in enumerate(acceptable_input_array):
#convert elements in array to string
acceptable_input_array[i] = str(element).upper()
while not valid_input:
print("Please enter choice of",
acceptable_input_array,
"for",
prompt,
", blank to cancel.")
user_input = input()
user_input = user_input.upper()
#if blank input
if user_input == '':
#exit program
print('Exiting program.')
sys.exit()
#if input is in acceptable input
if(user_input in acceptable_input_array):
valid_input = True
else:
print('Incorrect Input')
return user_input
#this function simply prints the board state
def print_board(board):
#0 is blank, 1 is X, 2 is O
token = ''
for i, element in enumerate(board):
if element == 1:
token = 'X'
elif element == 2:
token = 'O'
else:
token = ' '
position = i%BOARD_SIZE
#position = 0 is leftmost
if position == 0:
#print numberals at left of table
print(i//BOARD_SIZE,end='')
#.center to make an equidistant board
print(token.center(BOARD_SIZE), end='')
print('|',end='')
#position BOARD_SIZE - 1 is rightmost
if position == BOARD_SIZE - 1:
#print newline
print()
#each token has BOARD_SIZE + BOARD_SIZE^2 spacing
print('-' * (BOARD_SIZE + BOARD_SIZE**2))
print(' ',end='')
for j in range(BOARD_SIZE):
#print numerals at bottom of table
print(str(j).center(BOARD_SIZE+1),end='')
print()
#this function accepts co-ordinates from player then updates board
def player_turn(board):
valid_coords = False
while not valid_coords:
i = int(get_user_input(range(BOARD_SIZE),'i co-ordinate'))
j = int(get_user_input(range(BOARD_SIZE),'j co-ordinate'))
print('You selected position [', i, j, ']')
index = j*BOARD_SIZE + i
#if not already X or O in position
if(board[index] == 0):
valid_coords = True
else:
print('Spot already taken, please enter valid co-ordiantes.')
board[index] = 1
print_board(board)
#this function chooses a random free space for a.i. turn
def cpu_turn(board):
print('cpu turn')
list_of_indices = []
for i, element in enumerate(board):
if(element == 0):
list_of_indices.append(i)
print(list_of_indices)
random_index = random.choice(list_of_indices)
print('random index: ', random_index)
board[random_index] = 2
print_board(board)
#variable board size, based on user input.
BOARD_SIZE = int(get_user_input([3,5,7],'board size'))
#create empty list board with length of BOARD_SIZE squared
#board represented by a 1D array
board = [0] * BOARD_SIZE**2
print_board(board)
print("You are playing as 'X'")
while True:
player_turn(board)
cpu_turn(board)