#!/usr/bin/env python import socket import time import random import copy s=socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("194.106.195.60",9503)) # fetch manual data = s.recv(1024) print "MANUAL: " + data # fetch board board = s.recv(1024).translate(None, "|-").split("\n") board.remove('') board.remove('') board.remove('') board.remove('') board.remove('') for y in range(9): board[y] = list(board[y]) print "BOARD: " + str(board) backup = [] guess = -1 solved = 0 while 1: # print "SCANNING:" found = 0 for y in range(9): for x in range(9): if board[y][x] == "_": possible = [ "1", "2", "3", "4", "5", "6", "7", "8", "9" ] # CHECK LINE for i in range(9): if board[y][i] in possible: possible.remove(board[y][i]) # CHECK ROW for i in range(9): if board[i][x] in possible: possible.remove(board[i][x]) # CHECK SQUARE sy = (y/3) * 3 sx = (x/3) * 3 for i in range(sy,sy+3): for j in range(sx,sx+3): if board[i][j] in possible: possible.remove(board[i][j]) # print "POSSIBLE for pos (" + str(x) + "," + str(y) + ") --> " + str(possible) if len(possible) == 0: ## GUESSING FAILED!! # print "GUESSING FAILED!!" board = copy.deepcopy(backup) found = 0 else: if len(possible) == 1: board[y][x] = possible[0] found = 1 else: if (guess != -1) and (guess == y): r = random.randint(0,len(possible)-1) # print "GUESSED " + str(y) + "," + str(x) + " to " + str(possible[r]) board[y][x] = possible[r] found = 1 guess = -1 guess = -1 if found == 0: solution = "" for y in range(9): solution = solution + "".join(board[y]) if "_" in solution: guess = random.randint(0,9) if backup == []: backup = copy.deepcopy(board) # print "CANNOT SOLVE! GUESSING LINE: " + str(guess) continue # break else: print "SOLUTION: " + solution s.send("solution " + solution) # time.sleep(1) solved += 1 print "Solved: " + str(solved) + "x" if (solved == 100): while 1: print s.recv(1024) data = "" while not "-------------" in data: data = data + s.recv(1024) print "GOT: " + data board = data.translate(None, "|-").split("\n")[2:] board.remove('') board.remove('') board.remove('') board.remove('') board.remove('') for y in range(9): board[y] = list(board[y]) print "BOARD: " + str(board) backup = [] s.close()