#graph #graphics #mazes #gamedev

bin+lib maze_rs

一个简单的Rust迷宫游戏

1 个稳定版本

1.0.0 2021年6月28日

#585游戏开发

自定义许可证

52KB
901

迷宫项目

目标是生成完美的迷宫——迷宫对于迷宫中的任何一对单元格都是可解的,并且具有该对单元格存在唯一解的附加属性。此外,解决给定场景的迷宫

  • 绘制迷宫从左上角到底右角的路径。

  • 发现迷宫中的最长路径。

  • 发现迷宫中最复杂的路径。

项目将为每次程序运行生成随机形状的迷宫(在合理范围内)。

有关项目的结构设计,请参阅设计文档

项目设置

此项目是用Rust编写的(版本:rustc 1.28.0 (9634041f0 2018-07-30))。Rust使用cargo作为构建/依赖关系管理器,并使用rustup作为安装Rust工具链的工具。

要设置Rust,请按照以下步骤操作

  1. 访问rustup.rs网站,使用网站上显示的命令安装最新的rustupcurl https://sh.rustup.rs -sSf | sh

  2. 按照默认选项应该会安装工具链管理工具rustup。请注意,您可以从多个预设的工具链中进行选择——stablenightly以及一些其他选项。请选择stablenightly也应该可以正常工作)。

  3. 验证安装是否成功

      $ cargo --version
      cargo 1.28.0 (96a2c7d16 2018-07-13)
    

构建项目

注意:此项目应与所有Rust版本兼容——20152018

从项目根目录运行以下命令: cargo clean && cargo build --release,如下所示

