Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 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. def bones_shuffle():
  17. # генерация двух последовательностей: эталонной и перемешанной
  18.     original_seq = list(range(1, 16))
  19.     original_seq.append("*")
  20.     vacant_position = 15
  21.  
  22.     mixed_seq = original_seq[:]
  23.     roulette = 1, -1, 4, -4
  24.  
  25.     i = 0
  26.     while i != 100:
  27.         step = random.choice(roulette)
  28.         destination = vacant_position + step
  29.  
  30.         if not (destination > 15 or destination < 0) and (abs(destination % 4 - vacant_position % 4) != 3):
  31.             native = mixed_seq[destination]
  32.             mixed_seq[destination] = "*"
  33.             mixed_seq[vacant_position] = native
  34.             vacant_position = destination
  35.         i += 1
  36.  
  37.     return original_seq, mixed_seq, vacant_position
  38.  
  39. def bones_put():
  40. # вывод поля костей-кнопок
  41.     for i in mixed_seq:
  42.         if i != "*":
  43.             bone_row = mixed_seq.index(i)//4
  44.             bone_col = mixed_seq.index(i)%4
  45.             Button(main_frame, width=4, height=3, text=str(i)).grid(row=bone_row, column=bone_col)
  46.     return
  47.  
  48. def bones_move(event):
  49.     global original_seq
  50.     global mixed_seq
  51.     global vacant_position
  52.     vacant_row = vacant_position // 4
  53.     vacant_col = vacant_position % 4
  54.     current_pos = event.widget.grid_info()
  55.     bone_row, bone_col = current_pos['row'], current_pos['column']
  56.     bone_position = 4 * bone_row + bone_col
  57.     number = mixed_seq[bone_position]
  58.     delta_raw = abs(vacant_row - bone_row)
  59.     delta_col = abs(vacant_col - bone_col)
  60.     if (delta_col + delta_raw) == 1:
  61.         event.widget.grid_forget()
  62.         event.widget.grid(row=vacant_row, column=vacant_col)
  63.         vacant_position, bone_position = bone_position, vacant_position
  64.         mixed_seq[bone_position] = number
  65.         mixed_seq[vacant_position] =  "*"
  66.  
  67.     if vacant_position == 15:
  68.         if original_seq ==  mixed_seq:
  69.             if  not messagebox.askyesno("Победа!", "Good for you!\nСыграешь ещё раз?"):
  70.                 quit()
  71.             else:
  72.                 for btn in main_frame.grid_slaves():
  73.                     btn.grid_remove()
  74.                 original_seq, mixed_seq, vacant_position = bones_shuffle()
  75.                 bones_put()
  76.  
  77. original_seq, mixed_seq, vacant_position = bones_shuffle()
  78. bones_put()
  79. main_frame.bind_class('Button', '<Button-1>', bones_move)
  80.  
  81. tk.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement