Пример #1
0
class Game:
    def __init__(self, generate=False, solve=False):
        self.generate = generate
        self.solve = solve
        self.data_manager = DataManager() if solve else None
        self.data = self.data_manager.read_data(
        ) if self.data_manager else None
        self.board = Board(data=self.data)
        self.solver = Solver(self.board) if solve else None

    def run(self):
        if self.solve:
            # Solve existing SuDoKu
            self.board.display()
            input('\n<enter> to solve...\n')
            self.solve_board()
            self.board.display()
        elif self.generate:
            # Generate random SuDoKu
            raise NotImplementedError(
                'SuDoKu generator is not implemented yed.')

    def solve_board(self):
        try:
            while not self.solver.is_solved():
                self.solver.place_next()
        except ImpossibleToSolveError:
            print(
                '\nThis SuDoKu is impossible to solve with any of the implemented techniques.'
            )
            print('\nPencil marks:\n')
            self.solver.display_pencil_marks()
Пример #2
0
 def __init__(self, generate=False, solve=False):
     self.generate = generate
     self.solve = solve
     self.data_manager = DataManager() if solve else None
     self.data = self.data_manager.read_data(
     ) if self.data_manager else None
     self.board = Board(data=self.data)
     self.solver = Solver(self.board) if solve else None
Пример #3
0
class Game:
    def __init__(self):
        """
        Constructor. Create new game board.
        """
        self.__board = Board()
        self.__movement = {
            Direction.up: self.__board.move_up,
            Direction.down: self.__board.move_down,
            Direction.left: self.__board.move_left,
            Direction.right: self.__board.move_right
        }

    def restart(self):
        """
        Restarts game board.
        """
        self.__board = Board()

    def get_tiles(self):
        """
        Defensive copy of current board tiles.
        """
        return self.__board.get_tiles()

    def print_tiles(self):
        print(self.__board)

    def attempt_movement(self):
        """
        Tries all possible movements. Returns a list of (direction, tiles after movement) tuples.
        """
        movement_list = list()
        for direction in [Direction.left, Direction.down, Direction.right, Direction.up]:
            movable, tiles = self.__board.try_move(direction)
            if movable:
                movement_list.append((direction, tiles))

        return movement_list if len(movement_list) != 0 else None

    def move(self, direction):
        """
        Move tiles towards a specific redirection.

        Returns True if win. Otherwise, returns None.
        """
        self.__movement[direction]()
        if self.__board.is_win():
            return True
        self.__board.generate_new_tile()
Пример #4
0
 def __init__(self):
     """
     Constructor. Create new game board.
     """
     self.__board = Board()
     self.__movement = {
         Direction.up: self.__board.move_up,
         Direction.down: self.__board.move_down,
         Direction.left: self.__board.move_left,
         Direction.right: self.__board.move_right
     }
Пример #5
0
 def restart(self):
     """
     Restarts game board.
     """
     self.__board = Board()
Пример #6
0
from main.board import Board

__author__ = 'jeremy'

if __name__ == '__main__':
    game_board = Board()
    game_board.generate_new_tile()
    game_board.generate_new_tile()
    game_board.generate_new_tile()
    game_board.generate_new_tile()
    game_board.generate_new_tile()
    game_board.generate_new_tile()
    print(game_board)
    game_board.move_left()
    print(game_board)
    game_board.move_right()
    print(game_board)
    copy_game_board = game_board.get_tiles()
    print(copy_game_board)