Created
November 14, 2015 20:29
-
-
Save SpaceVoyager/8162c5bc33509f92849a to your computer and use it in GitHub Desktop.
eight_puzzle_v0.2.py
This file contains hidden or 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
import ui | |
import speech | |
def isAdjacent(row1, col1, row2, col2): | |
if abs(row1-row2) + abs(col1-col2) == 1: | |
return True | |
else: | |
return False | |
def button_pressed(sender): | |
number = sender.title | |
index = pieces.index(number) | |
(row, col) = index2loc(index) | |
empty_space_index = pieces.index('') | |
(empty_space_row, empty_space_col) = index2loc(empty_space_index) | |
if isAdjacent(row, col, empty_space_row, empty_space_col): | |
speech.say('i can move') | |
else: | |
speech.say('i am stuck') | |
pieces = ['1', '', '3', '4', '5', '7', '6', '8', '2'] | |
v = ui.View(background_color=(0.50, 0.50, 0.50)) | |
board = ui.View() | |
v.add_subview(board) | |
v.present('full_screen') | |
board.frame = ((v.width-v.height)/2, 0, v.height, v.height) | |
board.border_width = 3 | |
board.border_color = (0.00, 0.00, 0.50) | |
# convert index of button to row number and column number | |
def index2loc(index): | |
row = index / 3 | |
column = index % 3 | |
return (row, column) | |
for i in range(9): | |
button_text = pieces[i] | |
button = ui.Button(title=button_text) | |
button.background_color = (1,1,1) | |
button.font = ('Futura-CondensedExtraBold', 200) | |
button.width = v.height/3 | |
button.height = button.width | |
button.border_width = 1 | |
(r, c) = index2loc(i) | |
button.x = button.width*c | |
button.y = button.height*r | |
button.action = button_pressed | |
board.add_subview(button) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment