-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
127 lines (102 loc) · 3.98 KB
/
Copy pathgame.py
File metadata and controls
127 lines (102 loc) · 3.98 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
from board import *
from heuristics import heuristics_dict
from search import search_dict
from variable_selection import variable_selection_dict
class Game:
"""
Game engine class stores the current game state and controls when to
get input/draw output
"""
def __init__(self, xml_dict):
"""
:param xml_dict: dictionary with the following items: Puzzle name, Puzzle width, Puzzle height,
List of RGB values, paths, lists of lists of paths in the key color
"""
self.game_name = xml_dict["name"]
self.number_of_colors = len(xml_dict["colors"])
self.colors_dict = xml_dict['colors']
numbers_matrix, coloring_matrix = generate_matrix_from_xml_dict(xml_dict)
self.board = Board(self.number_of_colors, numbers_matrix)
self.initial_board = Board(self.number_of_colors, numbers_matrix)
self.goal_board = Board(self.number_of_colors, numbers_matrix, coloring_matrix)
self.search = None
self.variable_selection = None
self.heuristic = None
self.moves_counter = 0
# Filled only when used
self.boards_generator = None
def __str__(self):
"""
prints the board
"""
return (f'Search: {self.search}\nHeuristic: {self.heuristic}\nMoves counter: {self.moves_counter}'
f'\nBoard:\n{self.board}')
def do_move_csp(self):
"""
If no coordinates given, do the next move of the backtrack.
If coordinates are given, color the cell (x, y) with the color of 'cell_color'
Will increment moves counter by 1.
:return: List of all changed cells, with their new colors (list of ((number, color_number), cell_color))
"""
self.moves_counter += 1
self.board, path, color = next(self.boards_generator, (None, None, None))
return path, color
def do_move_other(self):
"""
If no coordinates given, do the next move of the backtrack.
If coordinates are given, color the cell (x, y) with the color of 'cell_color'
Will increment moves counter by 1.
:return: List of all changed cells, with their new colors (list of ((number, color_number), cell_color))
"""
self.moves_counter += 1
self.board = next(self.boards_generator, None)
return self.board.coloring_matrix
def is_goal_state(self):
"""
:return: True if we reached the goal state, else False
"""
return self.board == self.goal_board
# *** Getters *** #
def get_initial_board(self):
return self.initial_board
def get_current_numbers_matrix(self):
"""
returns the current board matrix
:return: Matrix of size (w*h) which contains [(number, number_color), cell_color]
"""
return self.board.numbers_matrix
def get_current_coloring_matrix(self):
"""
:return: The current colors matrix
"""
return self.board.coloring_matrix
def get_width(self):
"""
:return: The board's width
"""
return self.initial_board.get_width()
def get_height(self):
"""
:return: The board's height
"""
return self.initial_board.get_height()
def get_colors(self):
"""
:return: All the colors of the board
"""
return self.colors_dict
def get_moves_counter(self):
"""
:return: The number of moves made
"""
return self.moves_counter
# *** Setters *** #
def set_boards_generator(self, search, variable_selection, heuristic):
self.search = search_dict[search]
self.variable_selection = variable_selection_dict[variable_selection]
self.heuristic = heuristics_dict[heuristic]
self.boards_generator = self.search(self, self.variable_selection, self.heuristic)
def reset_game(self):
self.moves_counter = 0
self.board = copy.copy(self.initial_board)
self.boards_generator = None