I've read from several sources that enemies like the Romans were able to defeat the Greeks in battle taking place on rough and rocky environment didn't lend itself to phalanx warfare. Isn't Greece itself very hilly and uneven though? Why would the phalanx have developed there if the immediate environment appears to pretty much undermine it?
There is a bit of a stereotype that Greece is very hilly terrain, and it is one that the Greeks themselves like to play up, but it is only half right or at least a bit misleading. While there are certainly mountains and hills, it isn't like, for example, Albania which is pretty much all mountains and valleys. To give an idea of what Greece actually looks like, check out this picture I took at the Argos Heraion, an important Archaic period sanctuary. As you can see, while there are mountains, there are also significant plains. Greece's mountainous terrain effectively divides the different regions it doesn't really cut up the terrain itself, if that makes sense.
EDIT: Picture of Albania for comparison, taken at Gjirokaster. You will note that the flatlands here basically represents a single snaking line in between much more imposing mountains than you find in southern Greece.
Because if city states A and B were going to war, the typical offensive strategy would be to go straight for their crops/orchards, which generally lie on flat ground. They'd either show up to fight and you'd have your hoplite battle, or you'd burn their crops/they'd submit.
In a big coalition war, there'd be more emphasis on fighting all over Greece, so then you'll rely more on non-Phalanx based forces.
See here on page 124.
I think it would be best to take a closer look of what actually happened at those battles where Romans beat the Hellenes that used Phalangtais as the main front-line infantry. At Kynoskephalae for example at the earlier stages on the Makedonian Right flank against the Romans pretty move routed the Romans of the left flank, quoting from Polybius:
"By the time this was effected the enemy were close at hand; and, accordingly, the word was given to the phalanx to lower spears and charge; to the light infantry to cover their flank. At the same time Flamininus also, having received his advanced party into the intervals between his maniples, charged the enemy. The charge was made with great violence and loud shouting on both sides: for both advancing parties raised their war cry, while those who were not actually engaged shouted encouragement to those that were; and the result was a scene of the wildest excitement, terrible in the last degree. Philip's right wing came off brilliantly in the encounter, for they were charging down hill and were superior in weight, and their arms were far more suited for the actual conditions of the struggle...seeing that his men were unable to stand the charge of the phalanx, and that his left wing was losing ground, some having already fallen and the rest slowly retiring, but that hopes of saving himself still remained on the right, Flamininus hastily transferred himself to the latter wing."
Suggests that the Battle was not exactly going in the Romans way but what caused the Romans to win was not really by the Roman Legionaries themselves but *their elephants routing the right wing as Polybius states:
"keeping the elephants in front he led the maniples of his right against the enemy. The Macedonians having no one to give them orders, and unable to form a proper phalanx, owing to the inequalities of the ground and to the fact that, being engaged in trying to come up with the actual combatants, they were still in column of march, did not even wait for the Romans to come to close quarters: but, thrown into confusion by the mere charge of the elephants, their ranks were disordered and they broke into flight."
As stated the Makedonians that were still on the march hadn't formed and the elephants charged them and routed them which then led to the morbid ending of the Makedonians who raised their Sarrisae to surrender realizing their left-wing was compromised and the Roman maniples either ignorant of Hellenic customs or seeking glory fell upon them.
The idea of "Makedonian Phalanxes were unusable in non-flat ground" seems a bit contradicted, if they were inflexible on such ground how did the Makedonian Right wing able to go through the hill without much issue in formation? Which raises another problem of the idea I mentioned; why did the Swiss go with Pikes in the later Medieval period? Surely they could of gone with a more looser, more missile-heavy approach of the Ancient Romans (and I kind of don't want to get how the Romans fought ATM, that's probably another question I will ask sooner or later here)? While Polybius then goes onto comment on the inflexibility of Makedonian Phalanxes we have to question his viewpoint as well; were they so? Alexandros Megas had no problem with such "inflexible" formations if he fought against the Achaemenids who had far more mobile troops and vast amounts of cavalry at their disposal yet still lost and also as a honorable mention the bloody conquest of Afghanistan which is very mountainous.
EDIT: placed missed words giving a wrong impression. Should of proof read.