0

I coded a simple tic-tac-toe "from scratch" (haven't included a GUI or anything of that sophistication). I would like to know about any improvements that can be made to this game. Thank you.

```
import random
board_lst = []
class board:
def __init__(self, rows, columns):
self.rows = rows
self.columns = columns
def draw(self):
global board_lst
for number in range(self.rows):
board_lst.append([])
for number in range(self.columns):
for i in range(len(board_lst)):
board_lst[i].append('')
def add(self, move, x, y):
global board_lst
board_lst[x][y] = move
for i in range(len(board_lst)):
print board_lst[i],"\n"
tic_tac_toe = board(3,3)
tic_tac_toe.draw() #Draws the tic-tac-toe board
def is_empty(x, y): #Checks if a cell on the board is empty
return board_lst[x][y] == ''
def is_valid_move(move, x, y):#Checks if a move is valid
return (move == 'x' or move == 'o') and (x in range(3) and y in range(3))\
and is_empty(x,y)
def player_move(move, x, y): #Places an 'x' or an 'o' on the board
if is_valid_move(move, x, y):
tic_tac_toe.add(move, x, y)
else:
print "Move is not possible."
def vertical(): #Checks each column for a human move and makes a move in that column
if board_lst[0][0] == 'x' or board_lst[1][0] == 'x' or board_lst[2][0]:
if is_valid_move('o',0,0):
tic_tac_toe.add('o',0,0)
elif is_valid_move('o',1,0):
tic_tac_toe.add('o',1,0)
elif is_valid_move('o',2,0):
tic_tac_toe.add('o',2,0)
else:
vertical()
elif board_lst[0][1] == 'x' or board_lst[1][1] == 'x' or board_lst[2][1]:
if is_valid_move('o',0,1):
tic_tac_toe.add('o',0,1)
elif is_valid_move('o',1,1):
tic_tac_toe.add('o',1,1)
elif is_valid_move('o',2,1):
tic_tac_toe.add('o',2,1)
else:
vertical()
elif board_lst[0][2] == 'x' or board_lst[1][2] == 'x' or board_lst[2][2]:
if is_valid_move('o',0,2):
tic_tac_toe.add('o',0,2)
elif is_valid_move('o',1,2):
tic_tac_toe.add('o',1,2)
elif is_valid_move('o',2,2):
tic_tac_toe.add('o',2,2)
else:
vertical()
else:
player_move('o', random.randint(0,2), random.randint(0,2))
def computer_move(): #Allows the computer to make a move based on the human moves
if board_lst[0].count('') == 1:
board_lst[0][board_lst[0].index('')] = 'o'
for i in range(len(board_lst)):
print board_lst[i],"\n"
elif board_lst[1].count('') == 1:
board_lst[1][board_lst[1].index('')] = 'o'
for i in range(len(board_lst)):
print board_lst[i],"\n"
elif board_lst[2].count('') == 1:
board_lst[2][board_lst[2].index('')] = 'o'
for i in range(len(board_lst)):
print board_lst[i],"\n"
else:
vertical()
def human_move(): #Allows the user to make a move of his/her own choice
x = raw_input("Enter the row number: ")
x = int(x)
y = raw_input("Enter the column number: ")
y = int(y)
player_move('x', x-1, y-1)
def every(lst, el): #Checks if all the elements in a list are the second parameter
counter = 0
for i in range(len(lst)):
if lst[i] == el:
counter = counter
else:
counter += 1
return counter == 0
def turn():
#Allows one to choose which player gets the first turn
#and directs the game accordingly
def play():
print "Human's turn to move."
human_move()
if all_in_row():
print "Human wins."
print "\n"
game()
elif is_draw():
print "The game is a draw."
print "\n"
game()
else:
print "\nComputer's turn to move."
computer_move()
if all_in_row1():
print "Computer wins."
print "\n"
game()
elif is_draw():
print "The game is a draw."
print "\n"
game()
else:
print "\n"
play()
def play1():
print "Computer's turn to move."
computer_move()
if all_in_row1():
print "Computer wins."
print "\n"
game()
elif is_draw():
print "The game is a draw."
print "\n"
game()
else:
print "\n"
print "Human's turn to move."
human_move()
if all_in_row():
print "Human wins."
print "\n"
game()
elif is_draw():
print "The game is a draw."
print "\n"
game()
else:
print "\n"
play1()
turn_op = raw_input("Who do you want to go first (1 for computer or 2 for human): ")
if turn_op == '1':
play1()
elif turn_op == '2':
play()
else:
print "Invalid selection."
print "\n\n"
turn()
def is_draw(): #Determines if there is a draw
counter = 0
for i in range(len(board_lst)):
if not '' in board_lst[i] and not (board_lst[i] == ['x','x','x'] or board_lst[i] == ['o','o','o']):
counter = counter
else:
counter += 1
if counter == 0:
return True
else:
return False
def all_in_row(): #Checks if there is a tic-tac-toe all-in-a-row by human
for i in range(len(board_lst)):
if board_lst[i] == ['x','x','x']:
return True
else:
lst = [x[0] for x in board_lst]
lst1 = [x[1] for x in board_lst]
lst2 = [x[2] for x in board_lst]
if every(lst, 'x'):
return True
elif every(lst1, 'x'):
return True
elif every(lst2, 'x'):
return True
else:
chk = [board_lst[0][0], board_lst[1][1], board_lst[2][2]]
chk1 = [board_lst[2][0], board_lst[1][1], board_lst[0][2]]
if every(chk, 'x'):
return True
elif every(chk1, 'x'):
return True
else:
return False
def all_in_row1(): #Checks for all-in-a-row by computer
for i in range(len(board_lst)):
if every(board_lst[i], 'o'):
return True
else:
lst = [x[0] for x in board_lst]
lst1 = [x[1] for x in board_lst]
lst2 = [x[2] for x in board_lst]
if every(lst, 'o'):
return True
elif every(lst1, 'o'):
return True
elif every(lst2, 'o'):
return True
else:
chk = [board_lst[0][0], board_lst[1][1], board_lst[2][2]]
chk1 = [board_lst[2][0], board_lst[1][1], board_lst[0][2]]
if every(chk, 'o'):
return True
elif every(chk1, 'o'):
return True
else:
return False
def game(): #The actual game
print "==== TIC-TAC-TOE oxox ===="
print "\n"
for i in range(len(board_lst)):
print board_lst[i],"\n"
print "\n"
print "To start the game enter 1; to exit, enter 0."
choice = raw_input("Enter choice here: ")
if choice == '1':
turn()
elif choice == '0':
return
else:
print "Invalid entry. Try again."
print "\n"
game()
game()
```