Exemple #1
0
class FromScratchTest(unittest.TestCase):
    def setUp(self):
        self.board = Board()
        self.board.initialize_grid((10, 10))

    def testBasicGet(self):
        self.assertIsNone(self.board.get((3, 3)))
Exemple #2
0
    def _surround_bridge_with_simnodes(self):
        # Attach NAND/Wire horizontally
        self.board.set((2, 2), Wire())
        self.board.set((4, 2), Board.deserialize_simnode('r'))

        # Attach Switch/Wire vertically
        self.board.set((3, 1), Wire())
        self.board.set((3, 3), Board.deserialize_simnode('o'))

        self.board.tick()
        self.board.tick()
Exemple #3
0
def main():

    parser = argparse.ArgumentParser(description='Tile-based digital logic '
                                     'sandbox')
    parser.add_argument('--file', help='Load state from file')
    parser.add_argument('-x',
                        '--width',
                        dest='width',
                        metavar='N',
                        type=int,
                        default=10,
                        help='Width of the board')
    parser.add_argument('-y',
                        '--height',
                        dest='height',
                        metavar='N',
                        type=int,
                        default=10,
                        help='Height of the board')

    box_parser = parser.add_mutually_exclusive_group(required=False)
    box_parser.add_argument('--box-draw',
                            dest='box_draw',
                            action='store_true',
                            help='Enable UTF-8 box drawing characters')
    box_parser.add_argument('--no-box-draw',
                            dest='box_draw',
                            action='store_false',
                            help='Disable UTF-8 box drawing characters')
    box_parser.set_defaults(box_draw=True)

    args = parser.parse_args()

    logger.debug(args)

    if args.file:
        # Read board from file
        with open(args.file, 'r') as f:
            board_str = f.read()
            board = Board.deserialize(board_str)
    else:
        # Create a new board
        board = Board()
        board.initialize_grid((args.width, args.height))

    # # Start up the UI
    world = World([board])
    t = TermUI(args, world)
    # Block until we quit the UI
    t.start()
Exemple #4
0
 def setUp(self):
     # Create two boards, both of which form a nand loop
     self.boards = [
         # Directly output to simnode
         Board.deserialize("--D\n"
                           "u.d\n"
                           "---\n"),
         # Output via wire
         Board.deserialize("--D\n"
                           "u.-\n"
                           "--l\n")
     ]
     # Keep track of the leftmost nand on both grids.
     # They should both always have the same value.
     self.nands = [b.get((0, 1)) for b in self.boards]
Exemple #5
0
 def setUp(self):
     board_str = ("...o..\n"
                  "-R-|--\n"
                  "...-..\n")
     self.board = Board.deserialize(board_str)
     self.bridge_coords = (3, 1)
     self.bridge = self.board.get(self.bridge_coords)
Exemple #6
0
    def setUp(self):
        board_str = ("-r--\n")
        self.board = Board.deserialize(board_str)

        self.input_wire = self.board.get((0, 0))
        self.nand = self.board.get((1, 0))
        self.output_wires = [self.board.get((2, 0)), self.board.get((3, 0))]
Exemple #7
0
    def setUp(self):
        self.board = Board.deserialize("R--\n"
                                       "-..\n")
        self.nand = self.board.get((0, 0))
        self.bottom_wire = self.board.get((0, 1))
        self.right_wire = self.board.get((2, 0))

        self.nand.rotate_facing(1, (0, 0), self.board)
Exemple #8
0
    def setUp(self):
        board_str = ("-R---")
        self.board = Board.deserialize(board_str)

        self.nand = self.board.get((1, 0))

        # Split the wire
        self.board.set((3, 0), None)
Exemple #9
0
 def setUp(self):
     board_str = (".--l..\n"
                  "......\n"
                  "-r.|.-\n"
                  "......\n")
     self.board = Board.deserialize(board_str)
     self.bridge_coords = (3, 2)
     self.bridge = self.board.get(self.bridge_coords)
Exemple #10
0
    def setUp(self):
        board_str = ("-rR-.\n" ".....\n" "-r-R-\n")
        self.board = Board.deserialize(board_str)

        self.input_wires = [self.board.get((0, 0)), self.board.get((0, 2))]
        self.left_nands = [self.board.get((1, 0)), self.board.get((1, 2))]
        self.right_nands = [self.board.get((2, 0)), self.board.get((3, 2))]
        self.output_wires = [self.board.get((3, 0)), self.board.get((4, 2))]
        self.middle_wire = self.board.get((2, 2))
