This commit is contained in:
gfuse
2024-01-15 18:28:14 +01:00
parent bdcae8feae
commit 0567e9d3d6
7 changed files with 119 additions and 0 deletions
View File
+38
View File
@@ -0,0 +1,38 @@
class Cell:
def __init__(self, pose: tuple = (0, 0), parent = None) -> None:
self.move = [(1, 2), (1, -2), (-1, 2), (-1, -2), (2, 1), (2, -1), (-2, 1), (-2, -1)]
self.pose = pose
self.parent = parent
self.childs = []
self.find = False
def get_path(self) -> list:
path = []
current = self
while current is not None:
path.append(current.pose)
current = current.parent
return path[::-1]
def __eq__(self, other) -> bool:
return isinstance(other, Cell) and self.pose == other.pose
def make_moves(self, destination, paths) -> None:
if isinstance(destination, Cell):
if len(self.childs) < 1:
for move in self.move:
x = self.pose[0] + move[0]
y = self.pose[1] + move[1]
if 0 <= x < 8 and 0 <= y < 8:
new_cell = Cell(pose=(x, y), parent=self)
if destination == new_cell:
self.find = True
paths.append(new_cell.get_path())
else:
self.childs.append(new_cell)
if self.find:
self.childs.clear()
self.parent.childs.remove(self)
else:
for child in self.childs:
child.make_moves(destination, paths)
+17
View File
@@ -0,0 +1,17 @@
from knight.player import Player
import argparse
def main():
parser = argparse.ArgumentParser(
prog='Knight',
description='',
epilog='')
parser.add_argument('-x', '--x_pose', help='Destination X between 0 and 8', type=int, required=True)
parser.add_argument('-y', '--y_pose', help='Destination Y between 0 and 8', type=int, required=True)
parser.add_argument('-n', '--image_name', help='Destination Y between 0 and 8', type=str, required=True)
args = parser.parse_args()
player = Player()
player.find_paths(destination=(args.x_pose,args.y_pose))
player.plot_paths(name=args.image_name)
if __name__ == '__main__':
main()
+29
View File
@@ -0,0 +1,29 @@
from .cell import Cell
import logging
from graphviz import Digraph, render
class Player(Cell):
def __init__(self) -> None:
super().__init__()
self.destination = None
self.paths = []
self.depth = 0
def plot_paths(self, name:str = "shortest_paths") -> None:
dot = Digraph(comment='Paths')
for path in self.paths:
for i in range(len(path) - 1):
dot.edge(str(path[i]), str(path[i + 1]))
dot.save(f'{name}.dot')
render('dot', 'png', f'{name}.dot')
def find_paths(self, destination: tuple = (0, 0)):
self.destination = Cell(pose=destination)
if self.destination.pose[0] < 0 or self.destination.pose[1] > 8 or self.destination.pose[0] < 0 or self.destination.pose[1] > 8:
logging.error("Invalid destination position: it should be between 0 and 8")
return
while len(self.paths) < 1:
self.depth += 1
self.make_moves(self.destination, self.paths)