A simple cooperative game in Game Theory C # implementation

Source: Internet
Author: User
Tags bool unique id

I've been reading a book about games recently. There is a relatively simple game of non-cooperative cooperation. It's interesting, the main idea is this:

This game is a simple abstraction of the cooperation between people in real life, the specific content and rules can be summed up as "if A and B are cooperative attitude, it is a double win, 3 points per person, if a cooperative attitude, B play Yin, then a cheated B, achieved B should be benefited, then B to 5 points, a deduction 3 points, and vice versa. Finally, if A and B are not cooperative, then a beat two scattered, two people are in vain, then each buckle a point "in this game, everyone and in addition to their own cooperation 100 times, the highest scoring people win."

I abstract into C # code by using an interface to standardize the participants, to enable them to implement their own algorithms, and to save the previous collaboration record with the opponent through a generic list, and to return to the adopted policy based on the cooperative record. No nonsense. The interface code is as follows:

1 public interface ActorBase
2 {
3  bool Gamble(string OpponentName);//你的策略封装在这个函数里,true是合作false是不合作
4  string GetUniqueCode();//用于返回你的名字来让对手确认你的身份
5  int Score { get; set; }//记录总分
6  void AddRecord(string OpponentName,bool record);  //用于添加以往对战的记录
7 }

As for my strategy, I maintained a cooperative attitude in my first cooperation and whether I would cooperate in the future according to the situation of the opponents and the previous step.

The specific code is as follows:

1public class Careyson:actorbase
2 {
3 dictionary<string, list<bool>> record;//used to save and match the previous Record the
4 public Songyunjian ()//constructor for constructing records
5 {
6 record = new dictionary<string, list<bool>> ();
7}
8 public string Getuniquecode ()//back to your unique ID
9 {
Ten return "Careyson";
One}
public void AddRecord (string opponentname, bool record)
{
?    Record.containskey (opponentname)//If not, create a collaboration record
{
list<bool> L = new list<bool> ();
17 L.add (record);
Record.add (Opponentname, L);
19}
Else
{
Record[opponentname]. Add (record),//Use the indexer for adding records to the list
{}
}
public bool Gamble (string name)
{
?     Record.containskey (name)//If it is the first time cooperation, then maintain a cooperative attitude
{
return true;
}
to else
{
33 List<bool> t = record[name];
if (t.count >= 1)
35 {
if (t[t.count-1])//If the last time is cooperation, return cooperation
(s) {
returns true;
else//}
   Returns a non-cooperative
=
is returned false;
$}

' return true;
'
'
48
$}
a public int Score
Wuyi {
' get {return _score;}
Set{_score=value;}
54}
a public int _score=0;//is used to record each person's score
of

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.