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)
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)
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))
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
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)
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)
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
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
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
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... ')
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)
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
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
def RecordStrike(self, x, y, isHit): point = Point(x, y) self.recordedStrikes[point] = isHit
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"))), ])
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()))
def RecordStrike(self, x, y): strike = Point(x, y) self.recordedStrikes.add(strike) value = self.grid.Get(x, y) return (value is not None)
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))
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)
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))