HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

A* algorithm solution to "intermediate space probe" challenge

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
spacechallengealgorithmprobesolutionintermediate

Problem

This code is my solution to this Reddit challenge:

Description:


NASA has contracted you to program the AI of a new probe. This new probe must navigate space from a starting location to an end location. The probe will have to deal with Asteroids and Gravity Wells. Hopefully it can find the shortest path.

Map and Path:


This challenge requires you to establish a random map for the challenge. Then you must navigate a probe from a starting location to an end location.

Map:


You are given N -- you generate a NxN 2-D map.



  • 30% of the spots are "A" asteroids



  • 10% of the spots are "G" gravity wells (explained below)



  • 60% of the spots are "." empty space.


When you generate the map you must figure out how many of each spaces is needed to fill the map. The map must then be randomly populated to hold the amount of Gravity Wells and Asteroids based on N and the above percentages.


N and Obstacles


As n changes so does the design of your random space map. Truncate the
amount of obstacles and its always a min size of 1. (So say N is 11 so
121 spaces. At 10% for wells you need 12.1 or just 12 spots) N can be
between 2 and 1000. To keep it simple you will assume every space is
empty then populate the random Asteroids and Gravity wells (no need to
compute the number of empty spaces - they will just be the ones not
holding a gravity well or asteroid)

Asteroids


Probes cannot enter the space of an Asteroid. It will just be
destroyed.

Empty Spaces


Probes can safely cross space by the empty spaces of space. Beware of
gravity wells as described below.

Gravity Wells


Gravity wells are interesting. The Space itself is so dense it cannot
be travelled in. The adjacent spaces of a Gravity well are too strong
and cannot be travelled in. Therefore you might see this. . = empty
space, G = gravity well

.....
 .....
 ..G..
 .....
 .....




But due to the gravity you cannot

Solution

This is quite a bit of code, so I'll start with a few things for now:

-
I appreciate that you're using ` instead of rand(). Even in C++11 solutions, the latter seems to still be used.

-
SIZE can be a constexpr instead, though this may not really matter for a simple constant.

-
Since this is C++11, you can consider the
library in place of .

-
Vector2 should be a class so that its data members (x and y) can be kept under private. In addition, you can make this a templated class so that both data members aren't restricted to just int`s.

-
These lines seem unnecessary:

std::cin.ignore(2);
return 0;


The first one is done right before termination. The second one can be omitted because the compiler will provide the same return at this same point.

Code Snippets

std::cin.ignore(2);
return 0;

Context

StackExchange Code Review Q#94668, answer score: 5

Revisions (0)

No revisions yet.