数组路径中的 NPC

发布于 2024-12-17 20:13:13 字数 2330 浏览 0 评论 0原文

我要创建一个在终端中工作的游戏(无 GUI),我可以使用按键在整个数组中移动我的角色,现在我需要创建将在数组的 12,12 点生成的 NPC(猎人),并使用随机预定义路径移动到 0,0(必须使用 12x12 数组中的所有路径)有人可以对此进行一些说明吗?

package hunters;
import java.io.*;
import java.util.*; 
import java.awt.*;
import javax.swing.*;

public class Hunters {
    private static int score;
    private static String player = "P";
    private static String move;
    private static   String emptyfield = "X";
    private static   String [][]a2 = new String [12][12];
    private static int pr,pc;
    private static String hunter = "H";
    private static int hr=11,hc=11;

    public static void paint_board(){

        for (int r = 0 ; r < a2.length; r++){
            for (int c= 0; c <a2[r].length; c++){
                a2 [r][c] = emptyfield;
                a2[pr][pc] = player;
                a2[hr][hc]= hunter;
                System.out.print(" "+a2[r][c]);
            }
            System.out.println(""); 
        }

    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        score = 0;

        paint_board();
        do{
            ystem.out.println("Input your move");
            move = in.nextLine();
            if (move.equalsIgnoreCase("w")){
             //move up
               pr = pr -1;
                //repaint
               paint_board();
                //check for collision
                //check for health

            }else if(move.equalsIgnoreCase("s")){
                //move down
                pr = pr +1;
                 for (int i = 0; i <20; i++) System.out.println();
                //repaint
                paint_board();
                  //check for collision
                //check for health

            }else if(move.equalsIgnoreCase("d")){
                //move right
                pc = pc +1;
                //repaint
                paint_board();
                  //check for collision
                //check for health

            }else if(move.equalsIgnoreCase("a")){
                //move left
                pc = pc -1;
               for (int i = 0; i < 20; i++) System.out.println("");
                //repaint
                paint_board();
                  //check for collision
                //check for health

            }
        }while(score !=5);
    }   
}  

I am to create a game to work in the terminal(no GUI), I am able to move my character throughout the array using keys, now I need to create NPCs(Hunters) that will spawn at 12,12 point of the array, and move to 0,0 with a random pre-defined path ( it is essential that all paths within 12x12 array are used) Could anyone flash some light on this please?

package hunters;
import java.io.*;
import java.util.*; 
import java.awt.*;
import javax.swing.*;

public class Hunters {
    private static int score;
    private static String player = "P";
    private static String move;
    private static   String emptyfield = "X";
    private static   String [][]a2 = new String [12][12];
    private static int pr,pc;
    private static String hunter = "H";
    private static int hr=11,hc=11;

    public static void paint_board(){

        for (int r = 0 ; r < a2.length; r++){
            for (int c= 0; c <a2[r].length; c++){
                a2 [r][c] = emptyfield;
                a2[pr][pc] = player;
                a2[hr][hc]= hunter;
                System.out.print(" "+a2[r][c]);
            }
            System.out.println(""); 
        }

    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        score = 0;

        paint_board();
        do{
            ystem.out.println("Input your move");
            move = in.nextLine();
            if (move.equalsIgnoreCase("w")){
             //move up
               pr = pr -1;
                //repaint
               paint_board();
                //check for collision
                //check for health

            }else if(move.equalsIgnoreCase("s")){
                //move down
                pr = pr +1;
                 for (int i = 0; i <20; i++) System.out.println();
                //repaint
                paint_board();
                  //check for collision
                //check for health

            }else if(move.equalsIgnoreCase("d")){
                //move right
                pc = pc +1;
                //repaint
                paint_board();
                  //check for collision
                //check for health

            }else if(move.equalsIgnoreCase("a")){
                //move left
                pc = pc -1;
               for (int i = 0; i < 20; i++) System.out.println("");
                //repaint
                paint_board();
                  //check for collision
                //check for health

            }
        }while(score !=5);
    }   
}  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单爱 2024-12-24 20:13:13

只需使用广度优先搜索(BFS)即可。

这将为您提供一张地图,显示从 (12,12) 到达地图上的每个自由字段至少需要多少步。

然后,你可以让你的 NPC 根据随机决策,从 (0,0) 开始,朝着数字最大且小于他们当前所在区域的区域之一的方向向后移动。 。

这样,你就会得到最短的路径之一,然后你只需要让你的 NPC 沿着这条路径行走即可。

然后,如果您想要更多不可预测的行为,您可以添加进一步的随机化,使它们现在绕道而行,即让它们有时根据随机决策选择非最佳的下一个字段。

然后花一些时间在众多游戏开发博客之一上 - 即谷

歌游戏开发广度优先

游戏开发a-star

(a-star是一种更复杂的路径查找算法)

这样你应该能够学习关于这些事情的很多内容 - 如果没有必要的数学背景,维基百科文章有时很难理解,因此我建议寻找游戏开发的特定内容。

Simply use breadth-first search (BFS).

This gives you a map showing how many steps it takes at least to get to every free field on your map from (12,12).

Then you can make your NPCs move backward field by field backward from (0,0) in the direction of one of the fields with the greatest number that is smaller than that of the field they're currently standing on, based on a random desicion.

This way, you will get one of the shortest paths, which you then just have to let your NPCs walk along.

Then you can add further randomization to make them take a detour now and then if you want more unpredictable behavior - i.e. make them sometimes choose a non-optimal next field, based on a random decision.

And then go and spend some time on one of the many game development blogs out there - i.e. google

game development breadth-first

game development a-star

(a-star is a more sophsticated path-finding algorithm)

This way you should be able learn a lot about these things - wikipedia articles are sometimes rather difficult to understand without the necessary mathematical background, hence my suggestion of looking for game development specific content.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文