-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathSearcher.py
More file actions
147 lines (120 loc) · 5.68 KB
/
pathSearcher.py
File metadata and controls
147 lines (120 loc) · 5.68 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from path import Path
def calculate_block_moves(grid, col, rotation):
"""
Calculates the moves for block rotation and placement based on column and rotation.
Args:
grid: The grid representing the current state of the game.
col (int): The column index for block placement.
rotation (int): The rotation index for block rotation.
Returns:
list: A list of moves representing block rotation and placement.
"""
moves = []
moves.extend(["ROTATE"] * rotation)
moves.extend(["RIGHT"] * col)
moves.extend(["DOWN"] * grid.num_rows)
return moves
class PathSearcher:
"""
A class responsible for searching and analyzing paths for Tetris block movement.
Attributes:
paths (list): A list to store the paths found during the search.
game: The Tetris game instance.
height_weight (float): Weight assigned to the height of the block.
lines_cleared_weight (float): Weight assigned to the number of lines cleared.
holes_weight (float): Weight assigned to the number of holes in the grid.
blockades_weight (float): Weight assigned to the number of blockades in the grid.
"""
def __init__(self):
"""
Initializes the PathSearcher class.
"""
self.paths = []
self.game = None
self.height_weight = 0
self.lines_cleared_weight = 0
self.holes_weight = 0
self.blockades_weight = 0
def calculate_move_rank(self, path):
"""
Calculates the rank of a given path based on weights assigned to different factors.
Args:
path (Path): The path to calculate the rank for.
Returns:
float: The calculated rank for the path.
"""
move_rank = (
self.height_weight * path.height +
self.lines_cleared_weight * path.lines_cleared +
self.holes_weight * path.holes +
self.blockades_weight * path.blockades
# self.height_weight * path.height +
# self.lines_cleared_weight * path.lines_cleared +
# self.holes_weight * path.holes
)
return move_rank
def set_weights(self, height_weight, lines_cleared_weight, holes_weight, blockades_weight):
"""
Sets the weights for different factors used in calculating path ranks.
Args:
height_weight (float): Weight assigned to the height of the block.
lines_cleared_weight (float): Weight assigned to the number of lines cleared.
holes_weight (float): Weight assigned to the number of holes in the grid.
blockades_weight (float): Weight assigned to the number of blockades in the grid.
"""
self.height_weight = height_weight
self.lines_cleared_weight = lines_cleared_weight
self.holes_weight = holes_weight
self.blockades_weight = blockades_weight
print("Player Weights")
print(self.height_weight, self.lines_cleared_weight, self.holes_weight, self.blockades_weight)
def calculate_paths(self, game, grid, player_height_weight, player_lines_cleared_weight, player_holes_weight,
player_blockades_weight):
"""
Calculates all possible paths for block movement based on the current game state and grid.
Args:
game: The Tetris game instance.
grid: The grid representing the current state of the game.
player_height_weight (float): Weight assigned to the height of the block.
player_lines_cleared_weight (float): Weight assigned to the number of lines cleared.
player_holes_weight (float): Weight assigned to the number of holes in the grid.
player_blockades_weight (float): Weight assigned to the number of blockades in the grid.
Returns:
list: A list containing all the calculated paths.
"""
self.set_weights(player_height_weight, player_lines_cleared_weight, player_holes_weight,
player_blockades_weight)
for rotation in range(4):
for col in range(game.grid.num_cols):
print("The column is ---------------------")
print(col)
current_grid = grid.copy()
game_copy = game.copy()
block = game.current_block.copy()
moves = calculate_block_moves(current_grid, col, rotation)
holes, blockades, full_rows, max_height = game_copy.apply_moves_to_grid(moves)
path = self.create_path(moves, holes, blockades, full_rows, max_height)
self.paths.append(path)
return self.paths
def create_path(self, moves, holes, blockades, full_rows, max_height):
"""
Creates a Path object based on the given parameters.
Args:
moves (list): A list of moves representing block rotation and placement.
holes (int): The number of holes in the grid after block placement.
blockades (int): The number of blockades in the grid after block placement.
full_rows (int): The number of full rows in the grid after block placement.
max_height (int): The maximum height of the grid after block placement.
Returns:
Path: The created Path object.
"""
path = Path()
path.set_moves(moves)
height_of_path = max_height
lines_cleared_of_path = full_rows
holes_of_path = holes
blockades_of_path = blockades
path.set_attributes(height_of_path, lines_cleared_of_path, holes_of_path, blockades_of_path)
rank = self.calculate_move_rank(path)
path.set_rank(rank)
return path