目录

MoonPathfinding

A comprehensive pathfinding library for MoonBit, featuring 9 algorithms, 5 graph types, maze generators, and ASCII/HTML visualization.

CI

Features

Algorithms (9)

Algorithm Type Optimal Weighted Notes
BFS Uninformed Yes (unweighted) No Queue-based
DFS Uninformed No No Stack-based
Dijkstra Uninformed Yes Yes Priority queue
A* Informed Yes Yes f = g + h
Greedy BFS Informed No Yes f = h only
Bidirectional BFS Uninformed Yes (unweighted) No Two-end search
Bellman-Ford Uninformed Yes Yes (neg. OK) DP-based
IDA* Informed Yes Yes Memory-efficient
JPS Informed Yes Uniform grid Jump point pruning

Graph Types (5)

  • AdjacencyList — General-purpose directed/undirected graph
  • Grid — 2D grid with 4-direction movement
  • WeightedGrid — Grid with per-cell terrain costs and 8-direction support
  • HexGrid — Hexagonal grid (odd-q offset layout)
  • Graph trait — Implement your own graph types

Maze Generators (3)

  • DFS recursive backtracker
  • Prim’s algorithm
  • Random obstacle placement

Visualization

  • ASCII — Terminal-friendly path rendering
  • HTML — CSS-styled grid table export

Installation

moon add hzc666/moonpathfinding

Quick Start

// Grid pathfinding with A*
let lines = [
  "S....",
  ".###.",
  ".....",
  ".###.",
  "....E",
]
let grid = @graph.Grid::from_strings(lines).unwrap()
let start = grid.pos_to_id(0, 0)  // 'S' position
let goal = grid.pos_to_id(4, 4)   // 'E' position

let result = @algo.astar(grid, start, goal)
println(@visualize.ascii_render(grid, result, start, goal))
// S····
// *###·
// *****
// ·###*
// ····E
// General graph
let g = @graph.AdjacencyList::new(4)
g.add_edge_undirected(0, 1, 5)
g.add_edge_undirected(1, 2, 3)
g.add_edge_undirected(2, 3, 1)

let r = @algo.dijkstra(g, 0, 3)
// r.cost = 9, r.path = [0, 1, 2, 3]
// Generate and solve a maze
let maze = @maze.dfs_maze(15, 15, 42)
let grid = @graph.Grid::from_strings(maze).unwrap()
let r = @algo.astar(grid, 0, grid.width * grid.height - 1)
println(@visualize.ascii_render(grid, r, 0, grid.width * grid.height - 1))
// Compare all algorithms
let report = @bench.maze_benchmark()
println(report)

Project Structure

moonpathfinding/
├── graph/       # Graph trait, AdjacencyList, Grid, WeightedGrid, HexGrid
├── algo/        # 9 algorithms + MinHeap
├── visualize/   # ASCII and HTML renderers
├── maze/        # Maze generators (DFS, Prim, random)
├── bench/       # Performance benchmarks
├── cmd/main/    # CLI demo (moon run cmd/main)
└── docs/        # Competition documents

API Overview

Algorithms

@algo.bfs(graph, start, goal)              -> PathResult
@algo.dfs(graph, start, goal)              -> PathResult
@algo.dijkstra(graph, start, goal)         -> PathResult
@algo.astar(graph, start, goal)            -> PathResult
@algo.greedy_bfs(graph, start, goal)       -> PathResult
@algo.bidirectional_bfs(graph, start, goal)-> PathResult
@algo.bellman_ford(graph, start, goal)     -> PathResult
@algo.idastar(graph, start, goal)          -> PathResult
@algo.jps(grid, start, goal)              -> PathResult

Graph Trait

pub trait Graph {
  node_count(Self) -> Int
  neighbors(Self, Int) -> Array[(Int, Int)]   // (node_id, edge_cost)
  heuristic(Self, Int, Int) -> Int             // estimated cost
}

PathResult

pub(all) struct PathResult {
  path : Array[Int]    // ordered node IDs
  cost : Int           // total path cost
  nodes_visited : Int  // exploration count
}

Visualization

@visualize.ascii_render(grid, result, start, goal) -> String
@visualize.html_render(grid, result, start, goal, title) -> String
@visualize.result_summary(algo_name, result) -> String

Run CLI Demo

moon run cmd/main

Run Tests

moon test

License

MIT

Links

关于

纯 MoonBit 实现的路径查找算法库,提供 9 种算法(BFS/DFS/Dijkstra/A*/Greedy BFS/Bidirectional BFS/Bellman-Ford/IDA*/JPS)、5 种图类型(图/网格/加权网格/六边形网格/Trait 接口)、3 种迷宫生成器和 ASCII/HTML 双模式可视化。零外部依赖,基于 Graph trait 泛型设计。

537.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802047560号