Longest Road (EVER)
November 9, 2013
Posted by on
I think I actually got it. What a bitch.
It was a nightmare. I knew it wasn’t going to be easy, but when I first started planning for it, I was honestly stumped. I pieced together the eventual formula, but it took a lot of brain juice.
I’ll break it down. First off, I copy an array of all the roads the player has. Then from that array, I separate all the roads into single segments. What that means is that it is a list of roads that only have a single path. Of course these road segments could be longer when the roads have multiple branches and directions, but this is the simplest form. The roads are all accounted for in this instance. After this, I cycle back through all the segments matching them with other segments and creating new segments. This creates a list of all possible road paths (through all possible branches). This creates a longest road. Although, there’s a catch. Road length can be intercepted by opposing settlements, thus, there’s another check these completed segments must go through. I kept tabs on the ends of the roads throughout the process, so all I did now was cycle through the completed segments catching to see if an ‘enemy’ settlement was blocking, keeping track of the length. At the end, the longest length recorded and returned. Ta-da.
Clear sailing ahead!
Oh fuck, I forgot about ports again. Son of a …