This is my first program in Python that I have coded and I am relatively happy with how it turned out. I would like to know how I could make my code shorter and more efficient.
def one_round(player):
#Distinguish player 1 from player 2
if player == "p1":
p = "Player 1"
elif player == "p2":
p = "Player 2"
pin = raw_input("%s move (a/b/c for row | 1/2/3 for column):" % (p)) #receive input for player from console
#separating row from column
row = pin[0].lower()
col = int(pin[1]) - 1
if row == "a": #changing the row letter into a digit for the index of board
row = 0
elif row =="b":
row = 1
elif row == "c":
row = 2
while (row > 2 or col > 2) or (board[row][col] == 'X' or board[row][col] == 'O'):
if row > 2 or col > 2:
print "Oops, these co-ordinates are off of the board, please try again"
elif board[row][col] == 'X' or board[row][col] == 'O':
print "Oops this spot has been taken, please pick another"
pin = raw_input("%s move (a/b/c for row | 1/2/3 for column):" % (p)) #receive input for player from console
#separating row from column
row = pin[0].lower()
col = int(pin[1]) - 1
if row == "a": #changing the row letter into a digit for the index of board
row = 0
elif row =="b":
row = 1
elif row == "c":
row = 2
if player == "p1": #Determining whether to use X or O
x = 'X'
elif player == "p2":
x = 'O'
del board[row][col] #Swapping out the dashes for a symbol
board[row].insert(col, x)
print str(" ".join(board[0])) + '\n' + str(" ".join(board[1])) + '\n' + str(" ".join(board[2])) #Printing board
if board[0] == [x, x, x] or board[1] == [x, x, x] or board[2] == [x, x, x] or (board[0][0] == x and board[1][0] == x and board[2][0] == x) or (board[0][1] == x and board[1][1] == x and board[2][1] == x) or (board[0][2] == x and board[1][2] == x and board[2][2] == x) or (board[0][0] == x and board[1][1] == x and board[2][2] == x) or (board[0][2] == x and board[1][1] == x and board[2][0] == x): #Checking for winner
print '%s wins!' % (p) + '\n' + 'Game Over!'
return True
elif board[0][0] != '-' and board[0][1] != '-' and board[0][2] != '-' and board[1][0] != '-' and board[1][1] != '-' and board[1][2] != '-' and board[2][0] != '-' and board[2][1] != '-' and board[2][2] != '-': #Checking for draw
print 'Draw!' + '\n' + 'Game Over!'
return True
else:
return False
#---------------------------------------------------------------------------------------------------------------------
"""Tic tac toe game. (Noughts and crosses)"""
print "Welcome to Tic Tac Toe!" + "\n" + " Find a friend and start the game by typing coordinates for a 3x3 grid labelled with the letters a,b,c from top to bottom along the vertical axis and with 1,2,3 from left to right along the horizontal axis." + "\n" + "Have Fun!"
#initialising board and players
board = [["-", "-", "-"], ["-", "-", "-"], ["-", "-", "-"]]
p1 = False
p2 = False
row = 0
col = 0
while (p1 == False and p2 == False): #running game
p1 = one_round("p1")
if p1 == True:
break
p2 = one_round("p2")