Last active
August 29, 2015 14:08
-
-
Save quapka/e0370845ef724f4d8504 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
def game(): | |
pass | |
def load_plan(filename): | |
""" loads a file called filename, from working | |
directory; expects data such as: | |
..#. | |
.... | |
..#. | |
.... | |
and turns them into list like this: | |
[[0, 0, -1, 0], | |
[0, 0, 0, 0], | |
[0, 0, -1, 0], | |
[0, 0, 0, 0]] | |
""" | |
# secured opening, 'with' handles closing | |
# the file on its own | |
with open(filename, 'r') as f: | |
board_str = f.readlines() | |
board_lst = [] | |
for line in board_str: | |
new_line = [] | |
for char in line: | |
if char == '.': | |
new_line.append(0) | |
elif char == '#': | |
new_line.append(-1) | |
board_lst.append(new_line) | |
return board_lst | |
def print_board(board): | |
""" prints the board """ | |
for line in board: | |
# use .rjust(2) for nice column formatting | |
print ' '.join([str(x).rjust(2) for x in line]) | |
return | |
def dump_solution(board, filename): | |
""" dumps solution for queen problem; | |
expects e.g. | |
[[2, 1, -1, 2], | |
[1, 1, 2, 1], | |
[1, 2, -1, 1], | |
[1, 1, 1, 1]] | |
and creates a file called filename, | |
which contains: | |
2 1 # 2 | |
1 1 2 1 | |
1 2 # 1 | |
1 1 1 1 | |
""" | |
solution = '' | |
for line in board: | |
for c in line: | |
if c > 0: | |
solution += str(c) +' ' | |
else: | |
solution += '# ' | |
solution += '\n' | |
with open(filename, 'w') as f: | |
f.write(solution.strip()) | |
return | |
def solve_queen(board): | |
pass | |
if __name__ == '__main__': | |
print_board(load_plan('queen1')) | |
dump_solution([[2, 1, -1, 2], | |
[1, 1, 2, 1], | |
[1, 2, -1, 1], | |
[1, 1, 1, 1]], 'queen1sol') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 1 # 2 | |
1 1 2 1 | |
1 2 # 1 | |
1 1 1 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment