Solve some diplomatic issues
You are given a set of moves in a theoretical Diplomacy game. We're not going to handle validation of moves being legal or not, simply attempt to resolve a turn.
Here are the simplified rules to Diplomacy (full rules here):
- There are two types of units: Armies and Fleets. Armies can only move on land, Fleets on land and in the sea. You can safely ignore this, because we're assuming that all of the moves you've been given are at least theoretically valid.
- There are four types of moves: Hold (stay in place), Move, Support (a certain other unit), Convoy.
- When two or more units end up in the same country, whichever unit has the most support stays. Each unit without the most support (or each unit tied for the most) returns to its original country. A unit which is not Moving and which is not tied for the most support is Dislodged.
- A Fleet can Convoy an Army through its space to another space. That convoy is cancelled if the Fleet is Dislodged. Only a Fleet in a sea space can Convoy.
- A unit can support another unit holding or moving into an adjacent country if it can move into that country (Armies can't Support Fleets in the sea).
- A supporting unit which is attacked ceases to support, unless it is supporting an attack on the unit attacking it. If it is dislodged, it ceases to support in any case.
- The Beleaguered Garrison rule: If a unit is attacked by two units with the same amount of support, the attacked unit is not Dislodged, and the attackers return to their original countries.
You will receive a list of moves. Each move will be in the following format:
Power Unit_type Origin_country Move_type Destination_country
There is no Destination_country if a unit holds (or you can insert a placeholder). Destination_country for Support or Convoy is the Origin_country of the unit being Supported or Convoyed. For example, you could get:
E F Eng M Pic
F A Pic H
G A Bel S Eng
English Fleet in the English Channel Moves to Picardy
French Army in Picardy Holds
German Army in Belgium Supports the Fleet in the English Channel to Move to Picardy
Your output should be the location of each unit after the move. Any Dislodged unit should be marked as such.
Power Unit_type Country Dislodged?
For the example given above, the output should be:
E F Pic
F A Pic D
G A Bel
The more difficult part of this is in regards to convoys. A convoy fails if there is no valid path for the army to take. For instance, if we have:
E F Nth C Den
E A Den M Hol
F F Eng M Nth
F F Bel S Eng
The French fleets dislodge the English Fleet in the North Sea, and the convoy does not take place. But if we also had
E F Hel C Den
There would still be a valid path for the English Army and the convoy would succeed.
Be warned, there are paradoxes in Diplomacy. These are to be treated as undefined behaviour; any output is acceptable where
- Each unit is listed
- There is only one non-dislodged unit per country
- In any country with a dislodged unit there is a non-dislodged unit
- Each non-moving unit is listed in its origin country
- Each moving unit is listed either in its origin country or its destination country
Note that, in particular, each unit being in its starting country is a valid output.
For use with convoys, it's helpful to know which countries are adjacent to which other countries. (Everything is assumed to be valid, but it may not be clear whether a convoy with a single cut link is still possible otherwise).
You may assume that this list is available to you in any format: a function that takes two countries and returns true/false, a variable, a file, etc. In any case, the list itself does not add to your byte count.
[To be added]
You can use any Input/Output formats you choose, as is standard.
Questions for the sandbox
- How much clearer does my description of the rules of Diplomacy need to be?
- Does this sound like an interesting challenge?