def main():
    totalEdits = 0
    badEdits = 0
    goodEdit = True
    inStr = ''

    #Loading file
    fileName = input('Please enter a file name: ')
    puzzle = sudoku.load_puzzle(fileName)

    #This goes so long as the game hasn't been solved and the user hasn't inputted "quit"
    while not inStr.lower() == 'quit' and not sudoku.gameSolved(puzzle):

        #Checks if edit user wants is allowed
        if goodEdit:
            print('\nYour puzzle: ')
            print(sudoku.puzzle_to_string(puzzle))
        else:
            badEdits += 1

        totalEdits += 1

        #Where the input happens
        if len(puzzle) > 9:
            print(
                "You loaded in a board larger than 9x9, numbers inputted larger than nine will show as letters"
            )
        inStr = input('Enter move row,col,number(quit to exit): ')

        #Slicing the input string into numbers to be used in a helper function
        if inStr[0].isnumeric():
            ins = inStr.split(',')
            goodEdit = sudoku.editPuzzle(puzzle, int(ins[0]), int(ins[1]),
                                         int(ins[2]))

    #Displays if the game was solved
    if sudoku.gameSolved(puzzle):
        print(sudoku.puzzle_to_string(puzzle))
        print('\nYou solved the puzzle!')
    #Displays if user quit the game
    else:
        #I need to decrement total edits by 1 since the program counts the input "quit" as an entered number
        totalEdits -= 1
        print('\nYou did not complete the puzzle')

    print('You entered ' + str(totalEdits) + ' numbers in total')
    print('You entered ' + str(badEdits) + ' invalid number(s)')
Beispiel #2
0
def main():
    if len(sys.argv) < 2:
        print "Usage: python ga_agent.py file"
        sys.exit()

    initial_puzzle = sudoku.load_puzzle(sys.argv[1])
    population = get_initial_population(initial_puzzle, POPULATION_SIZE)

    for generation in range(GENERATIONS):
        if sudoku.is_puzzle_valid(population[0]):
            break

        new_population = get_elite_members(population, ELITE_MEMBER_COUNT)
        for i in range(POPULATION_SIZE - ELITE_MEMBER_COUNT):
            puzzle = random.choice(population)
            mutated_puzzle = get_mutated_puzzle(initial_puzzle, puzzle)
            new_population.append(mutated_puzzle)
        population = new_population

        print "Generation %d's Fitness Score: %d" % (generation, get_highest_fitness_score(population))

    print "Best Solution:"
    print population[0]
Beispiel #3
0
#!/usr/bin/python3.6
import sys

from functools import partial

from sudoku import load_puzzle, validate_puzzle, get_row, get_column,get_block,get_block_for_coord,get_used_numbers,get_free_numbers,iterate_puzzle

def print_free(p, target, row,col, val):
    if val == 0 and row == target:
        print('({0},{1}) -> {2}'.format(col, row, get_free_numbers(p, row, col)))
        
puzzle_file = sys.argv[1]
target = int(sys.argv[2])
p = load_puzzle(puzzle_file)
iterate_puzzle(p, partial(print_free, p, target))