Exemple #11
0
    def setUp(self):
        board_str = ("-r-..-\n" "-.-..x\n" "---..-\n")
        self.board = Board.deserialize(board_str)

        self.nand = self.board.get((1, 0))
        self.nand_wire = self.board.get((1, 2))

        self.switch = self.board.get((5, 1))
        self.switch_wires = [self.board.get((5, 0)), self.board.get((5, 2))]
Exemple #12
0
    def setUp(self):
        board_str = ("---\n"
                     "d..\n"
                     "---\n")
        self.board = Board.deserialize(board_str)
        self.nand = self.board.get((0, 1))

        # Dynamically finish the loop
        self.board.set((2, 1), Wire())
Exemple #13
0
    def setUp(self):
        board_str = ("-R-.-")
        self.board = Board.deserialize(board_str)

        self.nand = self.board.get((1, 0))

        # Join the two wires
        self.new_wire = Wire()
        self.board.set((3, 0), self.new_wire)
        self.placed_wire = self.board.get((3, 0))
Exemple #14
0
    def setUp(self):
        board_str = (".--l..\n" "...-..\n" "-r-|--\n" "...-..\n")
        self.board = Board.deserialize(board_str)
        self.top_nand = self.board.get((3, 0))
        self.right_wire = self.board.get((5, 2))
        self.bridge = self.board.get((3, 2))

        self.board.tick()
        self.board.tick()
        self.board.tick()
Exemple #15
0
    def setUp(self):
        board_str = ("r-...-l\n"
                     "r-----l\n"
                     "l-...-r\n")
        self.board = Board.deserialize(board_str)
        self.center_coords = (3, 1)

        self.left_input_nands = {self.board.get((0, 0)),
                                 self.board.get((0, 1))}
        self.left_output_nand = self.board.get((0, 2))

        self.right_input_nands = {self.board.get((6, 0)),
                                  self.board.get((6, 1))}
        self.right_output_nand = self.board.get((6, 2))
Exemple #16
0
    def process_queue(self):
        """Reads a single message from the queue."""
        message = self.queue.get()
        logger.info(f"Got message: {message}")

        tile_set = message.get('tile_set')
        nand_rotate = message.get('nand_rotate')
        tick = message.get('tick')
        switch_toggle = message.get('switch_toggle')

        if tile_set:
            node = Board.deserialize_simnode(tile_set['node'])
            coord = tile_set['coord']
            index = tile_set['index']
            self.boards[index].set(coord, node)

        elif nand_rotate:
            coord = nand_rotate['coord']
            index = nand_rotate['index']
            delta = nand_rotate['delta']

            board = self.boards[index]
            nand = board.get(coord)

            nand.rotate_facing(delta, coord, board)

        elif switch_toggle:
            coord = switch_toggle['coord']
            index = switch_toggle['index']
            value = switch_toggle['value']

            board = self.boards[index]
            switch = board.get(coord)

            switch.toggle(value)

        elif tick:
            for i in range(tick):
                for board in self.boards:
                    board.tick()
Exemple #17
0
    def setUp(self):
        board_str = ("--...-\n" "d....-\n" "--...-\n")
        self.board = Board.deserialize(board_str)

        self.board.copy((0, 0), (2, 3), (3, 0))
Exemple #18
0
 def setUp(self):
     board_str = ("r----r-.-\n"
                  ".-.......\n"
                  "-l-d.....\n"
                  "-..--....\n")
     self.board = Board.deserialize(board_str)
Exemple #19
0
 def setUp(self):
     self.board = Board()
     self.board.initialize_grid((10, 10))
Exemple #20
0
 def setUp(self):
     board_str = (".--\n"
                  "d.-\n"
                  ".--\n")
     self.board = Board.deserialize(board_str)
     self.nand = self.board.get((0, 1))
Exemple #21
0
 def setUp(self):
     self.board = Board.deserialize("dd\n")
     self.nand = self.board.get((0, 0))
Exemple #22
0
    def setUp(self):
        board_str = ("-R-r-")
        self.board = Board.deserialize(board_str)
        self.board.set((3, 0), Wire())

        self.nand = self.board.get((1, 0))
Exemple #23
0
 def setUp(self):
     self.board_str = ("--udlr\n" "--UDLR\n" "U-xo--\n")
     self.board = Board.deserialize(self.board_str)