-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathState.py
90 lines (73 loc) · 3.43 KB
/
State.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
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
###############
# State Class #
###############
class State:
"""This class represents the state of the game.
:param board: the actual board that belongs to this state
:type board: :class:`Board.Board`
:param parent_state: the State that the current State came from after applying a legal move
:type parent_state: :class:`State.State`
:param depth: the depth of the current state (number of moves made)
:type depth: int
:param f-value: the priority order of the state; some heuristic function of the state. Defaults to *0*
:type f-value: int, optional
:Attributes:
* :board (:class:`Board.Board`): number of rows
* :parent_state (:class:`State.State`) the State prior to current state
* :depth (*int*): depth of state
* :fvalue (*int*): priorty order of the state. The value of some heuristic function. Defaults to *0*
"""
# The representation of the current game state
def __init__(self, board, parent_state, depth, fvalue=0):
"""Constructor Method"""
self.board = board.duplicate()
self.parent_state = parent_state
self.depth = depth
self.fvalue = fvalue
# Checks if the f-value of this board is less than the f-value of another board
def __lt__(self, other):
"""A function to check if the f-value of this board is less than the f-value of another board.
:param other: State to compare this state to
:type other: :class:`State.State`
:return: *True* if self.fvalue < other.fvalue, *False* if self.fvalue > other.fvalue
:rtype: bool
"""
return self.fvalue < other.fvalue
# Converts this State into a string
def __str__(self):
"""A function to convert the current state to a string representation readable by humans.
:return: string representation of state
:rtype: str
"""
return f"{self.board}\nf-value: {self.fvalue}\nsteps: {self.depth}\n"
# A function to explain how the state is made
def __repr__(self):
"""A function to explain how the state is made.
:return: A string explaining how the state is made
:rtype: str
"""
if self.parent_state is self:
return f'State({self.board!r}, "is own parent", {self.depth!r}, {self.fvalue!r})'
try:
return f'State({self.board!r}, {self.parent_state!r}, {self.depth!r}, {self.fvalue!r})'
except RecursionError:
return 'State("could not be represented due to RecursionError")'
# Checks if two States are the same. This only compares the boards.
def __eq__(self, other):
"""A function to check if two States are the same. This only compares the boards.
:param other: State to compare self to
:type other: :class:`State.State`
:return: *True* if self.board is equal to other.board, *False* otherwise
:rtype: bool
"""
if type(other) is not State:
return False
return self.board == other.board
# Function to print a completed path from the initial state to the solution state #
def printPath(self):
"""A function to print a complated path from the initial state to the solution state. This function calls print() statements directly, rather than returning a string.
:return: *None*
"""
print(self.board)
if self.parent_state is not None:
self.parent_state.printPath()