首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

博弈論中的簡單合作博弈 C# 實作(四)

     編輯:關於C語言

最後是客戶端呼叫的程式碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TheGame;

public partial class GameTheory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<TheGame.ActorBase> player = new List<TheGame.ActorBase>();
        TheGame.ActorBase CareySon = new TheGame.Songyunjian();
        TheGame.ActorBase RandomPlayer = new TheGame.RandomPlayer();
        TheGame.ActorBase Tony = new TheGame.Tony();
        TheGame.ActorBase Jack = new TheGame.Jack();
        player.Add(CareySon);
        player.Add(RandomPlayer);
        player.Add(Tony);
        player.Add(Jack);

        /* 從這裡開始下面都是演算法部分 */
        for (int x = 1; x <= 100; x++) // 循環合作 100 次
        {
            for (int i = 0; i < player.Count; i++) // 讓選手與其他所有選手博弈
            {
                for (int j = i + 1; j < player.Count; j++)
                {
                    bool abBool = player[i].Gamble(player[j].GetUniqueCode());
                    ActorBase ab = player[i];
                    bool absBool = player[j].Gamble(player[i].GetUniqueCode());
                    ActorBase abs = player[j];

                    if (abBool && absBool) // 當 AB 合作時
                    {
                        ab.Score += 3;
                        abs.Score += 3;
                        ab.AddRecord(abs.GetUniqueCode(), true);
                        abs.AddRecord(ab.GetUniqueCode(), true);
                    }
                    else if (abBool & !absBool) // 當 AB 合作而 ABS 不合作
                    {
                        ab.Score -= 3;
                        abs.Score += 5;
                        ab.AddRecord(abs.GetUniqueCode(), false);
                        abs.AddRecord(ab.GetUniqueCode(), true);
                    }
                    else if (absBool & !abBool) // 當 ABS 合作而 AB 不合作
                    {
                        ab.Score += 5;
                        abs.Score -= 3;
                        ab.AddRecord(abs.GetUniqueCode(), true);
                        abs.AddRecord(ab.GetUniqueCode(), false);
                    }
                    else if (!absBool && !abBool) // 雙方都不合作
                    {
                        ab.Score -= 1;
                        abs.Score -= 1;
                        ab.AddRecord(abs.GetUniqueCode(), false);
                        abs.AddRecord(ab.GetUniqueCode(), false);
                    }
                }
            }
        }
        OutputResult(player); // 輸出並列印成績
    }

    private void OutputResult(List<TheGame.ActorBase> l)
    {
        foreach (ActorBase ab in l)
        {
            HttpContext.Current.Response.Write(
                "Player: <span style='background-color:#cdcdcd;color:blue;border:solid 1px #3cdcad'>" 
                + ab.GetUniqueCode() + "</span> and the Score is <span style='background-color:#cdcdcd;color:blue;border:solid 1px #3cdcad'>" 
                + ab.Score.ToString() + "</span><br />");
        }
    }
}

至此程式碼結束,程式碼稍顯粗糙,各位看官切勿丟磚頭~-.-!!

某次執行結果如下:

---------------------
Player: CareySon and the Score is 686
Player: RandomPlayer and the Score is 570
Player: Tony and the Score is 670
Player: Jack and the Score is 734
---------------------

園子裡有很多博弈論高手,有什麼好策略發上來一起探討吧 :-)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved