[Tutor] Help finishing a function

Peter Otten __peter__ at web.de
Wed May 17 11:26:30 EDT 2017


Grace Sanford wrote:

> 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 ()

Hello Grace!

While I can understand that you want to have something visibible you are 
clearly overambitious here. Forget about the turtle for now and try to 
implement exactly what the docstring is asking for. Indices are zero-based 
in Python and for a fresh board the function should return

False when any of the x and y arguments is outside the half-open interval 0 
to 3 and True when both are inside. "Half-open" means that 0, 1, 2 are a 
legal values, but 3 is not. Examples:

>>> do_user_move(the_board, 0, 0)
True
>>> do_user_move(the_board, 2, 0)
True
>>> do_user_move(the_board, 3, 0)
False
>>> do_user_move(the_board, -1, 0)
False
>>> do_user_move(the_board, 2, 2)
True
>>> do_user_move(the_board, 2, 3)
False

Once you have that part working you can tackle the problem of converting x 
and y to an index into the `the_board` list. Examples:

x = 0, y = 0 --> index = 0
x = 1, y = 0 --> index = 1
x = 0, y = 1 --> index = 3
x = 1, y = 2 --> index = 7

Then, finally, you can think about how to render the logical board as a 
pretty image on screen.


>     #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
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list