[Tutor] Bug
Theoretically, the following code is suppose to check if the user has won a tic tac toe game by checking if there are all "X"s in either the horizontal, vertical, or diagonal lines of a grid (represented by a list with "board" with elements 0-8). If one of these is the case, it is suppose to print the "You won" string. Nevertheless, when I change list variable to reflect one of these conditions, there is no printing occurring. I cannot figure out why. if board[0:3]==["X", "X", "X"] or board[3:6]==["X", "X", "X"] or board[6:9]==["X", "X", "X"] or \ [board[0],board[3],board[6]]==["X", "X", "X"] or [board[1],board[4],board[7]]==["X", "X", "X"] or [board[2],board[5],board[8]] ==["X", "X", "X"] or \ [board[0],board[4],board[8]]==["X", "X", "X"] or [board[2],board[4],board[6]]==["X", "X", "X"]: ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Help finishing a function
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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Help writing a function
I need suggestions/help for writing the following 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. """ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor