Ejemplo n.º 1
0
def input_cell(x, y, value):
    cell = Cell(x, y, value)
    if value != 0:
        cell.isfixed = True
    board.input_cell(cell)

    if cell.value == 0:
        eel.setCellColor(cell.x, cell.y, 'unset')

    for c in board:
        if c.isfixed is True:
            eel.setCellColor(c.x, c.y, 'fixed')
        else:
            eel.setCellColor(c.x, c.y, 'unset')

    if not board.is_valid():
        for cell_, classes in board.errors.items():
            for c in board:
                if 'x' in classes:
                    if c.x == cell_.x and c.value == cell_.value:
                        eel.setCellColor(c.x, c.y, 'error')
                if 'y' in classes:
                    if c.y == cell_.y and c.value == cell_.value:
                        eel.setCellColor(c.x, c.y, 'error')
                if 'b' in classes:
                    if c.block == cell_.block and c.value == cell_.value:
                        eel.setCellColor(c.x, c.y, 'error')
Ejemplo n.º 2
0
def set_pattern(pattern, width, height):
    assert (pattern in PATTERNS
            or pattern == 'random'), f'{pattern} is not a valid pattern!'

    if (width != 80 or height != 45) and pattern != 'random':
        return 0

    if pattern == 'random':
        return Board(width, height).board

    setup = list()

    for i in range(height):
        setup.append([])

    for row in setup:
        for i in range(width):
            c = Cell()
            c.set_state(0)
            row.append(c)

    for cell in PATTERNS[pattern]:
        setup[cell[0]][cell[1]].set_state(1)

    return setup
Ejemplo n.º 3
0
def test_cells_with_same_values_should_be_equal():
    # given
    cell1 = Cell('C', 1, 0)
    cell2 = Cell('C', 1, 0)
    # when
    res = cell1 == cell2
    # then
    expect(res).to(be(True))
Ejemplo n.º 4
0
def test_given_a_edge_cell_it_should_return_coordinates_of_correct_neighbours(
):
    # given
    cell = Cell('C', 1, 0)
    # when
    res = cell.get_coordinates_of_neighbours()
    # then
    expect(set(res)).to(equal(set([(0, 0), (0, 1), (1, 1), (2, 0), (2, 1)])))
Ejemplo n.º 5
0
def test_given_a_char_board_should_return_corresponding_cells():
    # given
    board = Board(['CDHG', 'ADFF', 'TKJU', 'FTGT'])
    # when
    res = board._get_cells_of_char('F')
    # then
    expect(list(res)).to(
        equal([Cell('F', 1, 2),
               Cell('F', 1, 3),
               Cell('F', 3, 0)]))
Ejemplo n.º 6
0
def test_given_a_corner_cell_board_should_return_neighbour_cells():
    # given
    board = Board(['CDHG', 'ADFF', 'TKJU', 'FTGT'])
    cell = Cell('C', 0, 0)
    # when
    res = board._get_neighbour_cells(cell)
    # then
    expect(set(res)).to(
        equal(set([Cell('A', 1, 0),
                   Cell('D', 1, 1),
                   Cell('D', 0, 1)])))
Ejemplo n.º 7
0
 def getBoardStateFromHash(self, boardStateHash):
     boardState = []
     for i in range(self.board.size):
         row = []
         for j in range(self.board.size):
             if int(boardStateHash[i * self.board.size + j]) == 0:
                 row.append(Cell(State.EMPTY))
             elif int(boardStateHash[i * self.board.size + j]) == 1:
                 row.append(Cell(State.X))
             else:
                 row.append(Cell(State.O))
         boardState.append(row)
     return boardState
Ejemplo n.º 8
0
    def parse_pos(token):
        try:
            cell = Cell(token[0], token[1])
            return cell
        except ValueError:
            pass

        return None
Ejemplo n.º 9
0
def setCells(line, board):
    toupleProcessXCoord = processQuantity(line[5:])
    toupleProcessYCoord = processQuantity(line[(6 + toupleProcessXCoord[0]):])
    position = (toupleProcessXCoord[1], toupleProcessYCoord[1])
    listStones = generateStones(line[(6 + toupleProcessXCoord[0] + 1 +
                                      toupleProcessYCoord[0]):])
    cell = Cell(listStones[0], listStones[1], listStones[2], listStones[3])
    board.addCell(position, cell)
    return board
Ejemplo n.º 10
0
def get_grid(boards, no):
    integer_board = np.array(boards[no * 9:no * 9 + 9, :], int)

    grid = []
    for i in range(0, 9):
        row = []
        for j in range(0, 9):
            value = integer_board[i][j]
            row.append(Cell(value))
        grid.append(row)
    grid = np.array(grid)

    return grid
Ejemplo n.º 11
0
def write_results():
    global steps_taken

    with open('test_results.csv', mode='w') as csv_file:
        csv_writer = csv.writer(csv_file,
                                delimiter=',',
                                quotechar='"',
                                quoting=csv.QUOTE_MINIMAL)

        csv_writer.writerow(['Puzzle no.', 'Time to complete', 'No. of steps'])

        for no in range(0, 4):
            board_no = no
            # 0:9, 9:18, 18:27, 27:36
            integer_board = np.array(boards[board_no * 9:board_no * 9 + 9, :],
                                     int)

            # setup grid
            grid = []
            for i in range(0, 9):
                row = []
                for j in range(0, 9):
                    value = integer_board[i][j]
                    row.append(Cell(value))
                grid.append(row)
            grid = np.array(grid)

            steps_taken = 0
            board = Board(grid)

            start_time = datetime.now()
            print_board(board)
            print('solving... puzzle: ', board_no)
            solve_board(board)

            end_time = datetime.now()
            time_diff = (end_time - start_time)  #.strftime('%H:%M:%S')

            csv_writer.writerow([board_no, str(time_diff), steps_taken])
Ejemplo n.º 12
0
#     [None, 4, 1, None, None, 6, None, 5, 2],
#     [None, None, None, None, None, 3, None, 1, None],
#     [None, None, None, 1, 9, 8, 5, None, None],
#     [7, 1, None, 5, None, None, None, None, None]
# ]

board = [[None, None, None, None, 3, None, None, None, None],
         [None, 1, 3, 4, None, 8, 9, None, None],
         [None, 9, None, 5, None, None, None, 8, None],
         [4, None, None, None, 9, None, 2, None, 5],
         [None, 5, 2, None, None, 1, None, None, 7],
         [6, None, None, 2, None, 5, 4, None, None],
         [3, None, None, 6, None, 4, 5, None, None],
         [None, 2, 7, None, None, None, 6, None, 9],
         [None, 4, 6, None, None, 9, None, None, None]]

sudoku_board = Board()

for x, row in enumerate(board):
    for y, cell in enumerate(row):
        value = cell if cell is not None else 0
        cell = Cell(x, y, val=value)
        sudoku_board.input_cell(cell)

if sudoku_board.is_valid():
    solved = Board.solve(sudoku_board)
    for cell in solved:
        print(cell.value)
else:
    print('invalid board')
Ejemplo n.º 13
0
import unittest

import copy

import numpy as np

from board import Board, Cell

from sudoku import find_next_empty, is_in_col, is_in_row, is_in_square, is_valid_placement, print_board

x = 0

