def generate_boards(n, size=10, prob=None):
    l = []
    for i in range(n):
        uprob = rd.random() if prob is None else prob
        b = generate_board(size, size, uprob)
        p = Game(b)
        l.append((size, [p.lists[1], p.lists[0]], b))
    return l
示例#2
0
            # if temp < Tmin:
            #     temp = Tmin
        it += 1

    return solution.board if error == 0 else None


def sa(width: int, col_rest: list, row_rest: list):
    game = Game(None, width, width, lists=[row_rest, col_rest])
    return sa_solve(game)


if __name__ == '__main__':
    from generator import generate_board
    from matplotlib.pylab import *

    board = generate_board(10, 10)
    g = Game(board)
    g.print()
    print(board)
    print('\n------------------------\n')
    e = []
    t = []
    p = []
    print(sa_solve(g, e, t, p))
    plot(e, color='red')
    show()
    plot(t, color='blue')
    plot(p, color='green')
    show()
示例#3
0
文件: solver.py 项目: patelsd/Sudoku
from itertools import product
from generator import generate_board

sudoku_board = generate_board()


def solve(board):
    find = find_empty(board)
    if not find:
        return True
    else:
        row, col = find

    for i in range(1, 10):
        if valid(board, i, (row, col)):
            board[row][col] = i

            if solve(board):
                return True

            board[row][col] = 0

    return False


def valid(board, num, position):
    # Check row for matching numbers
    for i in range(len(board[0])):
        if board[position[0]][i] == num and position[1] != i:
            return False
示例#4
0
文件: game.py 项目: Oussama2IA/Sudoku
 def generate_board(self):
     board = generate_board()
     for i in range(9):
         for j in range(9):
             self.board[i][j] = board[i][j]
             self.INITIAL_BOARD[i][j] = board[i][j]
示例#5
0
文件: game.py 项目: Oussama2IA/Sudoku
                click_position = pygame.mouse.get_pos()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_1:
                    number = 1
                if event.key == pygame.K_2:
                    number = 2
                if event.key == pygame.K_3:
                    number = 3
                if event.key == pygame.K_4:
                    number = 4
                if event.key == pygame.K_5:
                    number = 5
                if event.key == pygame.K_6:
                    number = 6
                if event.key == pygame.K_7:
                    number = 7
                if event.key == pygame.K_8:
                    number = 8
                if event.key == pygame.K_9:
                    number = 9
                if event.key == pygame.K_BACKSPACE:
                    number = 0

        draw(position, number, hover_position, click_position)

    pygame.quit()

if __name__ == "__main__":
    board = Board(generate_board())
    menu = Menu()
    main()
示例#6
0
    solution = LocalSearchSolution(game, next='iterative')
    error = solution.eval()
    ctr = 0
    while it < MAX_ITERATIONS and error > 0:
        solution.next()
        e = solution.eval()
        if e > error:
            solution.go_back()
            ctr += 1
        else:
            ctr = 0
            error = e
        if ctr == 1000:
            print('Restart', error)
            solution = LocalSearchSolution(game, next='iterative')
            error = solution.eval()
        it += 1

    return solution.board if error == 0 else None


if __name__ == '__main__':
    from generator import generate_board

    b = generate_board(15, 15)
    g = Game(b)
    g.print()
    print(b)
    print('\n-----------------------\n')
    print(lsmr_solve(g))
示例#7
0
    display_solving(Board.from_cell_array(test_boards.EASY_BOARD))  # solved
    input("\n\npress any key continue with the next board...")

    display_solving(Board.from_cell_array(test_boards.MEDIUM_BOARD))  # solved
    input("\n\npress any key continue with the next board...")

    display_solving(Board.from_cell_array(
        test_boards.MEDIUM_BOARD_2))  # solved
    input("\n\npress any key continue with the next board...")

    display_solving(Board.from_cell_array(
        test_boards.MEDIUM_BOARD_3))  # solved
    input("\n\npress any key continue with the next board...")

    display_solving(Board.from_cell_array(test_boards.HARD_BOARD))  # solved
    input("\n\npress any key continue with the next board...")

    display_solving(Board.from_cell_array(test_boards.HARDEST_BOARD))  # solved
    input("\n\npress any key continue with the next board...")

    display_solving(Board.from_cell_array(test_boards.EVIL_BOARD))  # solved
    input("\n\npress any key continue with the next board...")

    display_solving(Board.from_cell_array(test_boards.HARDEST_BOARD_2),
                    0)  # solved
    input("\n\npress any key continue with the next board...")

    display_solving(generate_board())  # solved

    print("\n\nall test boards solved")
示例#8
0
        _, v = generate_game(self.board)
        k = self.game.lists[1]
        error = 0
        for i in range(len(v)):
            line = v[i]
            correct = k[i]
            error += abs(len(line) - len(correct))
            for j in range(min(len(line), len(correct))):
                if line[j] != correct[j]: error += 1
        return error


if __name__ == '__main__':
    from generator import generate_board

    board = generate_board(5, 5)
    game = Game(board)
    sol = LocalSearchSolution(game)
    lsol = LocalSearchSolution(game, initial='leftmost')
    rsol = LocalSearchSolution(game, initial='rightmost')
    print('leftmost', lsol.board, sep='\n')
    print('rightmost', rsol.board, sep='\n')
    board2 = sol.board
    game.print()
    print(board, board2, sep='\n\n')
    print(game.check_horizontal(board2))
    print(game.check_vertical(board2), '\n')

    sol.next()
    print(sol.board, '\n')
    sol.next()