Thuật toán Sudoku trong Python

Sudoku là một câu đố sắp xếp số tổ hợp, dựa trên logic. Mục tiêu là lấp đầy lưới 9×9 bằng các chữ số sao cho mỗi cột, mỗi hàng và mỗi trong số chín lưới con 3×3 tạo thành lưới chứa tất cả các chữ số từ 1 đến 9.  

Chúng tôi sẽ xây dựng Trò chơi Sudoku trong python bằng thư viện pygame và tự động hóa trò chơi bằng thuật toán quay lui.  

Các tính năng đã triển khai.  

  • Giao diện trò chơi để chơi
  • tự động giải quyết
  • Trực quan hóa giải tự động i. e. , Trực quan hóa thuật toán quay lui
  • Tùy chọn. Đặt lại, Xóa trò chơi

Điều kiện tiên quyết.   

  • Thư viện Pygame phải được cài đặt sẵn
  • Kiến thức về thuật toán quay lui

Các bước thực hiện

 1. Fill the pygame window with Sudoku Board i.e., Construct a 9×9 grid. 
2. Điền vào bảng với các số mặc định.
3. Chỉ định một phím cụ thể cho từng thao tác và lắng nghe nó.
4. Tích hợp thuật toán quay lui vào nó.
5. Sử dụng bộ màu để trực quan hóa giải quyết tự động.

Hướng dẫn.   

  • Nhấn 'Enter' để tự động giải quyết và trực quan hóa
  • Để chơi trò chơi theo cách thủ công,
    Đặt con trỏ vào bất kỳ ô nào bạn muốn và nhập số.
  • Tại bất kỳ thời điểm nào, nhấn enter để giải quyết tự động

Dưới đây là Thực hiện.   

Python3




# import pygame library

import pygame

 

________X

pygame.font.init[]

 

# Total window

