Skip to content

Instantly share code, notes, and snippets.

@kgleeson
Last active May 23, 2018 20:08
Show Gist options
  • Save kgleeson/6ff63f6c1f6f8074f02c41862fe0d0e0 to your computer and use it in GitHub Desktop.
Save kgleeson/6ff63f6c1f6f8074f02c41862fe0d0e0 to your computer and use it in GitHub Desktop.
class Round(object):
def __init__(self, num, num_moves, deactivate_tile):
super(Round, self).__init__()
self.num = num
self.num_moves = num_moves
self.results = []
self.starting_points = []
self.deactivate_tile = deactivate_tile
def move(self, start, num_moves):
if num_moves:
for tile in tiles[start].viable_moves:
if tiles[tile].active:
self.move(tile, num_moves - 1)
else:
self.add_result(start)
def print_grid(self):
print '''
%s %s %s
%s %s %s
%s %s %s
''' % tuple(tile.grid_info() for tile in tiles)
def start_round(self):
print "== Round %d ==" % self.num
print "Number of moves: %s" % self.num_moves
if self.deactivate_tile:
self.deactivate_tile.deactivate()
for starting_point in self.starting_points:
self.move(starting_point, self.num_moves)
def add_result(self, result):
if result not in self.results:
self.results.append(result)
def add_starting_points(self, starting_points):
self.starting_points = starting_points
def push_starting_points(self):
if self.num < len(rounds) - 1:
next_round = self.num + 1
rounds[next_round].add_starting_points(self.results)
def print_results(self):
multiples = True if len(self.results) > 1 else False
print "Valid choice%s: %s" % ('s' if multiples else '', ', '.join(
str(choice) for choice in self.results))
print "=============\n"
def finish_round(self):
self.print_grid()
self.print_results()
self.push_starting_points()
def process_round(self):
self.start_round()
self.finish_round()
class Tile(object):
def __init__(self, num, viable_moves):
super(Tile, self).__init__()
self.num = num
self.viable_moves = viable_moves
self.active = True
def deactivate(self):
self.active = False
def grid_info(self):
if self.active:
return str(self.num)
else:
return "X"
tiles = [
Tile(0, [1, 3]),
Tile(1, [0, 2, 4]),
Tile(2, [1, 5]),
Tile(3, [0, 4, 6]),
Tile(4, [1, 3, 5, 7]),
Tile(5, [2, 4, 8]),
Tile(6, [3, 7]),
Tile(7, [4, 6, 8]),
Tile(8, [5, 7]),
]
rounds = [
Round(0, 1, False),
Round(1, 7, tiles[2]),
Round(2, 3, tiles[3]),
Round(3, 7, tiles[0]),
Round(4, 5, tiles[1]),
Round(5, 9, tiles[6]),
Round(6, 3, tiles[5]),
Round(7, 1, tiles[4]),
Round(8, 0, tiles[7]),
]
def main():
rounds[0].starting_points.append(2)
for current_round in rounds:
current_round.process_round()
if __name__ == '__main__':
main()
== Round 0 ==
Number of moves: 1
0 1 2
3 4 5
6 7 8
Valid choices: 1, 5
=============
== Round 1 ==
Number of moves: 7
0 1 X
3 4 5
6 7 8
Valid choices: 0, 4, 6, 8
=============
== Round 2 ==
Number of moves: 3
0 1 X
X 4 5
6 7 8
Valid choices: 1, 5, 7
=============
== Round 3 ==
Number of moves: 7
X 1 X
X 4 5
6 7 8
Valid choices: 4, 8, 6
=============
== Round 4 ==
Number of moves: 5
X X X
X 4 5
6 7 8
Valid choices: 5, 7
=============
== Round 5 ==
Number of moves: 9
X X X
X 4 5
X 7 8
Valid choices: 4, 8
=============
== Round 6 ==
Number of moves: 3
X X X
X 4 X
X 7 8
Valid choice: 7
=============
== Round 7 ==
Number of moves: 1
X X X
X X X
X 7 8
Valid choice: 8
=============
== Round 8 ==
Number of moves: 0
X X X
X X X
X X 8
Valid choice: 8
=============
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment