Hi guys,
Really confused how can I pass out information from functions then return it out so other functions can use it?
def error_move(board):
'''User Move'''
.......
Specifically here...
x_cod = board[int(row)]
assigning y_cod = [int(field)]
assigning return(x_coord, y_coord)
#pass outdef player_move(x_coord, y_coord):
#pass in ERROR - NameError: global name 'x_coord' is not defined
(x, y) = x_coord, y_coord
print x, y
How do I make it so it works im so confused and very fustrated
PLEASE HELP ANYONE
Code
# To start type: play(None)
PROMPT_SIZE = "Board size: "
ERROR_SIZE = "Size must be a number between 4 and 9"
PROMPT_MOVE = "Player X, enter move: " # Or player O ...
ERROR_MOVE_LENGTH = "Move must be 2 characters"
ERROR_MOVE_POSITION = "Invalid position" # you can't place your player on occupied position
ERROR_MOVE_FORMAT = "Invalid input format" # if you play, e.g., A1 instead of 1A
ERROR_MOVE_ILLEGAL = "Illegal move" #if move does not result in linearly enclosed opponents
WIN = "Win for player X!" # Or player O
DRAW ="Equal number of frendz. Game is drawn!"
def get_board_size():
'''Get board size'''
PPT_SIZE = raw_input(PROMPT_SIZE) #Asking for board size
while PPT_SIZE.isdigit() == False or int(PPT_SIZE) > 9 or int(PPT_SIZE) < 4:
print ERROR_SIZE # size error not between 4 & 9
PPT_SIZE = raw_input(PROMPT_SIZE) #repeating until correct input
return PPT_SIZE
def make_board(size):
''' Creating the board'''
board = []
board_odd_or_even = size % 2
for i in range(0, size):
list = []
for i in range(0,size):
list.append('.') # dots for empty cells
board.append(list)
first_point = (size / 2) - 1
board[first_point][first_point] = 'X' # Player X starting pieces
board[first_point][first_point + 1] = 'O' # player O starting pieces
board[first_point + 1][first_point] = 'O' # player O starting pieces
board[first_point+ 1][first_point + 1] = 'X' # Player X starting pieces
return board
def display_board(board):
'''Displaying the board'''
print # needs a space after board prompt
i = len(board)
num = 0
alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'] # reference
for line in board:
num = num + 1
print num, ' '.join(line) # printing board
letter_count = 0
print ' ',
for letter in alpha:
letter_count += 1
if letter_count <= i:
print letter, #printing alpha reference
print # fixes bug
print # needs a line after board
def error_move(board):
'''User Move'''
alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
user_move=raw_input(PROMPT_MOVE)
is_done1 = False
is_done2 = False
while len(user_move) != 2:
print ERROR_MOVE_LENGTH # Input must be 2 chars
user_move=raw_input(PROMPT_MOVE)
while user_move[0].isalpha() == False or user_move[1].isdigit() == False:
print ERROR_MOVE_FORMAT # format must be letter then number eg. A1
user_move=raw_input(PROMPT_MOVE)
while int(user_move[1]) > len(board) or int(user_move[1]) == 0:
print ERROR_MOVE_ILLEGAL # Number Move number must be on board
user_move=raw_input(PROMPT_MOVE)
while is_done1 == False:
if alpha.__contains__(user_move[0].upper()): #If letter is a letter on board
letter_number = alpha.index(user_move[0].upper()) + 1
if letter_number > len(board):
print ERROR_MOVE_POSITION # Letter is in Alpha but not right size
user_move=raw_input(PROMPT_MOVE)
is_done1 = False
else:
is_done1 = True #Letter is in Aplha and is right size
else:
print ERROR_MOVE_POSITION #letter not in Alpha and is not on board.
user_move=raw_input(PROMPT_MOVE)
is_done1 = False
while is_done2 == False:
field = alpha.index(user_move[0].upper())
row = int(user_move[1]) - 1
if board[int(row)][int(field)].__contains__('.') == False:
print ERROR_MOVE_POSITION # Peice already in place
user_move=raw_input(PROMPT_MOVE)
is_done2 = False
else:
is_done2 = True
x_cod = board[int(row)]
y_cod = [int(field)]
return(x_coord, y_coord) #pass out
def player_move(x_coord, y_coord): #pass in
(x, y) = x_coord, y_coord
print x, y
def play(initial_board_config): # main game function
'''Main Play function'''
if initial_board_config is None:
board_size = get_board_size() # 1. Prompt user for board size
board = make_board(int(board_size))
else:
board_size = len(initial_board_config)
board = initial_board_config
display_board(board) # 3. Display the board
error_move(board) #4. Check if players move is allowed
player_move(error_move(board))
# if __name__ == '__main__':
# play(None)