Traktor/kolejka.py

48 lines
1.6 KiB
Python
Raw Normal View History

2024-05-18 17:10:56 +02:00
from constant import rows, cols
2024-04-14 14:37:32 +02:00
class Stan:
2024-05-18 17:10:56 +02:00
def __init__(self, row, col, direction):
2024-04-14 14:37:32 +02:00
self.p = []
self.a = ""
self.row = row
self.col = col
2024-05-18 17:10:56 +02:00
self.direction = direction
2024-04-14 14:37:32 +02:00
def __str__(self):
return f"row: {self.row}, col: {self.col}, direction: {self.direction}"
2024-05-18 17:10:56 +02:00
2024-04-14 14:37:32 +02:00
def parrent(self, stan, action):
2024-05-18 17:10:56 +02:00
if len(self.p) == 0:
2024-04-14 14:37:32 +02:00
self.p.append(stan)
self.a = action
2024-05-18 17:10:56 +02:00
def succ(self, action, board):
move_offsets = {
"up": {"up": (-1, 0), "left": (0, -1), "down": (1, 0), "right": (0, 1)},
"left": {"up": "left", "left": "down", "down": "right", "right": "up"},
"right": {"up": "right", "right": "down", "down": "left", "left": "up"}
}
2024-04-14 14:37:32 +02:00
2024-05-18 17:10:56 +02:00
if action == "up":
row_offset, col_offset = move_offsets[action][self.direction]
new_row = self.row + row_offset
new_col = self.col + col_offset
if 0 <= new_row < rows and 0 <= new_col < cols and not board.is_rock(new_row, new_col):
return Stan(new_row, new_col, self.direction)
2024-04-14 14:37:32 +02:00
2024-05-18 17:10:56 +02:00
elif action in ["left", "right"]:
new_direction = move_offsets[action][self.direction]
return Stan(self.row, self.col, new_direction)
2024-04-14 14:37:32 +02:00
2024-05-18 17:10:56 +02:00
return None
2024-04-14 14:37:32 +02:00
class Odwiedzone:
def __init__(self):
self.stany = []
def check(self, stan):
for i in self.stany:
if(stan.direction == i.direction and stan.col == i.col and stan.row == i.row):
return True
return False
def dodaj_stan(self, stan):
self.stany.append(stan)