screen= pygame.display.set_mode[[500import0import1import2

 

import3

import4import5import6

import7__= import9pygame0import6

pygame2

 

pygame3= pygame5

pygame6= pygame5

pygame9= 500 ________ξ2 ________ξξ

________ξ4= pygame5

________x7

________xth=pygame.font.init[]0

pygame.font.init[]1pygame.font.init[]0pygame.font.init[]3import0pygame.font.init[]5import0pygame5import0pygame.font.init[]9import0pygame5import0pygame5import0# Total window5import0# Total window7import0pygame5screen0

pygame.font.init[]1pygame.font.init[]0screen3import0pygame5import0pygame5import0pygame5import0pygame.font.init[]3import0=3import0pygame5import0pygame5import0# initialise the pygame font3screen0

pygame.font.init[]1pygame.font.init[]0pygame5import0pygame5import0pygame5import0screen3import0pygame5import0# Total window5import0pygame5import0pygame.font.init[]3import0pygame.font.init[]5screen0

pygame.font.init[]1pygame.font.init[]0pygame5import0pygame5import0pygame.font.init[]3import0pygame5import0pygame.font.init[]9import0pygame5import0# Total window7import0screen3import0pygame5screen0

pygame.font.init[]1pygame.font.init[]0pygame5import0pygame5import0# Total window5import0pygame5import0=3import0pygame5import0# initialise the pygame font3import0import37import0pygame5screen0

pygame.font.init[]1pygame.font.init[]0# initialise the pygame font3_______10pygame5import0pygame.font.init[]9import0pygame5import0screen3import0pygame5import0pygame5import0pygame5import0=3screen0

pygame.font.init[]1pygame.font.init[]0pygame5import0pygame.font.init[]3import0pygame5import0import37import0pygame5import0pygame5import0pygame5import0# Total window5import0# Total window7screen0

pygame.font.init[]1pygame.font.init[]0# Total window5_______10# Total window7import0pygame5import0pygame5import0pygame5import0pygame.font.init[]3import0pygame.font.init[]9import0pygame5import0pygame5screen0

pygame.font.init[]1pygame.font.init[]0pygame5import0pygame.font.init[]9import0# initialise the pygame font3import0# Total window7import0pygame5import0screen3import0pygame5import0pygame5import0pygame.font.init[]3pygame20

pygame21____220

 

pygame23

pygame24= pygame26pygame27import0pygame29import6

pygame31= pygame26pygame27import0pygame36import6

pygame38 pygame39

pygame21pygame41 pygame3

pygame21pygame3= pygame46pygame5pygame20________ξ2________ξ2pygame9

pygame21pygame41 pygame6

pygame21pygame6= pygame46# Total window5pygame20________ξ2________ξ2pygame9

 

pygame64

pygame38 pygame66

pygame21pygame68 pygame69pygame70 pygame71pygame72# Total window7pygame74

pygame.font.init[]1pygame76_______277import0pygame5import0pygame5pygame82pygame83 pygame9pygame85import37pygame87pygame88 pygame89pygame83pygame91pygame83 pygame9pygame88 import37pygame87pygame88 pygame89pygame834__3__302____

pygame.font.init[]1pygame76______277import0pygame5import0pygame5# initialise the pygame font12pygame88 pygame89pygame83 # initialise the pygame font16pygame83 # initialise the pygame font18pygame88 pygame89pygame83 # initialise the pygame font16pygame83 pygame9pygame88 # initialise the pygame font02pygame.font.init[]3# initialise the pygame font28

 

________x29

pygame38 ________xxx1

pygame21________xxxxx

________xxx4

pygame21pygame68 pygame69pygame70 pygame71 pygame72________ξξpygame74

pygame.font.init[]1pygame68 ________ξ45pygame70 pygame71 pygame72________ξξpygame74

________ξ51________ξ52 ________ξ53= pygame5________ξ56

 

________ξ57________ξ58

# initialise the pygame font57# initialise the pygame font60pygame5import0# initialise the pygame font63import0# initialise the pygame font63# initialise the pygame font66pygame83 # initialise the pygame font68pygame83 # initialise the pygame font70pygame88 # Total window5# initialise the pygame font73pygame88 # Total window5import2

 

________ξ57________ξ78

________ξ57________ξθ0= ________ξθ2________ξθξ________ξθ4# Total window5________ξθ6pygame5import0pygame5import0pygame5import2

________ξ57________ξ94pygame83 pygame9pygame88 ________ξ98________ξ99pygame83 pygame9pygame88 ________ξ98import2

pygame21pygame.font.init[]06

pygame21pygame68 pygame69pygame70 pygame71pygame72pygame.font.init[]13pygame74

pygame.font.init[]1________ξ52 import535import536import537import536import539== screen23________ξ56

Có một thuật toán cho Sudoku?

Sự thật thú vị về Sudoku là nó là một câu đố tầm thường để giải. Lý do nó dễ giải là vì có một thuật toán để giải Sudoku . Thuật toán này là thuật toán tìm kiếm dựa trên cây dựa trên việc quay lui trong cây cho đến khi tìm thấy giải pháp.

Quy tắc 45 trong Sudoku là gì?

Quy tắc 45 . Quy tắc này cũng có thể được sử dụng cho các câu đố nhỏ hơn. số là 21 cho câu đố 6x6 và 10 cho câu đố 4x4. Hãy xem lưới Killer Sudoku bên dưới. All the numbers in a given row, column or nonet should add up to 45. This rule can also be used smaller puzzles: the number is 21 for a 6x6 puzzle and 10 for a 4x4 puzzle. Take a look at the Killer Sudoku grid below.

Sudoku có thể được giải bằng toán học không?

Khi một người nghe nói rằng không cần làm toán để giải Sudoku, điều thực sự có nghĩa là không cần làm phép tính số học . Câu đố không phụ thuộc vào thực tế là chín chỗ dành sẵn được sử dụng là các chữ số từ 1 đến 9. Bất kỳ chín biểu tượng nào cũng sẽ phục vụ tốt để tạo và giải các câu đố.

Sudoku có cao không?

Từ nghiên cứu điển hình này, có thể kết luận rằng một cá nhân có kỹ năng giải các câu đố Sudoku có khả năng có chỉ số IQ chung cao . Kết quả về mối tương quan yếu giữa điểm số Sudoku và bài kiểm tra WAIT chỉ ra rằng trong một số trường hợp, Sudoku cao không nhất thiết có nghĩa là chỉ số IQ chung cao.

Chủ Đề