[Tutor] Help finishing a function
Grace Sanford
gsanford at wesleyan.edu
Wed May 17 09:31:33 EDT 2017
I am wondering if someone can help/advise me on finishing the code for this
function:
import turtle
import time
import random
# This list represents the board. It's a list
# of nine strings, each of which is either
# "X", "O", "_", representing, respectively,
# a position occupied by an X, by an O, and
# an unoccupied position. The first three
# elements in the list represent the first row,
# and so on. Initially, all positions are
# unoccupied.
the_board = [ "_", "_", "_",
"_", "_", "_",
"_", "_", "_"]
def do_user_move(board, x, y):
"""
signature: list(str), int, int -> bool
Given a list representing the state of the board
and an x,y screen coordinate pair indicating where
the user clicked, update the board
with an O in the corresponding position.
The function returns a bool indicated if
the operation was successful: if the user
clicks on a position that is already occupied
or outside of the board area, the move is
invalid, and the function should return False,
otherise True.
"""
print("user clicked at "+str(x)+","+str(y))
width = turtle.window_width ()
height = turtle.window_height ()
#Given coordinates of user click, update board with "O" in
corresponding position
if x<(-width/2)+(width/3):
column = 0
elif x>(-width/2)+(width/3) and x<(width/2)-(width/3):
column = 1
elif x>(width/2)-(width/3):
column = 2
if y>(height/2)-(height/3):
row = 0
elif y<(height/2)-(height/3) and y>(-height/2)+(height/3):
row = 1
elif y<(-height/2)+(height/3):
row = 2
p = row * 3 + column
for board[p]=="_":
pass #code here
#Check if user clicks on a position that is already occupied
pass #code here
#Check if user clicks outside the board area
if x<(-width/2) or x>(width/2) or y<(-height/2) or y>(height/2):
return False
More information about the Tutor
mailing list