21 Flags Game Theory Puzzle - Dynamic Solution With C#

For conext, please read my previous posts:
I’m developing a program that can automatically generate a winning strategy based on different situations (e.g. different starting flags, winning condition, flags that allowed to remove per turn), compare to previous code, hard code some logic, and which only works for specific situations.
 
During the development, I discovered that I have to apply two different strategies  depending on the flags that  are allowed to be removed per turn. I called it: 
 
Consecutive Removeable Flags solution
  • Non-Consecutive Removeable Flags solution
  • I will explain it further in below.
I create a class named FlagRemovalBrain.cs to generate the winning strategy , and following are the properties of this class
  1. public List<int> FlagsThatCanBeRemove { getset; }
  2. public List<int> LosingStateFlagsNum { getset; }
  3. public bool UpperHandIfStartFirst { getset; }
  4. public int WinningCondition { getset; }
  5. public bool IsConsecutive { getset; }
  6. public bool IsCommonMultiple { getset; }
FlagsThatCanBeRemove is flag/s that can be removed per turn.
 
LosingStateFlagsNum is flag/s that will cause user to lose if  left to this amount of flag/s during his/her turn to make a move (refer to part 1 for detailed explanation).
 
UpperHandIfStartFirst is one of the important enhanced features for this version, which the system will have an insight before the game starts by analyzing all the parameters, and will know whether by choosing to start first or second if you will have the upper hand in order to win the game.
 
Please take note that you may not necessarily win the game if you have the upper hand by starting first/later, but at least you will NEVER lost the game if you have the upper hand and make no mistake in every coming turn. On the contrary, your only hope to win or at least draw is to rely on your opponent to make at least 1 mistake if you do not start in the upper hand position.
 
WinningCondition is the how the game ends. The game ends and the players will lose if being left with only 1 remaining flag, then the value for this property is 1, 0 for 0 flag and so on.
 
IsConsecutive is a property if the removable flags per turn are all in consecutive order. For example, if the removeable flags per turn are [1, 2, 3], then it is in consecutive order. But, if the removeable flags are [1, 2, 4,] then it is not.
 
IsCommonMultiple is a property to tell if the removable flags per turn contain at least 2 numbers and have common multiplier. For example, if the removable flags per turn are [1, 2, 4 ], first of all it is not consecutive, and it has at least two numbers that share a common multiplier (2 and 4 share the same multiplier of 2). This property is important in solving the Non-Consecutive Removeable Flags scenario, which I will explain later.
 
I begin with discussing the solution for Consecutive Removeable Flags scenario
 

Consecutive Removeable Flags Solution

 
Let's say we have 21 flags initially, and the removeable flags per turn is [1,2,3], and finally the winning condition is 1 flag and ONLY 1 flag remained to opponent. So in the implementation , the value of those parameters looks like this
  1. static void Main(string[] args)
  2. {
  3.     int[] FLAGS_TO_REMOVED = new[] {1,2,3 };
  4.        
  5.     const int STARTING_FLAGS = 21;
  6.     const int WINNING_CONDITION = 1;
  7.        
  8.     PrintRules(STARTING_FLAGS, WINNING_CONDITION, FLAGS_TO_REMOVED);
  9.     bool startFirst = true;
  10.        
  11.     string playAgain;
  12.     do
  13.     {
  14.         GameStart(STARTING_FLAGS, WINNING_CONDITION, FLAGS_TO_REMOVED, startFirst);
  15.         WriteLine("Play Again? Y/N");
  16.         playAgain = ReadLine();
  17.     }
  18.     while (playAgain.ToUpper() != "N" );
Constructor will assign the properties values of the class, e.g. assign value for winning condition, check if it is consecutive etc.
  1. public FlagRemovalBrain(int _WinningCondition, int StartingFlags, List<int> _FlagsThatCanBeRemove)
  2. {
  3.     IsConsecutive = !_FlagsThatCanBeRemove.Select((i, j) => i - j).Distinct().Skip(1).Any();
  4.     FlagsThatCanBeRemove = _FlagsThatCanBeRemove;
  5.     WinningCondition = _WinningCondition;
  6.     IsCommonMultiple = !IsConsecutive && IfRemoveableContainCommonMultiple(FlagsThatCanBeRemove);
  7.     (LosingStateFlagsNum, UpperHandIfStartFirst) = UpperHandAnalysis(IsConsecutive, StartingFlags);
  8.        
  9.     Console.WriteLine("Upper hand if {0}", UpperHandIfStartFirst ? "Start First" : "Start Second");
  10. }
Please note that the UpperHandAnalysis method, which serves to generate winning strategy and, from the strategy generated, we will know that if we have upper hand by starting the game first or second.
  1. public (List<int>, bool) UpperHandAnalysis(bool IsConsecutive,int StartingFlags)
  2. {
  3.     if(IsConsecutive)
  4.     {
  5.         return WiningStrategyForConsecutive(WinningCondition, StartingFlags, FlagsThatCanBeRemove);
  6.     }
  7.     else
  8.     {
  9.         return WiningStrategyForNonConsecutive(WinningCondition, StartingFlags);
  10.     }
  11. }
The UpperHandAnalysis method will decide which winning strategy method is to be called based on the IsConsecutive property. In this case, WiningStrategyForConsecutive method will be called.
  1. public (List<int>, bool) WiningStrategyForConsecutive(int WinningCondition, int StartingFlags, IEnumerable<int> FlagsThatCanBeRemove)
  2. {
  3.        
  4.     List<int> LosingStateFlagsNum = new List<int>();
  5.        
  6.     for (int i = WinningCondition; i <= StartingFlags; i++)
  7.     {
  8.         bool isLosingFlagStage = true;
  9.         foreach (int item in LosingStateFlagsNum)
  10.         {
  11.             int num = i - item;
  12.        
  13.             if (FlagsThatCanBeRemove.Contains(num))
  14.             {
  15.                 isLosingFlagStage = false;
  16.                 break;
  17.             }
  18.         }
  19.        
  20.         if (isLosingFlagStage)
  21.             LosingStateFlagsNum.Add(i);
  22.     }
  23.        
  24.     bool upperHandIfStartFirst = LosingStateFlagsNum.Max() != StartingFlags;
  25.     return (LosingStateFlagsNum, upperHandIfStartFirst);      
  26. }      
The winning strategy for consecutive is pretty simple
  1. Get the losing scenario ( 1 in this case), and using backward deduction, get the “Losing State Num” (player who being left with these number of flag/s is lose)
  2. For loop starts with 1, which also a losing state num, add it to the List.
  3. Keep the increment, and find out that 2,3 and 4 are NOT losing state numbers, because whoever is left with these number of flags can remove [1,2,or 3] flag/s to leave his/her opponent with 1 flag.
  4. Once we reach 5, we will find it is a “losing state number”, since removing [1,2,or 3] flag/s are unable to leave 1 flag to your opponent in the next turn.
  5. So 5 (and 1 previously) will be added to the List of “losing state numbers”.
  6. And the for loop continues until reaching 21 (the starting flags amount).
Please read my previous post for better understanding of this algorithm.
The C# decision to pull flags for the consecutive removeable flags solution is pretty simple,
  1. public int PulledFlagDecisionConsecutive(int remainingFlags)
  2. {
  3.     int pulledFlag = 0;
  4.     var remainingLosingStateFlagsNum = LosingStateFlagsNum.Where(x => x < remainingFlags).ToList();
  5.        
  6.     foreach (int item in remainingLosingStateFlagsNum)
  7.     {
  8.         int num = remainingFlags - item;
  9.         if (FlagsThatCanBeRemove.Contains(num))
  10.         {
  11.             pulledFlag = num;
  12.             break;
  13.         }
  14.     }
  15.     if (pulledFlag == 0)
  16.     {
  17.         Random random = new Random();
  18.         int index = random.Next(FlagsThatCanBeRemove.Count);
  19.         pulledFlag = FlagsThatCanBeRemove[index];
  20.     }
  21.     return pulledFlag;
  22. }
Computer only neesd one parameter, which is the current remaining number of flags, and removing the flag/s accordingly to ensure it left the opponent with “losing state number of flag/s “.
 
For this case, the losing state of number of flags are: [1, 5, 9, 13, 17, 21] , so, if current remaining flags is 20, computer will remove 3 flags to make sure its opponent has 17 flags left for his/her turn, remove 1 flag to make it 13, if the remaining flags is 14 and so on.
 
The console of the game will look like this,
 
21 Flags Game Theory Puzzle - Dynamic Solution With C#
 
21 Flags Game Theory Puzzle - Dynamic Solution With C#
 
By the way, users can choose to start first or later by changing the value of startFirst Boolean variable in the main class.
 
21 Flags Game Theory Puzzle - Dynamic Solution With C#
 

Non-Consecutive Removeable Flags Solution

 
For non-consecutive removeable flags solution, things are little bit more complicated, and non-consecutive solution can also split to TWO kind of solutions, which are,
  • Common Multiplier Scenario
  • Non Common Multiplier Scenario
I start with Non Common Multiplier Scenario.
 
So, consider the following scenario,
  • starting flag : 21 flags,
  • winning condition is 1 flag and ONLY 1 flag remained to opponent
  • removeable flags [2 , 7] (2 and 7 share no common multiplier)
Since 2 and 7 are not consecutive, so  I need to get all possibilities from game start (21 flags) till reach the end game (1 flag remained). There are 20 from start to end (21 minus 1), so I need to find all the permutation of sum between 2 and 7 to get 20.
 
I found a code snippet from https://jaytaylor.com that enable me to doing so.
 
So, there are 11 different permutations for 2 and 7 to get 20
 
21 Flags Game Theory Puzzle - Dynamic Solution With C#
 
I include a console project called CheckCombination in my solution for the checking.
 
In my FlagRemovalBrain class, there is a method to store all these possible paths.
  1. List<List<int>> ListOfPaths = GenarateAllPaths(remainingFlags - WinningCondition);
Each permutation list means every possibility  -- both players can remove the flags in the game. For example, 7,7,2,2,2 means first player takes 2 flags, then the second player takes 2 flags, then alternately 2 flags, 7 flags and 7 flags before reach 1 flag and end the game.
 
And from all the possible paths, the computer will choose the path with ODD number of count as winning condition. The rational of choosing ODD number of count is because whoever left with the last flag (in this example) is the loser, so the winner of this game is whoever FINSIHED THE LAST NUMBER of the path.
 
Let's assume now 12 flags remain, and the only possible path is 2,7,2 , and this list of path is ODD in count, so if you start your path with an ODD number of paths, you will secure the victory by removing 2 (2,7,2), and your opponent removes 7 (2,7,2) and you remove the final 2 (2,7,2), your opponent will be left with 1 flag (in this example remained 1 flag lost the game) and you win.
 
21 Flags Game Theory Puzzle - Dynamic Solution With C#
 
So we can say ODD is the key to victory.
 
(or choose EVEN number if the person takes the last flag wins, ODD or EVEN just depends how you design your logic, there is no special law that state that ODD must be the solution)
 
BUT, it does not mean we will secure a victory by choosing any ODD count of list of numbers, we still have to consider that the last number of ODD count list does not exist in the last number of EVEN count list.
 
21 Flags Game Theory Puzzle - Dynamic Solution With C#
 
From the above example, there are 11 permutations , where ten of them are odd counts of list of numbers (5) and one is even count (10), and the ODD count contains 2 and 7 as their last number, and EVEN contains only 2.
 
In order to secure the victory, we don't only need to choose the ODD count path, but also the last digit that only exists in ODD count list but not in EVEN count. In this case, it is 7, so we should choose odd count path that end with 7, whether 2,2,2,7,7, or 7,2,2,2,7 etc.
 
The reason is if we choose the list that ends with 2 (we hope the game end in this path [2,2,7,7,2] or [7,7,2,2,2] ) and expect our opponent to choose 7 in second or fourth turn so we can end the game accordingly, but we might faileto secure the victory if our opponent choose the EVEN count path [2,2,2,2,2,2,2,2,2,2] and finally will cause us a defeat. But, if we choose 7, all those remaining possibly path will lead us to our victory.
 
Following are the code snippets from PulledFlagDecisionNonConsecutive method that assigns last number of ODD count and EVEN count list of numbers to a different list, then compare both lists using Linq Except method to get the last number that only exist in ODD count. (if any)
  1. foreach (List<int> list in ListOfArrays)
  2. {
  3.     if (list.Count() % 2 != 0)//get odd Count of list of integer
  4.     {
  5.         if (!WinningSelection.Contains(list[list.Count - 1]))
  6.             WinningSelection.Add(list[list.Count - 1]);
  7.     }
  8.     else
  9.     {
  10.         if (!LosingSelection.Contains(list[list.Count - 1]))
  11.             LosingSelection.Add(list[list.Count - 1]);
  12.     }
  13.        
  14.     if (WinningSelection.Count == FlagsThatCanBeRemove.Count && LosingSelection.Count == FlagsThatCanBeRemove.Count)
  15.         break;
  16. }
  17. List<int> uniqueList = WinningSelection.Except(LosingSelection).ToList();//get unique last digit
The above steps will continue until we get the victory.
 
Forced Stalemate
 
How will the program react if all the possible path is an even number? That means it will lose regardless of how it choose. Well, if that is the case , the program has two choices, hoping the opponent  makes at least one mistake, or, trying to forced a stalemate if there is a way.
 
Consider the following scenario,
  • starting flag : 9 flags,
  • winning condition is 1 flag and ONLY 1 flag remained to opponent
  • removeable flags [2 , 5, 7]
So the target number is 8 and there is only one possible permutation,
 
 21 Flags Game Theory Puzzle - Dynamic Solution With C#
 
If the program chooses 2, it will lose the game after four turns by it and its opponent choose 2 alternately for another three turns.
 
If the computer realizes that it is in the losing state, the ForceStaleMate method will be called and the program will try to force a stalemate, by choosing any other number (if any) from removeable flags to break the ‘predicable outcome’. In this case, it will choose 5 or 7, so the game will end as a draw. A draw is still better than losing the game.
 
Here is the code snippet of the ForceStaleMate method
  1. public int ForceStaleMate(List<int> LosingSelection )
  2. {
  3.        
  4.     List<int> StaleMateList =  FlagsThatCanBeRemove.Except(LosingSelection).ToList();
  5.     if (StaleMateList.Count > 0)
  6.     {
  7.         StaleMateList.Sort();
  8.         return StaleMateList[StaleMateList.Count - 1];
  9.     }
  10.     else
  11.     {  //will lose anyway no matter choose what
  12.         //only hope the opponent make a mistake in their turn
  13.         Random random = new Random();
  14.         return FlagsThatCanBeRemove[random.Next(0, FlagsThatCanBeRemove.Count)];
  15.     }
  16. }
And that’s all about solutions for Non Common Multiplier Scenario. Now I will move to the final part of this article, the solution for Common Multiplier Scenario.
 

Common Multiplier Scenario

 
Consider the following scenario,
  • starting flag : 9 flags,
  • winning condition is 1 flag and ONLY 1 flag remained to opponent
  • removeable flags [2 , 4] (2 and 4 share common multiplier which is 2)
so the target number is 8 (9 minus 1), and the all possible permutations are , like following,
 
21 Flags Game Theory Puzzle - Dynamic Solution With C#
 
The last number of ODD count and EVEN count list of number are the same , both contain [2,4] , if we use the above mentioned logic for non common multiplier solution, the program will think that this is the losing situation, regardless of what number it chooses.
 
But it is not, since the removeable flags have at least two numbers which  share the common multiplier, we need to do an adjustment by summing up adjacent two numbers if the sum result exists in the list of removeable numbers.
 
In this case, we will add the adjacent numbers 2 from first, second and fourth row to become 4, since 4 is one of list removeable numbers for this example[2,4].
 
However, we will NOT add 2 and 4 to become 6, because 6 is NOT one of list of removeable numbers.
 
This is the code snippet of adding the two adjacent numbers and checking if those sum results exist in the list of removeable flags.
  1. public List<int> SumTwoAdjacent(List<int> arr, List<int> ListToRemoved, out List<int> afterAdjustment)
  2. {
  3.        
  4.     bool gotAdjIdentical = false;
  5.     List<int> NewArr = new List<int>();
  6.     for (int i = 0; i < arr.Count; i++)
  7.     {
  8.         if (i < arr.Count - 1 && arr[i] == arr[i + 1] && ListToRemoved.Contains(arr[i] + arr[i + 1]))
  9.         {
  10.             gotAdjIdentical = true;
  11.             NewArr.Add(arr[i] + arr[i + 1]);
  12.             i += 1;
  13.             continue;
  14.        
  15.         }
  16.         NewArr.Add(arr[i]);
  17.     }
  18.     afterAdjustment = NewArr;
  19.     if (gotAdjIdentical) 
  20.     {
  21.         SumTwoAdjacent(NewArr, ListToRemoved, out afterAdjustment);
  22.     }
  23.     return afterAdjustment;
  24. }
21 Flags Game Theory Puzzle - Dynamic Solution With C#
 
So after adjustment, there is only ONE ODD count of list, which is [2,4,2], so the program can secure a victory by removing 2 for this turn, and regardless  of whether 2 or 4 are removed by its opponent in next turn, the program can secure the victory in the final turn.
 
You can have my full source code via GitHub.
 
Please let me know if you able to beat the program after you let it have the upper hand by starting the game first/second as it has claimed. You should not, since my goal is to create an unbeaten flag removal program.