grid = np.array([
    [
        Cell(x),
        Cell(x),
        Cell(3),
        Cell(x),
        Cell(2),
        Cell(x),
        Cell(6),
        Cell(x),
        Cell(x)
    ],
    [
        Cell(9),
        Cell(x),
        Cell(x),
        Cell(3),
        Cell(x),
Ejemplo n.º 14
0
 def __init__(self):
     Obstacle.__init__(self)
     Cell.__init__(self)
     self.__xpos = 0
     self.__ypos = 0
Ejemplo n.º 15
0
def read_cells(board):
    number_of_cells = int(input())
    for _ in range(number_of_cells):
        cell = Cell.from_string(input())
        board.cells[cell.index] = cell
        board.update_cells_at_dist()
Ejemplo n.º 16
0
    def test_flagMines(self):

        flagCorners = [[Cell('X'),
                        Cell(2),
                        Cell(1),
                        Cell('X'),
                        Cell('X')],
                       [Cell('X'),
                        Cell(2),
                        Cell(1),
                        Cell(3),
                        Cell('X')],
                       [Cell(1), Cell(1),
                        Cell('-'),
                        Cell(1), Cell(1)],
                       [Cell(1),
                        Cell(1),
                        Cell('X'),
                        Cell('X'),
                        Cell('X')],
                       [Cell('X'),
                        Cell(1),
                        Cell('X'),
                        Cell('X'),
                        Cell('X')]]
        flagCornersCorrect = [['M', 2, 1, 'M', 'M'], ['M', 2, 1, 3, 'M'],
                              [1, 1, '-', 1, 1], [1, 1, 'X', 'X', 'X'],
                              ['M', 1, 'X', 'X', 'X']]
        flagCornersGameBoard = Board(5, 4)
        flagCornersGameBoard.player = flagCorners
        self.assertTrue(
            checkBoardEqual(flagMines(flagCornersGameBoard),
                            flagCornersCorrect, 5))

        noMines = [[Cell('X'),
                    Cell('X'),
                    Cell('X'),
                    Cell('X'),
                    Cell('X')],
                   [Cell('X'),
                    Cell('X'),
                    Cell('X'),
                    Cell('X'),
                    Cell('X')],
                   [Cell('X'),
                    Cell('X'),
                    Cell('X'),
                    Cell('X'),
                    Cell('X')],
                   [Cell('X'),
                    Cell('X'),
                    Cell('X'),
                    Cell('X'),
                    Cell('X')],
                   [Cell('X'),
                    Cell('X'),
                    Cell('X'),
                    Cell('X'),
                    Cell('X')]]
        noMinesCorrect = [['X', 'X', 'X', 'X', 'X'], ['X', 'X', 'X', 'X', 'X'],
                          ['X', 'X', 'X', 'X', 'X'], ['X', 'X', 'X', 'X', 'X'],
                          ['X', 'X', 'X', 'X', 'X']]
        noMinesGameBoard = Board(5, 4)
        noMinesGameBoard.player = noMines
        self.assertTrue(
            checkBoardEqual(flagMines(noMinesGameBoard), noMinesCorrect, 5))

        flagMiddle = [[Cell('X'),
                       Cell('X'),
                       Cell('X'),
                       Cell('X'),
                       Cell('X')],
                      [Cell('X'),
                       Cell('X'),
                       Cell('X'),
                       Cell('-'),
                       Cell('-')],
                      [Cell('X'),
                       Cell(8),
                       Cell('X'),
                       Cell(4),
                       Cell('-')],
                      [Cell('X'),
                       Cell('X'),
                       Cell('X'),
                       Cell('X'),
                       Cell('-')],
                      [Cell('X'),
                       Cell('X'),
                       Cell('X'),
                       Cell('X'),
                       Cell('X')]]
        flagMiddleCorrect = [['X', 'X', 'X', 'X', 'X'],
                             ['M', 'M', 'M', '-', '-'], ['M', 8, 'M', 4, '-'],
                             ['M', 'M', 'M', 'M', '-'],
                             ['X', 'X', 'X', 'X', 'X']]
        flagMiddleGameBoard = Board(5, 4)
        flagMiddleGameBoard.player = flagMiddle
        self.assertTrue(
            checkBoardEqual(flagMines(flagMiddleGameBoard), flagMiddleCorrect,
                            5))
Ejemplo n.º 17
0
 def test_init():
     box = Cell((5, 5), False, 9, Cell.EMPTY)
     assert box.guesses == [1, 2, 3, 4, 5, 6, 7, 8, 9]
     assert box.is_blocked == False
     assert box.pos == (5, 5)
     assert box.value == Cell.EMPTY
Ejemplo n.º 18
0
 def __init__(self):
     Obstacle.__init__(self)
     Cell.__init__(self)
Ejemplo n.º 19
0
def box():
    new_box = Cell((5, 5), False, 9, Cell.EMPTY)
    return new_box
Ejemplo n.º 20
0
 def __init__(self):
     Cell.__init__(self)
     Obstacle.__init__(self)
Ejemplo n.º 21
0
    def test_openCells(self):
        # this board must resort to random choice, therefore openCells() should leave board unchanged and return False
        noLogicalChoice = [
            [Cell(1), Cell('X'),
             Cell(2),
             Cell('X'), Cell('X')],
            [Cell('X'), Cell('X'),
             Cell('X'), Cell('X'),
             Cell('X')], [Cell(1),
                          Cell('X'),
                          Cell('X'),
                          Cell('X'),
                          Cell(1)],
            [Cell('X'), Cell('X'),
             Cell('X'), Cell('X'),
             Cell('X')], [Cell('X'),
                          Cell(4),
                          Cell('X'),
                          Cell('X'),
                          Cell(1)]
        ]
        noLogicalChoiceCorrect = [[1, 'X', 2, 'X', 'X'],
                                  ['X', 'X', 'X', 'X', 'X'],
                                  [1, 'X', 'X', 'X', 1],
                                  ['X', 'X', 'X', 'X', 'X'],
                                  ['X', 4, 'X', 'X', 1]]
        noLogicalChoiceGameBoard = Board(5, 4)
        noLogicalChoiceGameBoard.player = noLogicalChoice
        result = openCells(noLogicalChoiceGameBoard)
        self.assertTrue(checkBoardEqual(result[0], noLogicalChoiceCorrect, 5))
        self.assertFalse(result[1])

        # this test ensures that cells are being appropriately opened
        openMultiple = [
            [Cell(1), Cell(1),
             Cell('-'), Cell(1),
             Cell('M', True)],
            [Cell('M', True),
             Cell(1), Cell('-'),
             Cell(1), Cell(1)],
            [Cell('X'), Cell(1),
             Cell('-'), Cell('-'),
             Cell('-')], [Cell('X'),
                          Cell(1),
                          Cell(1),
                          Cell(1),
                          Cell(1)],
            [Cell('X'),
             Cell('X'),
             Cell('X'),
             Cell('M', True),
             Cell(1)]
        ]

        solution = [[Cell(1),
                     Cell(1),
                     Cell('-'),
                     Cell(1),
                     Cell('M', True)],
                    [Cell('M'),
                     Cell(1), Cell('-'),
                     Cell(1), Cell(1)],
                    [Cell(1),
                     Cell(1),
                     Cell('-'),
                     Cell('-'),
                     Cell('-')], [Cell(1),
                                  Cell(1),
                                  Cell(1),
                                  Cell(1),
                                  Cell(1)],
                    [
                        Cell('M', True),
                        Cell(1),
                        Cell(1),
                        Cell('M', True),
                        Cell(1)
                    ]]
        openMultipleCorrect = [[1, 1, '-', 1, 'M'], ['M', 1, '-', 1, 1],
                               [1, 1, '-', '-', '-'], [1, 1, 1, 1, 1],
                               ['X', 1, 1, 'M', 1]]
        openMultipleGameBoard = Board(5, 4)
        openMultipleGameBoard.player = openMultiple
        openMultipleGameBoard.solution = solution
        result = openCells(openMultipleGameBoard)
        self.assertTrue(checkBoardEqual(result[0], openMultipleCorrect, 5))
        self.assertTrue(result[1])