def __init__(self, file_name): try: self.fixed_board = SudokuBoard(file_name) except FileNotFoundError: exit(1) self.player_input = None self.missing_elements = None
def main(player): game_board = Extreme_3 if player == "human": human_player(game_board) elif player == "AI logic only": B = SudokuBoard(game_board) L = SudokuLogicManager(B) print(L) changing = True while changing: changing = L.logical_step() print(L) print(L) elif player == "AI" or player == "AI bifercating": # instantiating all necessary classes B = SudokuBoard(game_board) L = SudokuLogicManager(B) T = SudokuSearchTree(L) print(T) # doing bifercated search leaf = T.search() if leaf == False: print("Could not find a solution :(") raise Exception("Could not find a solution :(") else: print(leaf.val) print("Solution Found!") else: raise ValueError(f"Expected either 'human' or 'player', got {player}")
from board import SudokuBoard import json app = Flask(__name__) import os from flask import Flask, flash, request, redirect, url_for from werkzeug.utils import secure_filename UPLOAD_FOLDER = './uploads' ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'} app = Flask(__name__) app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER board = SudokuBoard() def allowed_file(filename): return '.' in filename and \ filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS @app.route('/', methods=['GET', 'POST']) def upload_file(): if request.method == 'POST': # check if the post request has the file part if 'file' not in request.files: flash('No file part') return redirect(request.url) file = request.files['file']
def copy_node(self): return SudokuSearchTree(self.val.copy()) def evolve(self, E): r, c, val = E self.val.B.move(r, c, val) self.val.update_candidates(r, c, val) return self if __name__ == "__main__": from board import SudokuBoard from puzzles import * B = SudokuBoard(Extreme_3) print(B) L = SudokuLogicManager(B) print(L) T = SudokuSearchTree(L) leaf = T.search() if leaf == False: print("Could not find solution") raise Exception("could not solve board at index", i) else: print(leaf.val) print("Solution Found!") """ changing = True
def __init__(self, filled_initial_cells): """ Constructor: filled_initial_cells is a dictionary specifying which cells are already filled. """ super().__init__(SudokuBoard(filled_initial_cells))
updates temperature by formula: T(i+1) = T(i) * cooling rate :return: """ self.temperature = self.temperature * self.cooling_rate self.check_for_reheat() def button_load(self,file): """ button load, loads sudoku task in application :param file: :return: """ self.board.clear() f = open(file,"r") for x in range(9): line = f.readline() for y in range(9): if int(line[y]) != 0: self.board.set(y,x,int(line[y]),True) self.calculate_rows_columns_costs() self.board.cost_global.value = self.calculate_cost_global() self.gui.sync_board_and_canvas() self.state = -1 random.seed(time.time()) sudoku_board = SudokuBoard() gui = SudokuGUI(Tk(), sudoku_board, SudokuSA(sudoku_board)) gui.mainloop()
def main(): board = SudokuBoard('./images/tests/0.png') for column in board.get_board(): print(column)
class SudokuGame: """ The Game's Logic """ ROWS = COLUMNS = 9 def __init__(self, file_name): try: self.fixed_board = SudokuBoard(file_name) except FileNotFoundError: exit(1) self.player_input = None self.missing_elements = None def start_game(self): """ Start Game """ self.player_input = copy.deepcopy(self.fixed_board.puzzle) counter = 0 for row in self.player_input: for element in row: if element == 0: counter += 1 self.missing_elements = counter def insert_number(self, number, row, column): """ inserts a number into a non-fixed cell if the number and cell coordinates are valid. :param row: int. range 0 - 8 :param column: int. range 0 - 8 :param number: int. range 1 - 9 :return: True if inserted """ if self.__validate_row_and_col_numbers( row, column) and self.fixed_board.is_cell_empty(row, column): if number < 1 or number > 9 and type(number) != int: return False else: if not self.__is_column_valid(number, column): return False if not self.__is_row_valid(number, row): return False if not self.__is_square_valid(number, row, column): return False self.player_input[row][column] = number self.missing_elements -= 1 return True return False def clear_number(self, row, column): """ Function clear number from a cell from a non-fixed cell :param row: int. range 0 - 8 :param column: int. range 0 - 8 :return: True on success """ if self.__validate_row_and_col_numbers( row, column) and self.fixed_board.is_cell_empty(row, column): if self.player_input[row][column]: self.player_input[row][column] = 0 self.missing_elements += 1 return True return False def is_game_won(self): """ Function returns True if the all missing elements were filled correctly :return: True if won otherwise False """ if self.missing_elements == 0: return True return False def __validate_row_and_col_numbers(self, row, column): if row < 0 or row >= SudokuGame.ROWS: return False if column < 0 or column >= SudokuGame.COLUMNS: return False return True def __is_square_valid(self, number, row, column): """ Checks if candidate number is unique in a square of 3X3 cells ,that the selected cell belongs to. :param number: int range 1 - 9 :param row: int. row number of cell :param column: int. column number of cell :return: True if all the numbers unique """ def calc_start(x): return (x // 3) * 3 def calc_end(x): return ((x + 3) // 3) * 3 start_row = calc_start(row) end_row = calc_end(row) start_col = calc_start(column) end_col = calc_end(column) square_of_cells = [ element for element_list in self.player_input[start_row:end_row] for element in element_list[start_col:end_col] ] if number in square_of_cells and number != 0: return False return True def __is_row_valid(self, number, row): """ Checks if a candidate number is unique in the row. :param number: int. range 1 - 9 :param row: int. row number of cell :return: True if the number is unique """ row_of_numbers = self.player_input[row] if number in row_of_numbers and number != 0: return False return True def __is_column_valid(self, num, column): """ Checks if a candidate number is unique in the column. :param num: int. range 1 - 9 :param column: int. column number of cell :return: True if the number is unique """ col_of_numbers = [row[column] for row in self.player_input] if num in col_of_numbers and num != 0: return False return True
def accept(board): if board.next == False: return True return False def solve(board): if accept(board): return True (x, y) = board.next for i in range(1, 10): board.set(x, y, i) if board.check(x, y): if solve(x, y) == True: return True board.erase(x, y) return False if __name__ == '__main__': board = SudokuBoard([[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]]) print(board) solve(board) print(board)
def human_player(game_board): B = SudokuBoard(game_board) print() print(B) while not B.is_complete(): """Getting next move""" print # Getting row valid = False while not valid: r = input("row: ") try: if int(float(r)) != float(r) or int(r) < 1 or int(r) > 9: print("invalid input") else: valid = True except: print("invalid input") r = int(r) # Getting col valid = False while not valid: c = input("col: ") try: if int(float(c)) != float(c) or int(c) < 1 or int(c) > 9: print("invalid input") else: valid = True except: print("invalid input") c = int(c) # Getting num valid = False while not valid: num = input("number: ") try: if int(float(num)) != float(num) or int(num) < 1 or int( num) > 9: print("invalid input") else: valid = True except: print("invalid input") num = int(num) # Checking if it's a valid move if B.get_cell(r, c) == 0: temp = B.copy() temp.move(r, c, num) if temp.is_consistent(): B.move(r, c, num) print() print(B) else: print( "This move causes a repeated value in either a row, column, or square" ) else: print("That spot is already occupied")