1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#include <algorithm>
#include <chrono>
#include <iostream>
#include <random>
#include <vector>
class Node {
public:
int x, y;
int f, g, h;
Node* parent;
Node();
Node(int x_arg, int y_arg, int f_arg,
int g_arg, int h_arg, Node* parent_arg);
friend std::ostream& operator<<(std::ostream& os, const Node& n)
{
return { os << "x: " << n.x << " y: " << n.y << " f: " << n.f << '\n' };
}
};
Node::Node() : x {}, y {}, f {}, g {}, h {}, parent { nullptr } {}
Node::Node(int x_arg, int y_arg, int f_arg,
int g_arg, int h_arg, Node* parent_arg)
: x { x_arg }, y { y_arg }, f { f_arg },
g { g_arg }, h { h_arg }, parent { parent_arg }
{}
inline bool operator==(const Node& l, const Node& r)
{
return l.x == r.x && l.y == r.y;
}
int getRndInt(int min, int max);
Node createRndNode(int min, int max);
int main()
{
constexpr std::size_t NEW_NODES_MAX { 8 };
std::vector<Node> closed(getRndInt(5, 11));
std::generate(closed.begin(), closed.end(),
[]() -> Node { return createRndNode(10, 15); } );
// For debug only:
for(const auto& v : closed) { std::cout << "closed: " << v; }
Node new_nodes[NEW_NODES_MAX];
for(std::size_t i{}; i < NEW_NODES_MAX; ++i) {
new_nodes[i] = createRndNode(10, 15);
// For debug only:
std::cout << "new_nodes: " << new_nodes[i];
}
for (const auto& n : new_nodes) {
if(closed.end() == std::find_if(closed.begin(),
closed.end(),
[&n] (const Node& a) { return n == a; } ) ) {
std::cout << "No matches up to here.\n";
} else {
std::cout << "One match found\n";
}
}
return 0;
}
int getRndInt(int min, int max)
{
static std::mt19937 eng { static_cast<unsigned>(
std::chrono::high_resolution_clock::now()
.time_since_epoch()
.count() ) };
std::uniform_int_distribution<> dis(min, max);
return dis(eng);
}
Node createRndNode(int min, int max)
{
return Node(getRndInt(min, max), getRndInt(min, max), getRndInt(min, max),
getRndInt(min, max), getRndInt(min, max), nullptr);
}
|