Exemple #1
0
 def test_zero_length_path(self):
     g = Grid(3, 3)
     path = [
         point
         for point in gen_straight_path(g, Point(0, 0), Point(1, 1), 0)
     ]
     self.assertEqual([], path)
Exemple #2
0
 def test_path_inside_grid(self):
     g = Grid(3, 3)
     path = [
         point
         for point in gen_straight_path(g, Point(0, 0), Point(0, 1), 3)
     ]
     self.assertEqual([Point(0, 0), Point(0, 1), Point(0, 2)], path)
Exemple #3
0
    def test_is_in_col_run(self):
        g = Grid(3,
                 3,
                 initial_grid=[[None, "red", None], [None, "red", None],
                               ["black", "red", "red"]])
        self.assertTrue(is_in_col_run(g, Point(2, 1), 3))

        g = Grid(3,
                 3,
                 initial_grid=[[None, "red", None], [None, "red", None],
                               ["red", "black", "black"]])
        self.assertFalse(is_in_col_run(g, Point(2, 1), 3))
Exemple #4
0
    def test_is_in_connected_diag_up(self):
        g = Grid(3,
                 3,
                 initial_grid=[[None, "red", "red"], [None, "red", None],
                               ["red", "black", "red"]])
        self.assertTrue(is_in_diag_up_run(g, Point(1, 1), 3))

        g = Grid(3,
                 3,
                 initial_grid=[[None, "red", None], [None, "red", None],
                               ["red", "black", "black"]])
        self.assertFalse(is_in_diag_up_run(g, Point(1, 1), 3))
def is_in_row_run(grid, point, length, start_state=None):
    if start_state is None:
        start_state = grid.at(point)
    else:
        start_state = start_state
    num_matches_left = run_len(
        grid,
        gen_straight_path(grid, Point(point.row, point.col - 1), Point(0, -1),
                          length - 1), start_state)
    num_matches_right = run_len(
        grid,
        gen_straight_path(grid, Point(point.row, point.col + 1), Point(0, 1),
                          length - 1), start_state)
    return num_matches_left + num_matches_right >= length - 1
def is_in_col_run(grid, point, length, start_state=None):
    if start_state is None:
        start_state = grid.at(point)
    else:
        start_state = start_state
    num_matches_down = run_len(
        grid,
        gen_straight_path(grid, Point(point.row - 1, point.col), Point(-1, 0),
                          length - 1), start_state)
    num_matches_up = run_len(
        grid,
        gen_straight_path(grid, Point(point.row + 1, point.col), Point(1, 0),
                          length - 1), start_state)
    return num_matches_down + num_matches_up >= length - 1
def is_in_diag_down_run(grid, point, length, start_state=None):
    if start_state is None:
        start_state = grid.at(point)
    else:
        start_state = start_state
    num_matches_diag_down_right = run_len(
        grid,
        gen_straight_path(grid, Point(point.row + 1, point.col + 1),
                          Point(1, 1), length - 1), start_state)
    num_matches_diag_up_left = run_len(
        grid,
        gen_straight_path(grid, Point(point.row - 1, point.col - 1),
                          Point(-1, -1), length - 1), start_state)
    return num_matches_diag_down_right + num_matches_diag_up_left >= length - 1
def gen_straight_path(grid, start, step, max_length):
    """Yields a path of points starting from |start| up to length |max_length| with each
  step |step| from the previous one. All points guaranteed to be inside of |grid|."""
    for i in range(max_length):
        next_point = Point(start.row + step.row * i, start.col + step.col * i)
        if grid.is_inside(next_point):
            yield next_point
Exemple #9
0
 def add_disc(self, col_index, color):
     for row_index in range(self.grid.height):
         if self.grid[row_index][col_index] is DiscState.empty:
             self.grid[row_index][col_index] = color
             return Point(row_index, col_index)
             break
     else:
         raise ValueError("column %i is full" % col_index)
Exemple #10
0
 def runTestCases(self, testCases):
     for case in testCases:
         points = tuple(Point(x, y) for (x, y) in case.PointTuples)
         if len(case.ExceptionRegex) > 0:
             self.assertRaisesRegexp(Exception, case.ExceptionRegex,
                                     lambda: Ship(points))
         else:
             Ship(points)
Exemple #11
0
def get_nodes(maze):
    maze_graph = {}
    for y in range(maze.rows):
        for x in range(maze.columns):
            loc = Point(x, y)
            v = maze.get(loc)
            if v.isalpha() or v == '@':
                maze_graph[v] = Node(v, loc)
    return maze_graph
Exemple #12
0
def count_bugs(plutonian):
    total = 0
    for y in range(plutonian.rows):
        for x in range(plutonian.cols):
            location = Point(x, y)
            if location == plutonian.center:
                continue
            for l in plutonian.levels:
                total += 1 if is_bug(plutonian.get(location, l)) else 0
    return total
Exemple #13
0
def advance_generation(eris):
    new_eris = copy.deepcopy(eris)
    for y in range(new_eris.rows):
        for x in range(new_eris.columns):
            location = Point(x, y)
            n = eris.count_neighbors(location)
            new_value = '.'
            if n == 1 or (not is_bug(eris.get(location)) and n == 2):
                new_value = '#'
            new_eris.set(location, new_value)
    return new_eris
Exemple #14
0
 def send_packet(self, packet):
   address, x, y = packet
   if address < 50:
     in_q = self.computers[address].InputQueue()
     in_q.write_computer(x,y)
   elif address == 255:
     self.nat = Point(x, y)
     if self.first_nat:
       print('First time writing to NAT: %s' % (self.nat))
       self.first_nat = False
   else:
     print('This should not happen... ')
Exemple #15
0
    def NextStrike(self, playedPoints):
        rawInput = raw_input("Please enter an x, y ")
        rawX, rawY = (s.strip() for s in rawInput.split(','))
        if not rawX.isdigit():
            print "The value: ", rawX, "isn't valid"
            return self.NextStrike()
        x = int(rawX)
        if x not in range(1, 11):
            print "The value: ", x, "isn't valid"
            return self.NextStrike()
        y = rawY.upper()
        if y not in "ABCDEFGHIJ":
            print "The value: ", y, "isn't valid"
            return self.NextStrike()

        return Point(x, y)
Exemple #16
0
def plutonian_advance(plutonian):
    # First, note that we will probably need one level up and one level down.
    # So add them to the current one.
    plutonian.extend_levels()
    levels = plutonian.get_levels_range()
    new_plutonian = copy.deepcopy(plutonian)
    for y in range(plutonian.rows):
        for x in range(plutonian.cols):
            location = Point(x, y)
            if location == plutonian.center:
                # No point in looking at this.
                continue
            for l in levels:
                n = plutonian.count_neighbors(location, l)
                new_value = '.'
                if n == 1 or (not is_bug(plutonian.get(location, l))
                              and n == 2):
                    new_value = '#'
                new_plutonian.set(location, l, new_value)
    return new_plutonian
Exemple #17
0
def get_winning_move(grid, current_player):
    for col_index in range(grid.width):
        row_index = grid_utils.get_row_of_first(grid, col_index,
                                                DiscState.empty)
        current_point = Point(row_index, col_index)
        if not grid.is_inside(current_point):
            continue

        if grid_utils.is_in_row_run(grid, current_point, 4, current_player):
            return col_index
        if grid_utils.is_in_col_run(grid, current_point, 4, current_player):
            return col_index
        if grid_utils.is_in_diag_down_run(grid, current_point, 4,
                                          current_player):
            return col_index
        if grid_utils.is_in_diag_up_run(grid, current_point, 4,
                                        current_player):
            return col_index

    else:
        return None
def get_row_of_first(grid, col_index, value):
    row = 0
    while row < grid.height and grid.at(Point(row, col_index)) is not value:
        row += 1
    return row
def is_row_all(grid, row_index, value):
    for col_index in range(grid.width):
        if grid.at(Point(row_index, col_index)) is not value:
            return False
    else:
        return True
Exemple #20
0
 def RecordStrike(self, x, y, isHit):
     point = Point(x, y)
     self.recordedStrikes[point] = isHit
Exemple #21
0
 def StartingFleet(self):
     return Fleet([
         Ship((Point(1, "A"), Point(2, "A"))),
         Ship((Point(1, "B"), Point(2, "B"), Point(3, "B"))),
         Ship((Point(1, "C"), Point(2, "C"), Point(3, "C"), Point(4, "C"))),
         Ship((Point(1, "D"), Point(2, "D"), Point(3, "D"), Point(4, "D"),
               Point(5, "D"))),
     ])
Exemple #22
0
 def test_all_valid_points(self):
     self.assertEqual(
         set(Point(x, y)
             for x in range(1,11)
             for y in "ABCDEFGHIJ"),
         set(Grid.IterAllPoints()))
Exemple #23
0
    def RecordStrike(self, x, y):
        strike = Point(x, y)
        self.recordedStrikes.add(strike)

        value = self.grid.Get(x, y)
        return (value is not None)
Exemple #24
0
 def test_run_len_full_path(self):
     g = Grid(2, 3, initial_grid=[[1, 1, 1], [0, 0, 0]])
     path = [Point(0, 0), Point(0, 1), Point(0, 2)]
     self.assertEqual(3, run_len(g, path, 1))
Exemple #25
0
from grid import Grid, Point, Rectangle

g = Grid()


class Point:
    def __init__(self, x, y, col='blue'):
        self.x = x
        self.y = y
        self.col = col

    def draw(self):
        g.draw_dot(self.x, self.y, self.col)


p1 = Point(0, 0, 'red')
p1.draw()

g.done()
def down(point):
    return Point(point.row - 1, point.col)
def up(point):
    return Point(point.row + 1, point.col)
def right(point):
    return Point(point.row, point.col + 1)
def left(point):
    return Point(point.row, point.col - 1)
Exemple #30
0
 def test_run_len_zero(self):
     g = Grid(3, 3, initial_value=1)
     path = [Point(0, 0), Point(0, 1), Point(0, 2)]
     self.assertEqual(0, run_len(g, path, 0))