Use C # To implement a simple cooperative game in Game Theory

Source: Internet
Author: User

I recently read a book about the game. There is a simple game of cooperation and non-cooperation. It is very interesting. The general idea is as follows:

This game is A simple abstraction of cooperation between people in real life. The specific content and rules can be summarized as "If A and B are both cooperative attitudes, it is A win-win situation, each person scored 3 points. If A's cooperation attitude and B's play were overcast, A cheated B and obtained B's interest, B scored 5 points and A scored 3 points, and vice versa. In the end, if both A and B do not cooperate, they will take A single shot and take two shots. If both of them are hard-working, each person deducts one point. "In this game, everyone has cooperated with people other than himself for 100 times, the person with the highest score wins.

I abstracted the code to C # using an interface to standardize participants, let them implement their own algorithms, and save the previous cooperation records with competitors through the generic list, the policy can be returned based on the cooperation record .. the interface code is as follows:
1 public interface ActorBase
2 {
3 bool Gamble (string OpponentName); // your policy is encapsulated in this function. true indicates cooperation. false indicates non-cooperation.
4 string GetUniqueCode (); // returns your name for the opponent to confirm your identity
5 int Score {get; set;} // total record Score
6 void AddRecord (string OpponentName, bool record); // used to add records of previous battles
7}

For my strategy, I maintain a cooperative attitude during my first cooperation, and determine whether to cooperate in the future based on the situation of my opponent and the previous step.
The Code is as follows:

1 public class CareySon: ActorBase
2 {
3 Dictionary <string, List <bool> Record; // used to save and Record
4 public Songyunjian () // constructor used to construct records
5
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.