Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.97 KB | None | 0 0
  1. #!/bin/env python3
  2.  
  3. from tkinter import *
  4. from tkinter import messagebox
  5. import random
  6.  
  7. tk = Tk()
  8.  
  9. tk.title("Пятнашки")
  10. tk.resizable(False, False)
  11. tk.bind("<Escape>", quit)
  12.  
  13. main_frame = Frame(tk, bg="#3f3f3f", cursor="hand1", width=48*8, height=48*8, bd=10)
  14. main_frame.pack()
  15.  
  16. class Bone():
  17.         def __init__(self, main_frame, bone_row, bone_col, bone_number):
  18.                 self.btn_id = Button(main_frame, width=4, height=3, text=str(bone_number))
  19.                 self.btn_id.grid(row=bone_row, column=bone_col)
  20.  
  21. def game(event):
  22.         global vacant_position
  23.         global original_seq
  24.         global mixed_seq
  25.         vacant_row = vacant_position // 4
  26.         vacant_col = vacant_position % 4
  27.         current_pos = event.widget.grid_info()
  28.         bone_row = current_pos['row']
  29.         bone_col = current_pos['column']
  30.         bone_position = 4 * bone_row + bone_col
  31.         number = mixed_seq[bone_position]
  32.         delta_raw = abs(vacant_row - bone_row)
  33.         delta_col = abs(vacant_col - bone_col)
  34.         if (delta_col + delta_raw) == 1:
  35.                 event.widget.grid_forget()
  36.                 event.widget.grid(row=vacant_row, column=vacant_col)
  37.                 vacant_position, bone_position = bone_position,vacant_position
  38.                 mixed_seq[bone_position] = number
  39.                 mixed_seq[vacant_position] =  "*"
  40.         if vacant_position == 15:
  41.                 if original_seq ==  mixed_seq:
  42.                         if  not messagebox.askyesno("Победа!", "Good for you!\nСыграешь ещё раз?"):
  43.                                 quit()
  44.                         else:
  45.                                 for btn in main_frame.grid_slaves():
  46.                                         btn.grid_remove()
  47.                         original_seq, mixed_seq, vacant_position = init()
  48.  
  49. def init():
  50.         original_seq = list(range(1, 16))
  51.         original_seq.append("*")
  52.         vacant_position = 15
  53.  
  54.         mixed_seq = original_seq[:]
  55.         roulette = 1, -1, 4, -4
  56.  
  57.         i = 0
  58.         while i != 100:
  59.                 step = random.choice(roulette)
  60.                 destination = vacant_position + step
  61.  
  62.                 if not (destination > 15 or destination < 0):
  63.                         native = mixed_seq[destination]
  64.                         mixed_seq[destination] = "*"
  65.                         mixed_seq[vacant_position] = native
  66.                         vacant_position = destination
  67.                 i += 1
  68.  
  69.         for i in mixed_seq:
  70.                 if i != "*":
  71.                         bone_row = mixed_seq.index(i)//4
  72.                         bone_col = mixed_seq.index(i)%4
  73.                         Bone(main_frame, bone_row, bone_col, i)
  74.                 else:
  75.                         vacant_position = int(mixed_seq.index(i))
  76.         return original_seq, mixed_seq, vacant_position
  77.  
  78. original_seq, mixed_seq, vacant_position = init()
  79. main_frame.bind_class('Button', '<Button-1>', game)
  80. tk.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement