-
Notifications
You must be signed in to change notification settings - Fork 0
/
mover.py
50 lines (42 loc) · 1.64 KB
/
mover.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from tile import Tile
class Mover:
@staticmethod
def alter_tile(tile, direction, up):
for rowIndex, row in enumerate(tile.layout):
for squareIndex, square in enumerate(row):
if(square == 0):
if(up):
return Mover.move_vertically(tile, direction, rowIndex, squareIndex)
else:
return Mover.move_horizontally(tile, direction, rowIndex, squareIndex)
@staticmethod
def move_vertically(tile, direction, rowIndex, squareIndex):
if(rowIndex == direction + 1):
return None
return Tile.swap(Tile.duplicate(tile), rowIndex + direction, squareIndex, rowIndex, squareIndex)
@staticmethod
def move_horizontally(tile, direction, rowIndex, squareIndex):
if(squareIndex == direction + 1):
return None
return Tile.swap(Tile.duplicate(tile), rowIndex, squareIndex + direction, rowIndex, squareIndex)
@staticmethod
def move_down(tile):
return Mover.alter_tile(tile, direction=1, up=True)
@staticmethod
def move_up(tile):
return Mover.alter_tile(tile, direction=-1, up=True)
@staticmethod
def move_right(tile):
return Mover.alter_tile(tile, direction=1, up=False)
@staticmethod
def move_left(tile):
return Mover.alter_tile(tile, direction=-1, up=False)
@staticmethod
def possible_moves(tile):
moves = [
Mover.move_up(tile),
Mover.move_left(tile),
Mover.move_right(tile),
Mover.move_down(tile),
]
return [item for item in moves if item != None ]