$ cargo clean && cargo build --release
   Compiling libc v0.2.43
   Compiling rand_core v0.2.1
   Compiling rand v0.5.5
   Compiling maze_project v0.1.0 (file:///Users/z0ltan/Code/Projects/games/maze_rs)
    Finished release [optimized] target(s) in 4.59s

为项目生成文档

Rust自带文档工具(类似于javadoc),内置在软件中。要生成项目文档,请运行以下命令:cargo doc --no-deps --open,如下所示:

$ cargo doc --no-deps --open
    Checking rand_core v0.2.1
    Checking libc v0.2.43
    Checking rand v0.5.5
 Documenting maze_project v0.1.0 (file:///Users/z0ltan/Code/Projects/games//maze_rs)
    Finished dev [unoptimized + debuginfo] target(s) in 4.35s
     Opening /Users/z0ltan/Code/Projects/games/maze_rs/target/doc/maze_rs/index.html
   Launching open

这应该会在您的默认浏览器中打开生成的HTML页面。

运行项目

要运行项目,请使用cargo run命令并传递任何参数,例如:

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
     Running `target/debug/maze_project`
Usage: cargo run HEIGHT WIDTH

运行测试

Rust自带内置的测试套件,使用cargo test命令应该运行项目中的所有测试。

$ cargo test
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
     Running target/debug/deps/maze_project-c360a1a0a2cd554c

running 16 tests
test ds::tests::test_maze_data ... ok
test ds::tests::test_point ... ok
test ds::tests::teste_maze_data_sanity ... ok
test helper::tests::test_char_for_direction_east ... ok
test helper::tests::test_char_for_direction_north ... ok
test helper::tests::test_char_for_direction_southt ... ok
test ds::graphs::test::test_add_edge_panic ... ok
test ds::graphs::test::test_get_adjancent_vertices_panic ... ok
test ds::graphs::test::test_get_spanning_tree_panic ... ok
test helper::tests::test_char_for_direction_west ... ok
test helper::tests::test_get_direction_east ... ok
test helper::tests::test_get_direction_north ... ok
test helper::tests::test_get_direction_south ... ok
test helper::tests::test_get_direction_west ... ok
test ds::graphs::test::test_spanning_tree_params ... ok
test ds::graphs::test::test_generate_spanning_tree ... ok

test result: ok. 16 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/debug/deps/maze_project-c14ba56a651feb63

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests maze_project

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

示例运行

只需用网格的期望尺寸运行项目。下面是一个示例运行:

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
     Running `target/debug/maze_project`
Usage: cargo run HEIGHT WIDTH


$ cargo run 15 20
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
     Running `target/debug/maze_project 15 20`


   +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
   | s                             |                           |   |   |   |   |   |
   +   +   +   +---+   +   +---+---+   +---+   +---+---+   +---+   +   +   +   +   +
   | v |   |       |   |           |       |           |                           |
   +   +---+---+---+---+   +   +   +   +   +---+---+   +---+---+---+---+---+---+---+
   | v     |       |   |   |   |       |           |               |       |       |
   +   +   +   +   +   +---+   +---+   +---+---+---+   +---+---+---+---+   +   +   +
   | v |       |           |   |   |               |   |   |       |   |   |   |   |
   +   +---+---+---+---+---+   +   +---+---+---+---+---+   +---+   +   +   +   +---+
   | >   v             |   |   |                       |       |   |   |           |
   +   +   +   +   +---+   +---+   +   +---+---+---+---+   +---+   +   +   +   +---+
   |   | v |   |                   |               |       |   |           |       |
   +   +   +   +---+   +   +   +---+   +   +   +---+---+   +   +   +   +---+---+   +
   |   | v |   |       |   |       |   |   |       |   |       |   |       |       |
   +   +   +---+---+---+   +---+   +   +---+---+---+   +   +---+   +---+---+---+   +
   |   | v             |       |   |   |           |               |   |   |   |   |
   +---+   +---+   +---+---+---+---+---+---+   +---+---+   +   +---+   +   +   +---+
   |     v     |                       |       |   |       |   |                   |
   +   +   +---+---+   +   +---+---+---+   +---+   +   +   +---+   +---+---+---+   +
   |   | v         |   |           |       |   |       |           |           |   |
   +   +   +---+---+   +---+---+---+---+   +   +   +   +---+---+---+   +---+   +---+
   |   | >   v     |           |   |       |   |   |       |               |   |   |
   +---+   +   +---+---+---+---+   +---+   +   +   +---+---+   +   +---+---+---+   +
   |       | >   v         |   |       |   |       |   |       |                   |
   +   +---+   +   +---+---+   +   +---+   +   +---+   +---+   +---+---+---+---+---+
   |   |       | >   >   v     |           |       |   |       | >   >   >   >   v |
   +---+   +   +   +---+   +---+   +---+---+   +---+   +   +---+   +---+---+---+   +
   |       |   |   |   | >   >   >   >   >   >   >   >   >   >   ^             | v |
   +   +---+   +   +   +---+   +   +---+   +---+   +---+   +   +---+---+   +---+   +
   |       |   |           |   |   |           |       |   |           |       | t |
   +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

Enter choice (1 - solve, 2 - longest path, 3 - quit)...
1

   +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
   | s                             |                           |   |   |   |   |   |
   +   +   +   +---+   +   +---+---+   +---+   +---+---+   +---+   +   +   +   +   +
   | v |   |       |   |           |       |           |                           |
   +   +---+---+---+---+   +   +   +   +   +---+---+   +---+---+---+---+---+---+---+
   | v     |       |   |   |   |       |           |               |       | >   v |
   +   +   +   +   +   +---+   +---+   +---+---+---+   +---+---+---+---+   +   +   +
   | v |       |           |   |   |               |   |   |       |   |   | ^ | t |
   +   +---+---+---+---+---+   +   +---+---+---+---+---+   +---+   +   +   +   +---+
   | >   v             |   |   |                       |       |   |   | >   ^     |
   +   +   +   +   +---+   +---+   +   +---+---+---+---+   +---+   +   +   +   +---+
   |   | v |   |                   |               |       |   | >   >   ^ |       |
   +   +   +   +---+   +   +   +---+   +   +   +---+---+   +   +   +   +---+---+   +
   |   | v |   |       |   |       |   |   |       |   |       | ^ |       |       |
   +   +   +---+---+---+   +---+   +   +---+---+---+   +   +---+   +---+---+---+   +
   |   | v             |       |   |   |           |     >   >   ^ |   |   |   |   |
   +---+   +---+   +---+---+---+---+---+---+   +---+---+   +   +---+   +   +   +---+
   |     v     |                       |       |   | >   ^ |   |                   |
   +   +   +---+---+   +   +---+---+---+   +---+   +   +   +---+   +---+---+---+   +
   |   | v         |   |           |       |   | >   ^ |           |           |   |
   +   +   +---+---+   +---+---+---+---+   +   +   +   +---+---+---+   +---+   +---+
   |   | >   v     |           |   |       |   | ^ |       |               |   |   |
   +---+   +   +---+---+---+---+   +---+   +   +   +---+---+   +   +---+---+---+   +
   |       | >   v         |   |       |   | >   ^ |   |       |                   |
   +   +---+   +   +---+---+   +   +---+   +   +---+   +---+   +---+---+---+---+---+
   |   |       | >   >   v     |           | ^     |   |       |                   |
   +---+   +   +   +---+   +---+   +---+---+   +---+   +   +---+   +---+---+---+   +
   |       |   |   |   | >   >   >   >   >   ^                                 |   |
   +   +---+   +   +   +---+   +   +---+   +---+   +---+   +   +---+---+   +---+   +
   |       |   |           |   |   |           |       |   |           |       |   |
   +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

Enter choice (1 - solve, 2 - longest path, 3 - quit)...
2

限制和可能的改进

  • 使用ANSI转义码进行迷宫渲染,因此滚动是一个问题。建议的限制是25 x 25的迷宫,但在更大屏幕和更高分辨率和配置的情况下,应该可以更多或更少。代码不强制执行任何限制,除了最小1 x 1的迷宫大小。

  • 再次,由于使用了ANSI转义码,代码应在任何符合ANSI规范的终端上运行良好,这意味着排除了基本的Windows命令行。

  • 大多数复杂的路径应该相对容易实现,计划的方法已在支持的设计文档中提及。

  • 所有可调整的配置(动画速度、字体颜色、单元格精灵等)应理想地放置在外部配置文件中。

许可

请参阅LICENCE文件中的许可文件。

依赖项

~315KB