Prisoner’s Dilemma .NET

The prisoner’s dilemma is a classic game of logic that has made its way into the computer science arena in the past. I am presenting Prisoners Dilemma.NET as a general programming challenge based around this game. I will be accepting custom built prisoners and facing them off in a master interrogation competition at the end of this challenge.

The challenge deadline will be May 1st, 2008. All you have to do is to create your prisoners into 1 or more .NET assemblies (v2.0 or greater) and send them to me in a zip file to enter the challenge.
I have created a prisoners dilemma application for you to use as a test bench for prisoner creation. It comes complete with the prisoner’s dilemma core source code for you to review, unit tests and a WPF application to see how your prisoners line up against other prisoners visually. There is a sample project created for you called “CustomPrisoners” with a basic prisoner outlined. All you need to do is implement a few abstract methods to get your prisoner going.
For example, here is the Mafia Hitman source code:
public class MafiaHitman : Prisoner
    public override bool Interrogate(string partnerId)
        return false;
    public override void Sentence(InterrogationResponse partnerResponse, string partnerId)
    public override string Name
            return “Mafia Hitman”;
Why the Prisoner’s Dilemma?
For a real detailed explanation of the prisoner’s dilemma you should head over to the Wikipedia article for the Prisoner’s Dilemma. If this is too dense for you and you would prefer someone to explain it in simpler terms I recommend hearing Richard Dawkins explanation, from Nice Guys Finish First. It’s lengthy but quite good.
What caused me to put together this challenge, though, was a conversation I had with a colleague regarding the use of torture as a valid means of interrogation. At some point in the discussion I decided to use the Prisoner’s Dilemma as an example to counter one of his arguments. Essentially he was arguing that American’s should be allowed to torture terrorists because they are also torturing American soldiers. So, of course, this discussion gets much more complicated than this but when I brought up the idea that the Prisoner’s Dilemma should illustrate that cooperation is more beneficial than betrayal (or not torturing enemies of America as, at least, a gesture to also not torture American soldiers) he disregarded the study and claimed that it has questionable scientific validity to begin with.
So as someone who values reason I would like to perform a bit of a scientific study to validate or invalidate the theory behind the Prisoner’s Dilemma before we continue with this point of the debate.
Challenge Details
·         You may freely download and run the source code for the Prisoners Dilemma .NET challenge. You may also modify it but this is the code that will run for the challenge.
·         Changes may be made to the PrisonersChallenge.Core code base between now and the challenge deadline. Be sure to check more recent blog posts for notifications of any changes before submitting your entry. Anybody who has already made an entry will be notified of changes if contact information is available.
·         Changes will not be made to the codebase unless major bugs are found or critical security issues are revealed with the present codebase.
·         You must submit only a single .NET library file (dll) per submission. All other file types will be disregarded.
·         All entries must be packaged in a single zip file. So that means 1 dll in 1 zip file is an entry.
·         (optional) You should create an Assembly level attribute in each submitted assembly of the type PrisonersDilemma.Core.PrisonerAuthorAttribute with your name and email address so I can get back to you later with results.
·         The challenge will be run on a VPC with limited memory and no internet access.
·         No code obfuscation allowed.
·         No private reflection allowed.
·         The challenge will be run in a medium trust environment.
·         You may use the Temporary folder for writing files if you so choose.
·         You may package files and 3rd party assemblies as embedded resources in your prisoner assembly if you so choose but your submission must be only a single .NET assembly file.
·         By submitting code to this challenge you give me (Justin Chase) the rights to execute and distribute this source code as I see fit.
·         I reserve the right to deny any entry for any reason.
·         Entries will be denied if they appear to be doing anything malicious to the machine or to the memory of the application.
Challenge Procedure
Each prisoner will iteratively be interrogated against each other Prisoner. What this means is that for each prisoner in the application your prisoner will be put up against that prisoner 100 times. A unique value will identify each prisoner so they can keep track of each interrogation against a specific partner.
Insanity Variation
Given the fact that I’m executing arbitrary, 3rd party code for this challenge I, naturally, needed to cover the situations where a Prisoner throws an exception from within its Interrogate or Sentence methods. Rather than take down the entire application for these scenarios I have decided to eat those exceptions and treat them as if the Prisoner has “plead insanity”. If a prisoner does this then they will receive the maximum sentence.
This has some interesting implications because it expands the previous game states from 2 possible choices to 3, which adds a little variety into the mix. I will draw up a small chart to illustrate the various possibilities.
A / B
Plead Insanity
2, 2
5, 0
2, 5
0, 5
4, 4
4, 5
Plead Insanity
5, 2
5, 4
5, 5
I chose these values by the following logic:
·         This whole theory assumes that both parties are in fact guilty and that the reward comes from not being able to be proven guilty with 100% certainty.
·         If you’ve plead insane then you are known to be 100% guilty.
·         Betraying someone who has pleaded insanity does not benefit the cops at all, you are simply admitting your own guilt.
·         This probably won’t change the game too much since it’s unlikely anyone will ever take the tactic of intentionally pleading insanity.
It’s well known that the “tit for tat” strategy was the winning strategy behind the original Prisoner’s Dilemma challenges. I would love to see some interesting tit-for-tat prisoners appear on the scene but it will only be interesting if we have lots of varied strategies to compare it with. I have included a few basic strategies that aren’t very interesting but I would love to see every possible tactic exhausted for comparison.
Just remember this isn’t a competition! This is a programming challenge intended to be fun and to be a casual scientific experiment so please, create a variety of interesting prisoners.
If you find any bugs in the source code or have any recommendations feel free to email me directly!

