Skip to content

Instantly share code, notes, and snippets.

@masious
Last active September 30, 2016 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masious/d907acc5687986c70135b95cc70ec11c to your computer and use it in GitHub Desktop.
Save masious/d907acc5687986c70135b95cc70ec11c to your computer and use it in GitHub Desktop.
Simple puzzle (Installation of click package needed - with pip)
import click
from random import shuffle
blocks = ['1', '2', '3', '4', '5', '6', '7', '8', '-']
def init_puzzle():
shuffle(blocks)
flat_puzzle = blocks
puzzle = [[0 for j in range(3)] for i in range(3)]
for i in range(3):
for j in range(3):
puzzle[i][j] = flat_puzzle[i * 3 + j]
return puzzle
def draw_puzzle(puzzle):
for i in range(3):
print ' '.join(puzzle[i])
print '-----'
def find_dash(puzzle):
for i in range(3):
for j in range(3):
if puzzle[i][j] == '-':
return i, j
def up(puzzle):
i, j = find_dash(puzzle)
if i == 0:
return
prev = puzzle[i-1][j]
puzzle[i-1][j] = '-'
puzzle[i][j] = prev
def down(puzzle):
i, j = find_dash(puzzle)
if i == 2:
return
prev = puzzle[i+1][j]
puzzle[i+1][j] = '-'
puzzle[i][j] = prev
def right(puzzle):
i, j = find_dash(puzzle)
if j == 2:
return
prev = puzzle[i][j+1]
puzzle[i][j+1] = '-'
puzzle[i][j] = prev
def left(puzzle):
i, j = find_dash(puzzle)
if j == 0:
return
prev = puzzle[i][j-1]
puzzle[i][j-1] = '-'
puzzle[i][j] = prev
def main():
puzzle = init_puzzle()
while True:
draw_puzzle(puzzle)
key = ord(click.getchar()[2])
if key == 65:
up(puzzle)
elif key == 66:
down(puzzle)
elif key == 68:
left(puzzle)
elif key == 67:
right(puzzle)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment