Added All files.

This commit is contained in:
Nikas36
2019-09-04 18:18:53 +03:00
committed by GitHub
parent eec2c93773
commit d940bd7519
14 changed files with 262 additions and 0 deletions

182
android_solver.py Normal file
View File

@@ -0,0 +1,182 @@
# This program allows the sudoku_solver.py script to interact with an android emulator running the app
# 'Sudoku - The Clean One'.
import pyautogui
import time
import sudoku_solver
board = [
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]
]
startx, starty = pyautogui.locateCenterOnScreen('./numbers/back.png')
startx = startx - 20
starty = starty + 73
def board_scanner():
# Scan for Number 1s
pyautogui.click(startx + 60, starty + 512)
time.sleep(.25)
for y in range(9):
for x in range(9):
if pyautogui.pixelMatchesColor(int(startx + x*52), int(starty + y*52), (168, 181, 189), tolerance=10):
board[y][x] = 1
# Scan for Number 2s
pyautogui.click(startx + 141, starty + 512)
time.sleep(.25)
for y in range(9):
for x in range(9):
if pyautogui.pixelMatchesColor(int(startx + x*52), int(starty + y*52), (168, 181, 189), tolerance=10):
board[y][x] = 2
# Scan for Number 3s
pyautogui.click(startx + 220, starty + 512)
time.sleep(.25)
for y in range(9):
for x in range(9):
if pyautogui.pixelMatchesColor(int(startx + x*52), int(starty + y*52), (168, 181, 189), tolerance=10):
board[y][x] = 3
# Scan for Number 4s
pyautogui.click(startx + 301, starty + 512)
time.sleep(.25)
for y in range(9):
for x in range(9):
if pyautogui.pixelMatchesColor(int(startx + x * 52), int(starty + y * 52), (168, 181, 189), tolerance=10):
board[y][x] = 4
# Scan for Number 5s
pyautogui.click(startx + 376, starty + 512)
time.sleep(.25)
for y in range(9):
for x in range(9):
if pyautogui.pixelMatchesColor(int(startx + x * 52), int(starty + y * 52), (168, 181, 189), tolerance=10):
board[y][x] = 5
# Scan for Number 6s
pyautogui.click(startx + 60, starty + 597)
time.sleep(.25)
for y in range(9):
for x in range(9):
if pyautogui.pixelMatchesColor(int(startx + x * 52), int(starty + y * 52), (168, 181, 189), tolerance=10):
board[y][x] = 6
# Scan for Number 7s
pyautogui.click(startx + 141, starty + 597)
time.sleep(.25)
for y in range(9):
for x in range(9):
if pyautogui.pixelMatchesColor(int(startx + x * 52), int(starty + y * 52), (168, 181, 189), tolerance=10):
board[y][x] = 7
# Scan for Number 8s
pyautogui.click(startx + 220, starty + 597)
time.sleep(.25)
for y in range(9):
for x in range(9):
if pyautogui.pixelMatchesColor(int(startx + x * 52), int(starty + y * 52), (168, 181, 189), tolerance=10):
board[y][x] = 8
# Scan for Number 9s
pyautogui.click(startx + 301, starty + 597)
time.sleep(.25)
for y in range(9):
for x in range(9):
if pyautogui.pixelMatchesColor(int(startx + x * 52), int(starty + y * 52), (168, 181, 189), tolerance=10):
board[y][x] = 9
def fill_in():
# Place Number 1s
pyautogui.click(startx + 60, starty + 512)
time.sleep(.25)
for y in range(len(board)):
for x in range(len(board)):
if board[y][x] == 1:
pyautogui.click(int(startx + x*52), int(starty + y*52))
# Place Number 2s
pyautogui.click(startx + 141, starty + 512)
time.sleep(.25)
for y in range(len(board)):
for x in range(len(board)):
if board[y][x] == 2:
pyautogui.click(int(startx + x*52), int(starty + y*52))
# Place Number 3s
pyautogui.click(startx + 220, starty + 512)
time.sleep(.25)
for y in range(len(board)):
for x in range(len(board)):
if board[y][x] == 3:
pyautogui.click(int(startx + x*52), int(starty + y*52))
# Place Number 4s
pyautogui.click(startx + 301, starty + 512)
time.sleep(.25)
for y in range(len(board)):
for x in range(len(board)):
if board[y][x] == 4:
pyautogui.click(int(startx + x*52), int(starty + y*52))
# Place Number 5s
pyautogui.click(startx + 376, starty + 512)
time.sleep(.25)
for y in range(len(board)):
for x in range(len(board)):
if board[y][x] == 5:
pyautogui.click(int(startx + x*52), int(starty + y*52))
# Place Number 6s
pyautogui.click(startx + 60, starty + 597)
time.sleep(.25)
for y in range(len(board)):
for x in range(len(board)):
if board[y][x] == 6:
pyautogui.click(int(startx + x*52), int(starty + y*52))
# Place Number 7s
pyautogui.click(startx + 141, starty + 597)
time.sleep(.25)
for y in range(len(board)):
for x in range(len(board)):
if board[y][x] == 7:
pyautogui.click(int(startx + x*52), int(starty + y*52))
# Place Number 8s
pyautogui.click(startx + 220, starty + 597)
time.sleep(.25)
for y in range(len(board)):
for x in range(len(board)):
if board[y][x] == 8:
pyautogui.click(int(startx + x*52), int(starty + y*52))
# Place Number 9s
pyautogui.click(startx + 301, starty + 597)
time.sleep(.25)
for y in range(len(board)):
for x in range(len(board)):
if board[y][x] == 9:
pyautogui.click(int(startx + x*52), int(starty + y*52))
print('\n\nEmpty Board:')
sudoku_solver.printb(board)
board_scanner()
print('\n\nScanned Board:')
sudoku_solver.printb(board)
sudoku_solver.solve(board)
print('\n\n\nSolved Board:')
sudoku_solver.printb(board)
print('\n\n')
print(board)
fill_in()

8
mousescanner.py Normal file
View File

@@ -0,0 +1,8 @@
import pyautogui
import time
while True:
print(pyautogui.position())
x, y = pyautogui.position()
print(pyautogui.pixel(x,y))
time.sleep(3)

BIN
numbers/1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 920 B

BIN
numbers/2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
numbers/3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
numbers/4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

BIN
numbers/5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
numbers/6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
numbers/7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1000 B

BIN
numbers/8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
numbers/9.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
numbers/back.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 557 B

BIN
numbers/board.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

72
sudoku_solver.py Normal file
View File

@@ -0,0 +1,72 @@
# This program solves a sudoku by backtracking and filling all possible spaces.
def solve(board):
empty = find_empty(board)
if empty == None:
return True
else:
x, y = empty
for i in range(1,10):
if check_validity(board, x, y, i):
board[y][x] = i
if solve(board):
return True
board[y][x] = 0
return False
# This function Prints a Sudoku Board
def printb(board):
for x in range(len(board)):
if x % 3 == 0:
print('-------------------------')
for y in range(len(board[x])):
if y % 3 == 0:
print('|', end=' ')
print(board[x][y], end=' ')
print('|')
print('-------------------------')
# This function checks if a passed number in a set of coordinates in valid.
def check_validity(board, x, y, num):
# Check horizontal
for i in range(len(board)):
if board[y][i] == num and x != i:
return False
# Check vertical
for i in range(len(board)):
if board[i][x] == num and y != i:
return False
# Check square
sqr_x = (x // 3) * 3
sqr_y = (y // 3) * 3
for i in range(sqr_y, sqr_y + 3):
for j in range(sqr_x, sqr_x + 3):
if board[i][j] == num and (i, j) != (y, x):
return False
return True
# This function finds the first empty square in the Sudoku.
def find_empty(board):
for y in range(len(board)):
for x in range(len(board)):
if board[y][x] == 0:
return x, y
return None
# printb(board)
# starttime = time.time()
# solve(board)
# print('\n--------------------------\n')
# printb(board)
# print('\nIt took us ' + str(round(time.time() - starttime)) + ' seconds to solve the sudoku.')