Dynamic Templated TreeViewItem in WPF

I struggled with this for a long time for some reason so I decided to post a simple example here for everyone who may be encountering the same thing. The scenario I have here is when you have a treeview with several hardcoded treeview items and at the lowest levels of leafs you have them bound to some data provider. The trick is trying to dynamically bind the treeview items to different data templates based on some criteria.

The real example was to have a label and a text box for the dynamic items based on whether you are trying to edit it or not. The trick turned out to be to wrap your DataTemplate in a control and use styling to dynamically switch it.

 Download Sample Project










   <ControlTemplate x:Key=”SelectedTemplate” TargetType=”{x:Type Control}”>

    <Label Content=”{Binding XPath=title}” Foreground=”Red” FontWeight=”Bold”  />


   <ControlTemplate x:Key=”DefaultTemplate” TargetType=”{x:Type Control}”>

    <Label Content=”{Binding XPath=title}” />


   <DataTemplate x:Key=”template”>

    <Control Focusable=”False”>


      <Style TargetType=”{x:Type Control}”>

       <Setter Property=”Template” Value=”{StaticResource DefaultTemplate}” />                           


         <Trigger Property=”IsMouseOver” Value=”True”>

          <Setter Property=”Template” Value=”{StaticResource SelectedTemplate}” />







    <XmlDataProvider x:Key=”justnbusiness” Source=”; />           

















     Source={StaticResource justnbusiness},


    ItemTemplate=”{StaticResource template}”

    Header=”Blog Posts”/>




















Today I spent some time looking at how to organize my WPF application to use a Model View Controller design pattern. I ended up getting it to work fairly well but I am pretty dissapointed in the lack of support for this model directly.

Specifically I got hung up on the limitations around the ObjectDataProvider class. This is a DataSourceProvider object you can use to bind your controls against. You have two options with this data provider, you either specify an ObjectType or an ObjectInstance. If you specify the ObjectType then you end up with complete design time support for databinding against this provider, which is a wonderful thing in Blend 2. However, if you do this then the ObjectDataProvider will take it upon itself to create your instance based on the Type information you provided… which does not work if you want to specify an interface or an abstract class. Using the ObjectInstance approach will also give you design time support if you’re the instance you provide is also declared  in Xaml but this has the same activation limitations as simply specifying the Type. So this is a huge problem for an MVC style application.

What I really wanted to do was to create interfaces for all of my Modelsobjects, such as IProduct and IPurchase. Then I could create some mock objects for my test environment and let the other developer create the real objects for the real application. Well how the heck do you use the ObjectDataProvider so that it’s possible to bind against an abstract Model, the real instance of which is created by the controller? I think the answer is that you cannot, well not without sacrificing design time databinding support at least.

After looking at it for a while I realized that if I set IsInitialLoadEnabled property of my ObjectDataSource to false then after the controller is set in my form I could set the ObjectInstance field to my Model provided by the controller, then call InitialLoad. This would work but no design time support 🙁

You’d think that you could specify the ObjectType for design time support then, using the initial load disabled trick create the real object but you would be wrong. Doing this throws an exception, you cannot set both fields for some inexplicable reason. The next thing you’d think of would be to inherit from ObjectDataProvider, override a method that creates the instance and instead create an event such as LoadInstance so that your controller can pass in the instance instead… but once again you’d be wrong. It seems that the ObjectDataProvider does not really expose any virtual methods of much use.

My next thought is to try to create an MvcDataProvider that supports this functionality a little better. I’m a little hesitant to do this though because of the difficulties I’ve had in the past creating a DataSource for ASP.NET. What a nightmare that was, even a very simple one.

Other than this one problem (which I’m hoping is just an ignorance problem still) I have to say I am enjoying using WPF immensely. Maybe creating a new DataSourceProvider will be just as easy and intuitive as the rest of WPF.

If anybody has any insight on how to resolve this issue I’d love to hear it. If I end up creating an MvcDataProvider I’ll be sure to post it in another blog.