博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A星之地形成本的实现
阅读量:6871 次
发布时间:2019-06-26

本文共 2637 字,大约阅读时间需要 8 分钟。

hot3.png

昨天提交了一点点的改动,是关于地形成本的。

 

AStarConstants.java追加对于地形的定义。

 

--- a/src/java/astar/AStarConstants.java

+++ b/src/java/astar/AStarConstants.java

@@ -8,10 +8,15 @@ package astar;

  * DBJ(dubenju@126.com)

  */

 public class AStarConstants {

+            public static int COST_NONE = 0;

     /** 正交移动一格的路径分值 */

     public static int COST_ORTHOGONAL = 10;

     /** 对角线移动一格的路径分值 */

     public static int COST_DIAGONAL = 14;

+    public static int COST_GRASS = 12; // 草地

+    public static int COST_HILL = 20; // 丘陵

+    public static int COST_SWAMP = 30; // 沼泽

+    public static int COST_RIVER = 40; // 河流

 

在地形类Terrain中,追加对地形的考虑。

 

--- a/src/java/astar/Terrain.java

+++ b/src/java/astar/Terrain.java

@@ -10,6 +10,7 @@ public class Terrain {

 

     private int val;

     private int walkable;

+    private int cost;

 

     /**

      * 构造函数

@@ -17,19 +18,54 @@ public class Terrain {

     public Terrain(int val) {

         this.val = val;

         if (this.val == 0) {

+                    // 0:unwalkable

             this.walkable = AStarConstants.NOTE_UNWALKABLE;

+            this.cost = AStarConstants.COST_NONE;

         }

         if (this.val == 1) {

+                    // 1:walkbale,ground

             this.walkable = AStarConstants.NOTE_WALKABLE;

+            this.cost = AStarConstants.COST_NONE;

         }

         if (this.val == 2) {

+                    // 2:

             this.walkable = AStarConstants.NOTE_UNWALKABLE;

+            this.cost = AStarConstants.COST_NONE;

         }

         if (this.val == 3) {

+                    // 3:侧壁

             this.walkable = AStarConstants.NOTE_UNWALKABLE;

+            this.cost = AStarConstants.COST_NONE;

         }

         if (this.val == 4) {

+                    // 4:target:目标

             this.walkable = AStarConstants.NOTE_WALKABLE;

+            this.cost = AStarConstants.COST_NONE;

+        }

+        if (this.val == 5) {

+                    // 5:grass:草地

+            this.walkable = AStarConstants.NOTE_WALKABLE;

+            this.cost = AStarConstants.COST_GRASS;

+        }

+        if (this.val == 6) {

+                    // 6:hill:丘陵

+            this.walkable = AStarConstants.NOTE_WALKABLE;

+            this.cost = AStarConstants.COST_HILL;

+        }

+        if (this.val == 7) {

+                    // 7:swamp:沼泽

+            this.walkable = AStarConstants.NOTE_WALKABLE;

+            this.cost = AStarConstants.COST_SWAMP;

+        }

+        if (this.val == 8) {

+                    //  8:river:河流

+            this.walkable = AStarConstants.NOTE_WALKABLE;

+            this.cost = AStarConstants.COST_RIVER;

+        }

+        if (this.val == 9) {

+                    // 9:bridge:桥

+            this.walkable = AStarConstants.NOTE_WALKABLE;

+            this.cost = AStarConstants.COST_NONE;

         }

     }

 

@@ -60,4 +96,18 @@ public class Terrain {

     public void setWalkable(int walkable) {

         this.walkable = walkable;

     }

+

+            /**

+             * cost

+             */

+            public int getCost() {

+                          return cost;

+            }

+

+            /**

+             * cost

+             */

+            public void setCost(int cost) {

+                          this.cost = cost;

+            }

 

AStar中计算成本时考虑地形的成本。

--- a/src/java/astar/AStar.java

+++ b/src/java/astar/AStar.java

            node.setG(parent.getG() + step);

                      // 考虑地形的成本

            node.setG(parent.getG() + step + node.getTerrain().getCost());

 

测试程序结果图:

052003_c2PG_660460.jpg

 

关于A*请参照。

转载于:https://my.oschina.net/dubenju/blog/472730

你可能感兴趣的文章
实用JVM参数总结
查看>>
oracle 11g R2 64位 安装详细步骤
查看>>
Jpeg 库的解码OpenCL优化
查看>>
码易应用商城入驻流程
查看>>
正则表达式
查看>>
『中级篇』docker之虚拟机创建vagrant技巧(番外篇)(81)
查看>>
交换机SPAN功能配置
查看>>
MySQL 架构组成—存储引擎
查看>>
基于数值分析思想对多项式求值的原理和应用进行探究
查看>>
vue-devtools vue开发调试神器
查看>>
PHP扩展模块的安装
查看>>
BGP基础操作
查看>>
selenium系列->Actions命令实例整理->goBack()
查看>>
CentOS 7上构建squid传统代理,透明代理(squid3.4.6)
查看>>
SimpleXml项目
查看>>
php下使用PDO创建sqlite3数据库
查看>>
Istio技术与实践6:Istio如何为服务提供安全防护能力
查看>>
大型架构及配置技术Docker
查看>>
ISTP的重要作用
查看>>
GitHub---(使用SSH方式)
查看>>