473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

I Double Dare you: Write a shortest path process for a grid-layout game if you can.

SwissProgrammer
220 128KB
C and/or C++

Not Visual Basic. Not Visual C++. Not .NET . Just C or C++.

Let me see some real C or C++ code that does this.

{please and thank you if you feel like it}

And not some text based CLI output that pretends to work and looks like it might work and maybe works and bla bla bla, but does NOT work for a grid-based game.

Here are some grid background examples [X], [X]

Prove to me that you can write one in C or C++ that does what is shown on this [X] page. That is an animated gif on that page. That is NOT a working C or C++ example on that page. But you get the idea.




I have been studying some current and past games and doing some testing of them to see how they got this working.

SURPRISE ! I did NOT find ANY game that had a shortest path correctly working. They got close, but with testing I found errors in shortest paths.



I have found many examples of how to write a shortest path algorithm. NONE of those (NOT EVEN ONE!) of those tested out to be useful in a simply structured grid-layout game. A grid-layout game is about as simple as it gets for testing a shortest path algorithm and this did not work for it. Thus, if Dijkstra’s algorithm worked for Dijkstra that does not mean that it will work for anyone else that has a computer generated game that uses a grid layout.

If YOU have a shortest path process that works for a grid-layout type of game then please post it here on bytes.com as an article.

Or if you need a challenge, write one of these yourself. I dare you to try. Not just some pseudo-code. Some actual working code. I Double Dare you.

Thank you.
Mar 26 '21 #1
8 5207
Rabbit
12,516 Expert Mod 8TB
I don't program in C or C++ but it sounds like you can find working code for a graph but not one for a grid layout. A grid can be represented as a graph with edges to all orthogonal cells. So you could insert a step to convert the grid to a graph and then run the graph version of dijkstra's.
Mar 28 '21 #2
SwissProgrammer
220 128KB
Rabbit,

I tried that. Every example that I found and every way that I tried did not work for a grid (or graph) layout such as [X].

I have been struggling with this for over 6 months. Not all day every day but often. I work at it until I give up, then try again later. I tried pseudo code. I tried writing it out on paper each step and then tried to code that in. I have posted here on bytes.com some of my attempts that seemed to get close. None of these has worked so far. It seems to be a matter of logic converted to code.

The process should be simply an if then else process.

Try going to one grid square at a time. Add that result, if not blocked like at the edge or similar (with length or time) to a vector for comparison later. I know that. At this point, I can't seem to even code that in.

I can do it, but I can not do it.

It has to be basic simple logic. It can NOT be this difficult.
Mar 29 '21 #3
Rabbit
12,516 Expert Mod 8TB
Well, it's not C, but I threw this together in javascript of you want to take a look. It just outputs to the console so you'll have to open up your browser's console or the console that the site provides.

https://jsfiddle.net/thru41wj/

