public void InitGame() { DllImport.Initialize("ZenInit-" + m_CurrentGameTimes + " " + DateTime.Now.ToString("yyMMddHHmmss") + ".txt");//TODO:文件名,在界面中加入playerName,然后命名 m_History = new List <Tuple <int, int, bool, bool> >(); ClientLog.FilePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + DateTime.Now.ToString("MM-dd HH-mm-ss") + "~ZenVsZen.sgf";//TODO,命名 ClientLog.WriteLog("(;PB[xyz]PW[abc]"); }
private void ExecuteVsSelf(object obj) { ClientLog.FilePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + DateTime.Now.ToString("MM-dd HH-mm-ss") + "~ZenVsZen.sgf"; DllImport.ClearBoard(); moveCount = 0; new Thread(() => { ClientLog.WriteLog("(;WP[Zen]BP[Zen]"); while (true) { int nextColor = DllImport.GetNextColor(); DllImport.StartThinking(nextColor); Thread.Sleep(1500); DllImport.StopThinking(); bool isThinking = DllImport.IsThinking(); int p0 = 0, p1 = 0; bool p2 = false, p3 = false; DllImport.ReadGeneratedMove(ref p0, ref p1, ref p2, ref p3); string msg = string.Format("Turn:{0}, Generated:\t{1}{2}\t{3}\t{4}", nextColor, (char)('A' + p0), p1 + 1, p2, p3); //ClientLog.WriteLog(msg); Console.WriteLine(msg);//WriteMsgLine(msg); //int para0 = 0, para1 = 0, para2 = 0, para3 = 0, para6 = 0; //float para4 = 0; //byte[] para5 = new byte[19 * 19]; //DllImport.GetTopMoveInfo(para0, ref para1, ref para2, ref para3, ref para4, para5, para6); //msg = string.Format("Turn:{0}, TopMoveInfo:\t{1}{2}\t{3}\t{4}", nextColor, (char)('A' + para1), para2 + 1, para3, para4); //ClientLog.WriteLog(msg); //int[] output = new int[19 * 19]; //DllImport.GetTerritoryStatictics(output); //ArrayChanged?.Invoke(output); if (p2 || p3) { ClientLog.WriteLog(")"); //MessageBox.Show("down"); return; } DllImport.Play(p0, p1, nextColor); moveCount++; ClientLog.WriteLog(";" + (nextColor == 1 ? "W" : "B") + "[" + (char)('a' + p0) + (char)('a' + p1) + "]"); //return; } }).Start(); }
private void ExecuteVsGnugo(object obj) { VsGnugo vsgnugo = new VsGnugo(); OnExit += vsgnugo.Exit; vsgnugo.OnMsgOutput += Vsgnugo_OnMsgOutput; vsgnugo.Start(); ClientLog.FilePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "让八子" + testCount + "~ZenVsGnugo.sgf"; DllImport.ClearBoard(); DllImport.FixedHandicap(9); DllImport.SetNumberOfSimulations(5000); moveCount = 0; ClientLog.WriteLog("(;AB[pd][dd][pp][jj][dj][pj][jp][jd][dp]BP[gnugo]WP[Zen]");//9 //ClientLog.WriteLog("(;AB[pd][dd][pp][dj][pj][jp][jd][dp]BP[gnugo]WP[Zen]");//8 //new Thread(() => // { // Thread.Sleep(2000); // vsgnugo.GenMove(2); // }).Start(); new Thread(() => { int nextColor = DllImport.GetNextColor(); DllImport.StartThinking(nextColor); Thread.Sleep(5000); DllImport.StopThinking(); int p0 = 0, p1 = 0; bool p2 = false, p3 = false; DllImport.ReadGeneratedMove(ref p0, ref p1, ref p2, ref p3); DllImport.Play(p0, p1, nextColor); moveCount++; string msg = string.Format(moveCount + "\tZen:\t{0}", "" + (char)('A' + p0) + (p1 + 1)); Console.WriteLine(msg);//WriteMsgLine(msg); ClientLog.WriteLog(";" + (nextColor == 1 ? "W" : "B") + "[" + (char)('a' + p0) + (char)('a' + p1) + "]"); //nextColor = DllImport.GetNextColor(); char gnuX = (char)('A' + p0); if (gnuX > 'H') { gnuX++; } vsgnugo.InputMove(nextColor, "" + gnuX + (p1 + 1)); }).Start(); }
private void ExecuteIsSuicide(object obj) { ClientLog.FilePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + DateTime.Now.ToString("MM-dd HH-mm-ss") + ".log"; for (int i = 0; i < 19; i++) { for (int j = 0; j < 19; j++) { for (int turn = 1; turn <= 2; turn++) { if (DllImport.IsSuicide(i, j, turn)) { WriteMsgLine("suicide " + i + "," + j + ":" + turn); } } } } WriteMsgLine("ExcuteIsSuicide Over"); }
/// <summary> /// Zen走棋(线程) /// </summary> /// <param name="stepNum"></param> public void GetZenMove(int stepNum) { Task task = Task.Factory.StartNew(() => { int turn = stepNum % 4; DllImport.SetNumberOfSimulations(aiSettings[turn].Layout); DllImport.StartThinking(aiSettings[turn].Color); Thread.Sleep(aiSettings[turn].TimePerMove * 1000); DllImport.StopThinking(); //Thread.Sleep(500); int x = 0, y = 0; bool isPass = false, isResign = false; DllImport.ReadGeneratedMove(ref x, ref y, ref isPass, ref isResign); int count = 0; float winRate = 0; DllImport.GetTopMoveInfo(0, ref x, ref y, ref count, ref winRate, null, 0); DealZenResult(stepNum, x, y, isPass, isResign, count, winRate); }); }
private bool Vsgnugo_OnMsgOutput(string obj, Action <int, string> inputMove) { if (obj.Contains("illegal move")) { WriteMsgLine("illegal"); return(true); } if (obj.Contains("resign")) { ClientLog.WriteLog(")"); //MessageBox.Show("done"); ZenVsGnugoOver(); return(true); } if (obj.Contains("PASS")) { ClientLog.WriteLog(")"); //MessageBox.Show("done"); ZenVsGnugoOver(); return(true); } obj = obj.Substring(2); int opponentColor = DllImport.GetNextColor(); //分析对手 DllImport.StartThinking(opponentColor); Thread.Sleep(1500); DllImport.StopThinking(); int opX = 0, opY = 0; bool opPass = false, opResign = false; DllImport.ReadGeneratedMove(ref opX, ref opY, ref opPass, ref opResign); char gnugoOutX = char.Parse(obj.Substring(0, 1)); if (gnugoOutX > 'I') { gnugoOutX--; } int x = gnugoOutX - 'A'; int y = int.Parse(obj.Substring(1, obj.Length - 1)) - 1; DllImport.Play(x, y, opponentColor); moveCount++; string msg = string.Format(moveCount + "\tgnugo:\t{0}", "" + obj); Console.WriteLine(msg);//WriteMsgLine(msg); ClientLog.WriteLog(";" + (opponentColor == 1 ? "W" : "B") + "[" + (char)('a' + x) + (char)('a' + y) + "]"); if (x != opX || y != opY) { ClientLog.WriteLog("LB[" + (char)('a' + opX) + (char)('a' + opY) + ":A]"); //ClientLog.WriteLog("LB[" + (char)('a' + opX) + (char)('a' + opY) + ":A]C[Zen认为A点可能更好]"); } int nextColor2 = DllImport.GetNextColor();//这一条有时会得不到正确结果 DllImport.StartThinking(nextColor2); Thread.Sleep(1500); DllImport.StopThinking(); int selfX = 0, selfY = 0; bool selfPass = false, selfResign = false; DllImport.ReadGeneratedMove(ref selfX, ref selfY, ref selfPass, ref selfResign); int para0 = 0, para1 = 0, para2 = 0, para3 = 0, para6 = 0; float winning = 0; byte[] para5 = new byte[19 * 19]; DllImport.GetTopMoveInfo(para0, ref para1, ref para2, ref para3, ref winning, para5, para6); if (selfPass || selfResign) { ClientLog.WriteLog(")"); //MessageBox.Show("done"); ZenVsGnugoOver(); return(true); } DllImport.Play(selfX, selfY, nextColor2); moveCount++; //Console.WriteLine("zen\t" + (nextColor2)); msg = string.Format(moveCount + "\tZen:\t{0}", "" + (char)('A' + selfX) + (selfY + 1) + "\t " + winning.ToString("G2")); Console.WriteLine(msg);//WriteMsgLine(msg); ClientLog.WriteLog(";" + (nextColor2 == 1 ? "W" : "B") + "[" + (char)('a' + selfX) + (char)('a' + selfY) + "]C[预估胜率:" + (winning * 100).ToString("G3") + "%]"); char gnuX = (char)('A' + selfX); if (gnuX > 'H') { gnuX++; } inputMove(nextColor2, "" + gnuX + (selfY + 1)); return(false); }
/// <summary> /// 获得外部提交的一步棋步(非主线程) /// </summary> /// <param name="stepNum"></param> /// <param name="x"></param> /// <param name="y"></param> /// <param name="isPass"></param> /// <param name="isResign"></param> public void OutsiderMoveArrived(int stepNum, int x, int y, bool isPass, bool isResign) { m_History.Add(new Tuple <int, int, bool, bool>(x, y, isPass, isResign)); if (isPass) { if (m_History.Count > 1 && m_History[m_History.Count - 2].Item3)//两手pass { ClientLog.WriteLog(")"); if (m_CurrentGameTimes == m_TotalGameLoopTimes)//全部比赛完成 { GameOverCallback?.Invoke(stepNum, x, y, isPass, isResign); } else { m_CurrentGameTimes++; Start();//又重新开始 } return; } } if (isResign) { ClientLog.WriteLog(")"); if (m_CurrentGameTimes == m_TotalGameLoopTimes)//全部比赛完成 { GameOverCallback?.Invoke(stepNum, x, y, isPass, isResign); } else { m_CurrentGameTimes++; Start();//又重新开始 } return; } DllImport.Play(x, y, 2 - stepNum % 2); UICallback?.Invoke(stepNum, x, y, isPass, isResign); if (TerritoryCallback != null) { int[] territoryStatictics = new int[m_BoardSize * m_BoardSize]; DllImport.GetTerritoryStatictics(territoryStatictics); TerritoryCallback.Invoke(territoryStatictics); } ClientLog.WriteLog(";" + (stepNum % 2 == 1 ? "W" : "B") + "[" + (char)('a' + x) + (char)('a' + y) + "]"); //Console.WriteLine((stepNum % 2 == 0 ? "黑" : "白") + " WinRate: " + x + " " + y); stepNum++; int turn = stepNum % 4; if (aiSettings[turn].IsZen) { //如果下一步还是Zen GetZenMove(stepNum); } else { //如果不是zen,交出控制 HandTurnCallback?.Invoke(stepNum); } }
/// <summary> /// 处理Zen结果(非主线程) /// </summary> /// <param name="stepNum"></param> /// <param name="x"></param> /// <param name="y"></param> /// <param name="isPass"></param> /// <param name="isResign"></param> private void DealZenResult(int stepNum, int x, int y, bool isPass, bool isResign, int count, float winRate) { m_History.Add(new Tuple <int, int, bool, bool>(x, y, isPass, isResign)); if (isPass) { if (m_History.Count > 1 && m_History[m_History.Count - 2].Item3)//两手pass { ClientLog.WriteLog(")"); if (m_CurrentGameTimes == m_TotalGameLoopTimes)//全部比赛完成 { GameOverCallback?.Invoke(stepNum, x, y, isPass, isResign); } else { m_CurrentGameTimes++; Start();//又重新开始 } return; } } if (isResign) { ClientLog.WriteLog(")"); if (m_CurrentGameTimes == m_TotalGameLoopTimes)//全部比赛完成 { GameOverCallback?.Invoke(stepNum, x, y, isPass, isResign); } else { m_CurrentGameTimes++; Start();//又重新开始 } return; } DllImport.Play(x, y, 2 - stepNum % 2); //因为GetTerritoryStatictics耗时,所以这样处理两次,让UI连续 int[] territoryStatictics = null; if (TerritoryCallback != null) { territoryStatictics = new int[m_BoardSize * m_BoardSize]; DllImport.GetTerritoryStatictics(territoryStatictics); } UICallback?.Invoke(stepNum, x, y, isPass, isResign); if (TerritoryCallback != null) { TerritoryCallback.Invoke(territoryStatictics); Console.WriteLine((stepNum % 2 == 0 ? "黑" : "白") + "走棋 黑胜率: " + (stepNum % 2 == 0 ? winRate : 1 - winRate).ToString("F2") + " 黑领先目数:" + (territoryStatictics.Sum() / 1000.0 - 6.5).ToString("F1")); } ClientLog.WriteLog(";" + (stepNum % 2 == 1 ? "W" : "B") + "[" + (char)('a' + x) + (char)('a' + y) + "]" + "C[胜率:" + winRate.ToString("F2") + "% count=" + count + "]"); WinRateCallback?.Invoke(stepNum % 2 == 0 ? winRate : 1 - winRate); stepNum++; int turn = stepNum % 4; if (aiSettings[turn].IsZen) { //如果下一步还是Zen GetZenMove(stepNum); } else { //如果不是zen,交出控制 HandTurnCallback?.Invoke(stepNum); } }