### Can someone explain this code to me?

Hey I'm pretty new to programming, and for the semester we have an extra credit group project. It's about codingames's skynet revolution episode 1.

<https://www.codingame.com/ide/puzzle/skynet-revolution-episode-1>

My group got the solution to the problem, but I really want to understand it, so here is the code can someone explain it to me.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117`` ``````#include #include #include #include #include using namespace std; struct Node // data structure node { int A; bool GateWay = false; // gate way virus is trying to get to bool pathvisited = false; //paths we have visited vector connections; // links between the nodes }; vector nodes; // vector for nodes vector getGatewayLinks(Node* node) // node pointer for get gateway { vector nodes; // vector for nodes for (auto& connection : node->connections) // for-loop using variable connection scope resolution to look for total connections in the node { if (connection->GateWay) // if connection scope points to gateway / exit nodes.push_back(connection); //add connection to the end of vector nodes } return nodes; // return the vector nodes } pair findRoute(Node* first) { for (auto& node : nodes) // finds movement of the virus node.pathvisited = false; // return false when virus is not found because it moved away. bool Location = false; // location of virus pair PlanB; // if fails activate planB queue pathway; // Queue pathway pathway.push(first); // add to pathway while (!pathway.empty()) // if pathway is not empty { Node* node = pathway.front(); // node = last element of the queue pathway.pop(); // pop/ remove the last element of the queue node->pathvisited = true; // vector gatewayLinks = getGatewayLinks(node); if (gatewayLinks.size() > 1) { return {node->A, gatewayLinks[0]->A}; } else if (gatewayLinks.size() == 1) { if (!Location) { Location = true; PlanB = {node->A, gatewayLinks[0]->A}; } for (auto& neighbor : node->connections) { if (!neighbor->pathvisited) pathway.push(neighbor); } } else { if (!Location) { for (auto& neighbor : node->connections) { if (!neighbor->pathvisited) pathway.push(neighbor); } } } } if (Location) return PlanB; } int main() { int N; // the total number of nodes in the level, including the gateways int L; // the number of links int E; // the number of exit gateways cin >> N >> L >> E; cin.ignore(); nodes.resize(N); for (int i = 0; i < N; ++i) nodes[i].A = i; for (int i = 0; i < L; i++) { int N1; // N1 and N2 defines a link between these nodes int N2; cin >> N1 >> N2; cin.ignore(); nodes[N1].connections.push_back(&nodes[N2]); nodes[N2].connections.push_back(&nodes[N1]); } for (int i = 0; i < E; i++) { int EI; // the index of a gateway node cin >> EI; cin.ignore(); nodes[EI].GateWay = true; } // game loop while (1) { int SI; // The index of the node on which the Skynet agent is positioned this turn cin >> SI; cin.ignore(); pair connection = findRoute(&nodes[SI]); cout << connection.first << " " << connection.second << endl; } }``````

Last edited on
I'm not going to go through that line by line. If there are specific lines you don't understand, please post the specific lines you don't understand.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Mainly this part and some lines in the main.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849`` ``````pair findRoute(Node* first) { for (auto& node : nodes) // finds movement of the virus node.pathvisited = false; // return false when virus is not found because it moved away. bool Location = false; // location of virus pair PlanB; // if fails activate planB queue pathway; // Queue pathway pathway.push(first); // add to pathway while (!pathway.empty()) // if pathway is not empty { Node* node = pathway.front(); // node = last element of the queue pathway.pop(); // pop/ remove the last element of the queue node->pathvisited = true; // vector gatewayLinks = getGatewayLinks(node); if (gatewayLinks.size() > 1) { return {node->A, gatewayLinks[0]->A}; } else if (gatewayLinks.size() == 1) { if (!Location) { Location = true; PlanB = {node->A, gatewayLinks[0]->A}; } for (auto& neighbor : node->connections) { if (!neighbor->pathvisited) pathway.push(neighbor); } } else { if (!Location) { for (auto& neighbor : node->connections) { if (!neighbor->pathvisited) pathway.push(neighbor); } } } } if (Location) return PlanB; }``````
when dealing with someone else's uncommented gibberish code you have a few options.

What I usually do is throw it away if it does not work out of the box for the problem I was trying to solve. Almost nothing is so complex that spending time figuring out unclear code is worth the time.

If that is not an option, and you have to figure it out, the next step is to treat it like a debugging problem. Pull it apart, add print statements, fill it with data, run it and see what results it produces, and what intermediate results it produces, etc. Exercise it with all kinds of test cases until you get a feel for what it is doing.

You have a better option not always available: why not ask your friends who wrote it how it works?! What was your contribution to the team, anyway? Surely you had some role in the algorithm development or design?

Last edited on