Expand|Select|Wrap|Line Numbers
  1. class PriorityQueue {
  2.   constructor() {
  3.     this.collection = [];
  4.   }
  5.  
  6.   enqueue(element, priority){
  7.     let added = false;
  8.  
  9.     for (let i = 0; i < this.collection.length; i++){
  10.       if (priority < this.collection[i][1]) {
  11.         this.collection.splice(i, 0, [element, priority]);
  12.         added = true;
  13.         break;
  14.       }
  15.     }
  16.  
  17.     if (!added) {
  18.       this.collection.push([element, priority]);
  19.     }
  20.   }
  21.  
  22.   dequeue() {
  23.     return this.collection.shift();
  24.   }
  25.  
  26.   size() {
  27.     return this.collection.length;
  28.   }
  29. }
  30.  
  31.  
  32.  
  33. const grid = [
  34.   [1        , 1         , 5         , 2         , 1         , 1         , 1 ],
  35.   [Infinity , 2         , 1         , 2         , 5         , 1         , 1 ],
  36.   [Infinity , 9         , 1         , 2         , 5         , 1         , 1 ],
  37.   [Infinity , 1         , 5         , Infinity  , Infinity  , 1         , 1 ],
  38.   [Infinity , 1         , 5         , 2         , 1         , 1         , 1 ],
  39.   [Infinity , 1         , 5         , 2         , 1         , 1         , 1 ],
  40.   [Infinity , 1         , 5         , 2         , 1         , 1         , 1 ],
  41.   [Infinity , 1         , 5         , 2         , 1         , 1         , 9 ],
  42.   [Infinity , 1         , 5         , 2         , 1         , Infinity  , 9 ],
  43.   [Infinity , 1         , 5         , 2         , 1         , 1         , 1 ],
  44. ];
  45.  
  46. const gridHeight = grid.length;
  47. const gridWidth = grid[0].length;
  48. const startNode = calcNodeID(0, 0, gridWidth);
  49. const endNode = calcNodeID(6, 9, gridWidth);
  50.  
  51.  
  52. const { distance, previous } = dijkstra(grid, gridWidth, gridHeight, startNode, endNode);
  53. const shortestPath = getShortestPath(gridWidth, distance, previous, startNode, endNode);
  54.  
  55.  
  56. const drawnPath = [];
  57. const startCell = shortestPath.shift();
  58. const endCell = shortestPath.pop();
  59.  
  60. for (let i = 0; i < gridHeight; i++) {
  61.   drawnPath.push(new Array(gridWidth).fill('_'));
  62. }
  63.  
  64. drawnPath[startCell[1]][startCell[0]] = 'S';
  65. drawnPath[endCell[1]][endCell[0]] = 'E';
  66. shortestPath.forEach(cell => drawnPath[cell[1]][cell[0]] = 'X');
  67.  
  68. console.log('shortest distances array:', distance);
  69. console.log('previous node taken array:', previous);
  70. console.log('\n' + drawnPath.map(row => row.join(' | ')).join('\n') + '\n');
  71.  
  72.  
  73.  
  74. function calcNodeID(x, y, gridWidth) {
  75.   return y * gridWidth + x;
  76. }
  77.  
  78.  
  79. function calcNodeXY(nodeID, gridWidth) {
  80.   const x = nodeID % gridWidth;
  81.   const y = Math.floor(nodeID / gridWidth);
  82.   return [x, y];
  83. }
  84.  
  85.  
  86. function dijkstra(grid, gridWidth, gridHeight, start, end = null) {
  87.     // list of transforms to apply to find orthogonal edges
  88.   const transforms = [[-1, 0], [0, -1], [1, 0], [0, 1]];
  89.   // additional diagonal edges if wanted
  90.   //, [-1, -1], [-1, 1], [1, -1], [1, 1]];
  91.  
  92.   const pq = new PriorityQueue();
  93.   const numNodes = gridWidth * gridHeight;
  94.   const visited = new Array(numNodes).fill(false);
  95.   const previous = new Array(numNodes).fill(null);
  96.   const distance = new Array(numNodes).fill(Infinity);
  97.  
  98.   distance[start] = 0;
  99.   pq.enqueue(start, 0);
  100.  
  101.   while (pq.size() > 0) {
  102.     const [current, minValue] = pq.dequeue();
  103.     const nodeXY = calcNodeXY(current, gridWidth);
  104.     visited[current] = true;
  105.  
  106.     if (distance[current] < minValue) continue;
  107.  
  108.     // normally a loop of all edges, adjusted for grid
  109.     for (const transform of transforms) {
  110.       const nodeToVisitX = nodeXY[0] + transform[0];
  111.       const nodeToVisitY = nodeXY[1] + transform[1];
  112.       const nodeToVisit = calcNodeID(nodeToVisitX, nodeToVisitY, gridWidth);
  113.  
  114.       if (nodeToVisitX < 0 || nodeToVisitY < 0 || nodeToVisitX >= gridWidth || nodeToVisitY >= gridHeight) continue;
  115.       if (visited[nodeToVisit]) continue;
  116.  
  117.       newDistance = distance[current] + grid[nodeToVisitY][nodeToVisitX];
  118.       if (newDistance < distance[nodeToVisit]) {
  119.         previous[nodeToVisit] = current;
  120.         distance[nodeToVisit] = newDistance;
  121.         pq.enqueue(nodeToVisit, newDistance);
  122.       }
  123.     }
  124.  
  125.     if (current == end) break;
  126.   }
  127.  
  128.  
  129.   return { distance, previous };
  130. }
  131.  
  132.  
  133. function getShortestPath(gridWidth, distance, previous, start, end) {
  134.   const path = [];
  135.  
  136.   if (distance[end] == Infinity) return path;
  137.  
  138.   for (let current = end; current != null; current = previous[current]) {
  139.     path.push(calcNodeXY(current, gridWidth));
  140.   }
  141.  
  142.   return path.reverse();
  143. }
Mar 29 '21 #4
SwissProgrammer
220 128KB
I apologize.

I should have fought with this on my own.

Please someone remove this entire thread.
Mar 29 '21 #5
Rabbit
12,516 Expert Mod 8TB
Rather than deleting it, it would probably be more useful for future readers to know what breakthrough helped you out
Mar 29 '21 #6
SwissProgrammer
220 128KB
Thank you Rabbit.

I did not get it to work.

Thank you for adding your code example. That was nice.

Thank you Rabbit.
Mar 29 '21 #7
Rabbit
12,516 Expert Mod 8TB
Well if the question is unresolved, then even more reason to not delete it
Mar 30 '21 #8
SwissProgrammer
220 128KB
That was me giving up again. And then you helping me to keep trying.

I like this site so much. Thank you.
Mar 31 '21 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Lau | last post by:
How do I easily calculate the shortest path between two geographical spots on a map? The map is divided into zones. So I guess it is possible to use Dijkstra’s Shortest Path algorithm, but it...
6
by: ThanhVu Nguyen | last post by:
Hi all, I need recommendation for a very fast shortest path algorithm. The edges are all directed, positive weights. Dijkstra shortest path will solve it just fine but the if the graph is not...
20
by: Webdad | last post by:
Hi! I running my first year as industrial engineer (informatics) We have an assignment to do : .... create a playfield (matrix). Some places in that field are blocked, so you can't pass them....
5
by: leezard | last post by:
I am developing a program using VB.NET that will accept a start and end point, the system then will generate the shortest path to reach the end point. Anyone here have idea on doing this or some...
4
by: Shuch | last post by:
Hi all, I am in shortage of time...and i want to know if someone has a code written in c++ or c for finding the shortest path using stack or queue??????my specifications r as follow: Input...
2
by: Bytter | last post by:
Hi everyone, I need to implement a very quick (performance-wise) Dijkstra shortest path in python, and found that libboost already has such thing. Problem is: I cannot find the installation...
5
by: aleya | last post by:
I am developing a program using VB.NET that will accept a start and end location (2 list boxes), the system then will generate the shortest path to reach the end point. for your information i got a...
3
by: Nguyen2010 | last post by:
Hello All, I very need the code of the second shortest path or k-shortest paths that paths are separate(only same as source and destination) and find them almost parallel(not find the shortest...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.