In my previous post I had create an unbeaten flag removal C# program to play the classical Game Theory 21 Flags puzzle, today I want to create another few flag removal program in C# but with different rules and winning condition.
First game is start with 21 Flags, whoever being left the LAST flag is the loser, similar to my previous post.Following is the snippet of the code for this situation.
Second game is start with 21 Flags, whoever being left with NO flag is the loser. Following is the snippet of the code for this situation
Third game is start with 21 Flags, whoever being left with 5 flags or less is the loser. Following is the snippet of the code for this situation
Regarding of how I get the ‘Losing flags Numbers’ for different situation, please refer to my previous post
My program will start by let user to choose which game that would like to challenge by providing the number, i.e. 1 for Game 1, 2 for Game 2 and 3 for Game 3.
The pseudocode will look like this,
Based on user’s selection on which game she would like to play, I need to provide corresponding flag removal method to fight against her, in the below do while loop. So I realized that I need to pass the C# function as a references to the below do while loop, and the suitable solution is using delegates function.
I just learned the feature of delegates in C# not long ago, so I think the best way to understand it is by using it in solving my problem.
For my understanding, delegates is passing the function as a parameter, like what we did in passing argument to a function, but this time the entire function is the parameter we pass.
One important rule of delegates is all the method that being parameterized must share same signature, which means they accept same parameter/s and have same return type.
All of m three functions share same signature, they return Integer type data, and accept one parameter in Integer type, this means they can be parameterized.
First I create a delegate that have same signature with all my functions, and named it as RemoveAIFlagDel.
delegate int RemoveAIFlagDel(int remainingflags);
Then, create an instance of this delegate
RemoveAIFlagDel removeFlagAIdeleg = null;
Then pass my functions name to this delegate instance in switch statement, which at here I will know which game the user want to play, so I can use the correct function to beat her.
Delegate “removeFlagAIdeleg” will become my variable that store my function, and I will pass it to my another function named ProcessComputerDecision, which another function that I created to process the current situation (remaining flags, how many flag the computer need to remove in order to beat the user etc) of the game.
ProcessComputerDecision(remainingFlags, turn, removeFlagAIdeleg, ListOfResultTable);
Then we are done, we can now play the game.
Different to my previous post, this time I create a class named ResultTable to store the information all the turn, from this table we can see the decision that had been made from each player in every turn until the last turn, which looks like below.
Full source code can be download at my Git Hub repository
Side Notes
- If you understand the Game Theory Backward Induction well , the best you can do in this C# program is beat my Game 2, which whoever left with 0 flag lose the game.
- Since the winner already being decided in the first step, and who start first did matter in this kind of Flag Remover game regardless what is the wining condition or how many flags can be removed in each turn or even how many flags start in the first place.
- You only can win Game 2 because the wining condition for Game 2 is start first but another two games is start latter (In addition you do not remove the wrong flags number in each turn, otherwise my program will left you no room to win), so what is the point create this program?
- In my future post, I try to create a better challenge that let the computer randomly generate the starting flags, the number of flags can be removed in each turn, the winning condition (i.e. how many flag left to loser), and in order to make it more challenging, I will try to create a Flag Remover function that able to analysis and come out the best strategy dynamically based on those random generated parameters, off course including able to come out the conclusion whether start first or latter have the upper hand to win the flags remover game.
- Human can do Backward Induction in their brain by giving the condition like 21 flags, remove 1–3 and last flag remaining lose the game. But how about like starting with 2,999,999 flags, remove prime number flags each turn and whoever being left with the smallest prime number (which is 2) or a number cannot be reduced again to the losing number of flags consider loser.
- I will include it in my next post if I able to code it in C#. ??