理解*用于Java /处理

时间:2016-01-15 22:52:35

标签: java algorithm processing

我正在尝试学习a *算法,Dijkstra也是。昨天我找到了一个处理的工作示例,我认为很容易编码。我查看了项目的内容,但我没有看到任何方法来获取步骤或保存到阵列中的目标的方式... 所以最后是我的问题:

  1. 要将这样的寻路系统实现为像pacman这样的游戏,需要有一个数组,其中的步骤或方式会被保存吗?

  2. 因为我还不知道如何使用寻路......敌人(幽灵)怎么可能通过查看阵列移动到目标?

  3. 如果有人想看到算法的代码:

    int[][] findPath () { int[][]done=asArray(); done[fR][fC] = 0;int counter = 0;while (true) { boolean foundOne = false; for (int i = 0; i < copyArray.length; i++) { for (int j = 0; j < copyArray[0].length; j++) { if (done[i][j] == counter) { foundOne = true; if (isValid(done, i-1, j, counter+1)) done[i-1][j] = counter + 1; if (isValid(done, i+1, j, counter+1)) done[i+1][j] = counter + 1; if (isValid(done, i, j-1, counter+1)) done[i][j-1] = counter + 1; if (isValid(done, i, j+1, counter+1)) done[i][j+1] = counter + 1; } } } counter ++; if (!foundOne) break; } for (int i = 0; i < copyArray.length; i++) { for (int j = 0; j < copyArray[0].length; j++) { //print ( done[i][j] + "\t" ); } println (); } return done; } ArrayList getPath(int[][] flood) { if (flood[sR][sC] == -1) return null; ArrayList path = new ArrayList(); int cR = sR, cC = sC; while (true) { if (cR == fR && cC == fC) { return path; } if (isValid(flood, cR-1, cC)) { if (flood[cR-1][cC] < flood[cR][cC]) { cR = cR-1; cC = cC; PVector spot = new PVector (cR, cC); path.add (spot); continue; } } if (isValid(flood, cR+1, cC)) { if (flood[cR+1][cC] < flood[cR][cC]) { cR = cR+1; cC = cC; PVector spot = new PVector (cR, cC); path.add (spot); continue; } } if (isValid(flood, cR, cC-1)) { if (flood[cR][cC-1] < flood[cR][cC]) { cR = cR; cC = cC-1; PVector spot = new PVector (cR, cC); path.add (spot); continue; } } if (isValid(flood, cR, cC+1)) { if (flood[cR][cC+1] < flood[cR][cC]) { cR = cR; cC = cC+1; PVector spot = new PVector (cR, cC); path.add (spot); continue; } } } } boolean isValid (int[][] arr, int r, int c, int count) { if (r < 0 || r >= rows) return false; if (c < 0 || c >= cols) return false; if (arr[r][c] == -2) return false; if (arr[r][c] <= count && arr[r][c] != -1) return false; return true; } boolean isValid (int[][] arr, int r, int c) { if (r < 0 || r >= rows) return false; if (c < 0 || c >= cols) return false; if (arr[r][c] == -2) return false; return true; } int[][] asArray () { int[][] res = new int[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (maze[i][j].type != 1) res[i][j] = -1; else res[i][j] = -2; } } return res; } int maxValIn2D (int[][] look) { int mx = 0; for (int i = 0; i < look.length; i++) { for (int j = 0; j < look[i].length; j++) { mx = look[i][j] > mx ? look[i][j] : mx; } } return mx; } int wrapAround (int low, int high, int data) { return (data >= low && data <= high) ? data : (data < low ? high - (low - data - 1) : low + (data - high - 1)); //LOL } 
    

    主档案:

    int[][] testArray2 = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};int[][] copyArray = new int[testArray2.length][testArray2[0].length];Tile[][] maze; int rows; int cols; int lastMilli = 0; int[][] mapData; ArrayList path; int state; boolean found; boolean isNeeded = false; int sR, sC, fR, fC; void setup() { size (600, 600); background (255); arrayCopy(testArray2,copyArray); path = new ArrayList<Integer>(); maze = new Tile[copyArray.length][copyArray[0].length]; for (int i = 0; i < copyArray.length; i++) { for (int j = 0; j < copyArray[0].length; j++) { // was bedeuted 0 ?? if(copyArray[i][j] == 0 || copyArray[i][j] == 2) { maze[i][j] = new Tile(i, j, 0); } if(copyArray[i][j] == 1 ) { maze[i][j] = new Tile(i, j, 1); } } rows = copyArray.length; cols = copyArray[0].length; } state = 0; // sind die koordinaten des startes, im array sR = 4; sC = 5; // sind die koordinaten des zieles, im array fR = 8; fC = 8; found = true; noStroke(); } void draw() { background (255); if (!found && sR != -1 && sC != -1 && fR != 1 && fC != -1) { found = true; mapData = findPath(); path = getPath(mapData); } noStroke (); // mahlt nur das array for (int i = 0; i < copyArray.length; i++) { for (int j = 0; j < copyArray[0].length; j++) { switch(maze[i][j].type) { case 0:if (mapData != null) { if (mapData[i][j]==-1) { fill (255); } else { colorMode (HSB); fill (160, map(mapData[i][j], 0, maxValIn2D(mapData), 0, 255), 255); colorMode (RGB); } } else { fill (255); } break; case 1: fill (0); break; case 2: fill (0, 255, 0); break; case 3: fill (255, 0, 0); break; case 4: fill (64, 64, 255); break; } if (path != null) { for (Object o : path) { PVector p = (PVector)o; // zeigt den gelben weg an if (p.x == i && p.y == j && !(p.x == fR && p.y == fC)) { fill (255, 255, 0); println(p.x+" "+p.y); } } } rect ((width/cols) * j, (height/rows) * i, width/cols, height/rows); } } switch (state) { case 0: stroke (128); break; case 1: stroke (0, 255, 0); break; case 2: stroke (255, 0, 0); break; } noFill (); int row = (int) map (mouseY, 0, height, 0, rows); int col = (int) map (mouseX, 0, width, 0, cols); rect ((width/cols) * col, (height/rows) * row, width/cols, height/rows); maze[sR][sC].type = 2; maze[fR][fC].type = 3; milli(2500); found = false; for(int i = 0; i< path.size();i++) { print(path.get(i)); } } void milli(int intervall) { if(millis()>lastMilli+intervall)//been at least 200 millis { lastMilli=millis(); //wpdate the last time we did anything //println("Millis is Running"); } }
    

    我知道也许很多,而且有两个问题。

1 个答案:

答案 0 :(得分:0)

你正在努力,我为此鼓掌。

但是你有点倒退:你不会先通过查看代码来理解算法。您可以从简单的旧英语开始理解算法,最后步骤是查看(或写入)代码。

如果你想了解这些算法,你需要做的第一件事是阅读它们 - 维基百科是一个很好的起点。如果您认为自己已经理解了自己所阅读的内容,那么下一步就是将用自己的话语写出来作为一系列步骤。用英语做这个 - 不是用代码做的;甚至没有伪代码。

假装你有一个非常愚蠢的朋友,他不知道A *或Dijkstra是什么。写下您的朋友可以遵循的步骤列表,以完成其中一种算法。记住你的朋友是多么愚蠢,所以要确保每一步都尽可能小!只要有可能,就可以将这些步骤分解为更小的子步骤。

说真的,在现实生活中这样做。使用一些方格纸绘制迷宫,并将该迷宫与您已写出的步骤列表一起提供给其他人。让他们按照你的步骤走出迷宫。

如果您已经写出了这些步骤,那么您可以开始考虑转换为代码。

如果您遇到特定步骤,那就是Stack Overflow的用途。但是现在你的问题仍然太宽泛,无法给你一个具体的答案。