private void ButtonNewGame_Click(object sender, RoutedEventArgs e) { Board.Tables myTable; Board.Tables yoTable; bool isMyFirst; if (RadioButtonMyInner.IsChecked.Value) { myTable = Board.Tables.Inner; } else if (RadioButtonMyOuter.IsChecked.Value) { myTable = Board.Tables.Outer; } else if (RadioButtonMyLeft.IsChecked.Value) { myTable = Board.Tables.Left; } else { myTable = Board.Tables.Right; } if (RadioButtonYoInner.IsChecked.Value) { yoTable = Board.Tables.Inner; } else if (RadioButtonYoOuter.IsChecked.Value) { yoTable = Board.Tables.Outer; } else if (RadioButtonYoLeft.IsChecked.Value) { yoTable = Board.Tables.Left; } else { yoTable = Board.Tables.Right; } if (RadioButtonMyFirst.IsChecked.Value) { isMyFirst = true; } else { isMyFirst = false; } mainBoard = new Board(myTable, yoTable, isMyFirst, false); StageMain.Board = mainBoard; mainBoard.Changed += MainBoard_Changed; if (RadioButtonMyControllerAI.IsChecked.Value) { myController = Controllers.AI; } else if (RadioButtonMyControllerHuman.IsChecked.Value) { myController = Controllers.Human; } if (RadioButtonYoControllerAI.IsChecked.Value) { yoController = Controllers.AI; } else if (RadioButtonYoControllerHuman.IsChecked.Value) { yoController = Controllers.Human; } //종료 절차 if (mcts != null) { isRunning = false; userWaiter.Set(); mcts.ProgressUpdated -= Mcts_ProgressUpdated; ResumeSearching(); mcts.ForceStopSearch(); thread?.Join(); } if (realYame == null) { realYame = new RealYame(client); } if (onlyPolicy == null) { onlyPolicy = new OnlyPolicy(client); } mcts = new Mcts(realYame) { MaxVisitCount = 500 }; TextBoxMaxVisitCount.Text = mcts.MaxVisitCount.ToString(); mcts.Init(mainBoard); mcts.ProgressUpdated += Mcts_ProgressUpdated; ResumeSearching(); thread = new Thread(runner); thread.Start(); }
void genMcts() { Console.WriteLine("genMcts ... "); //게임 한 판 시작 --------------------------- List <Tuple <Board, Move> > recP1 = new List <Tuple <Board, Move> >(); List <Tuple <Board, Move> > recP2 = new List <Tuple <Board, Move> >(); //랜덤으로 보드 생성 //상대방 선수로 놓는다. 어차피 시작하자마자 GetOpposite로 돌릴 거다. RealYame yame = new RealYame(tcpCommClient); OnlyPolicy policy = new OnlyPolicy(tcpCommClient, policyNetName); Mcts mcts1 = new Mcts(yame); Mcts mcts2 = new Mcts(policy); mcts1.MaxVisitCount = 300; mcts2.MaxVisitCount = 1; //누가 먼저 시작하나. bool isMyFirst = Global.Rand.NextDouble() > 0.5; Board board = new Board((Board.Tables)Global.Rand.Next(4), (Board.Tables)Global.Rand.Next(4), isMyFirst); bool isMyWin = false; mcts1.Init(board); mcts2.Init(board); for (int turn = 0; turn < 100; turn++) { Move move; Task <Node> task; if (board.IsMyTurn) { task = mcts1.SearchNextAsync(); } else { task = mcts2.SearchNextAsync(); } task.Wait(); move = task.Result.prevMove; if (move.IsEmpty) { var moves = board.GetAllPossibleMoves(); move = moves[Global.Rand.Next(moves.Count - 1)]; } //움직임을 저장해주고 if (isMyFirst) { recP1.Add(new Tuple <Board, Move>(board, move)); } else { recP2.Add(new Tuple <Board, Move>(board, move)); } mcts1.SetMove(move); mcts2.SetMove(move); board = board.GetNext(move); board.PrintStones(); //겜이 끝났는지 확인 if (board.IsFinished) { isMyWin = board.IsMyWin; break; } } //턴제한으로 끝났으면 점수로 if (!board.IsFinished) { isMyWin = (board.Point > 0); } lock (dataPolicy) { if (isMyWin) { Console.WriteLine(" Collect data : my win"); var flip = from rec in recP1 select(new Tuple <Board, Move>(rec.Item1.GetFlip(), rec.Item2.GetFlip())); dataPolicy.AddRange(recP1); dataPolicy.AddRange(flip); var list1 = from rec in recP1 select(new Tuple <Board, float>(rec.Item1, 1.0f)); var list2 = from rec in recP2 select(new Tuple <Board, float>(rec.Item1.GetOpposite(), 0f)); var list1Flip = from rec in list1 select(new Tuple <Board, float>(rec.Item1.GetFlip(), rec.Item2)); var list2Flip = from rec in list2 select(new Tuple <Board, float>(rec.Item1.GetFlip(), rec.Item2)); dataValue.AddRange(list1); dataValue.AddRange(list2); dataValue.AddRange(list1Flip); dataValue.AddRange(list2Flip); } else { Console.WriteLine(" Collect data : YO win"); var flip = from rec in recP2 select(new Tuple <Board, Move>(rec.Item1.GetFlip(), rec.Item2.GetFlip())); dataPolicy.AddRange(recP2); dataPolicy.AddRange(flip); var list1 = from rec in recP1 select(new Tuple <Board, float>(rec.Item1, 0f)); var list2 = from rec in recP2 select(new Tuple <Board, float>(rec.Item1.GetOpposite(), 1.0f)); var list1Flip = from rec in list1 select(new Tuple <Board, float>(rec.Item1.GetFlip(), rec.Item2)); var list2Flip = from rec in list2 select(new Tuple <Board, float>(rec.Item1.GetFlip(), rec.Item2)); dataValue.AddRange(list1); dataValue.AddRange(list2); dataValue.AddRange(list1Flip); dataValue.AddRange(list2Flip); } } }