Skip to content

Instantly share code, notes, and snippets.

@jfo
Created February 23, 2014 19:03
Show Gist options
  • Save jfo/9175670 to your computer and use it in GitHub Desktop.
Save jfo/9175670 to your computer and use it in GitHub Desktop.
def fetch_new_puzzle(difficulty):
"""Get a new puzzle from the online source.
Difficulties are from 1 to 5 maybe? Integers.
Boards are indexed x and y, where x is the row
number and y is the column number.
"""
page_text = urlopen("http://www.free-sudoku.com/sudoku.php?mode=%s" %
difficulty ).read()
tree = html.fromstring(page_text)
board = new_board()
for x, y in product(range(9), range(9)):
element_id = (x * 9) + y + 1
text_content = tree.get_element_by_id(element_id).text_content()
value = int(text_content) if text_content else None
board[(x,y)] = value
return board
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment