예제 #1
0
        public Move GetMove()
        {
            Board cloneBoard = b.Clone();

            while (true)
            {
                Console.WriteLine("Enter row and column for move coordinates, split by space");
                string moveCoordsString = Console.ReadLine();
                if (String.IsNullOrWhiteSpace(moveCoordsString) == true)
                {
                    continue;
                }
                string[] coords = moveCoordsString.Split(' ');
                if (coords.Length != 2)
                {
                    continue;
                }
                int row;
                int column;
                if (int.TryParse(coords[0], out row) == true)
                {
                    if (int.TryParse(coords[1], out column) == true)
                    {
                        Move result = new Move(row, column);
                        if (cloneBoard.PlaceStone(result) == true)
                        {
                            return(result);
                        }
                    }
                }
            }
        }
예제 #2
0
        public bool ReceiveTurn(Move m)
        {
            if (Root.Children != null)
            {
                foreach (UCTNode child in Root.Children)
                {
                    if (child.Position.Equals(m))
                    {
                        Console.WriteLine("UCTTurbo-{0} had {1} nodes, lost {2} nodes and now has {3} nodes", _player == 1?"Black":"White", Root.MeasureTree(), Root.MeasureTree() - child.MeasureTree(), child.MeasureTree());
                        Root = child;
                        Root.Parent.Children = null;
                        child.Parent         = null;
                        if (child.Children == null)
                        {
                            child.CreateChildren();
                        }
                        return(true);
                    }
                }
            }
            Board newBoard = Root.BoardState.Clone();

            if (newBoard.PlaceStone(m) == false)
            {
                throw new ArgumentException("invalid turn");
            }
            Console.WriteLine("UCTTurbo-{0} had {1} nodes, lost {1} nodes and now has {2} nodes", _player == 1 ? "Black" : "White", Root.MeasureTree(), 1);
            Root.Children = null; //break the link for garbage collection
            UCTNode newRoot = new UCTNode(null, new Move(m), newBoard);

            newRoot.CreateChildren();
            Root = newRoot;
            return(true);
        }
예제 #3
0
        double GetWinrate(Move move)
        {
            if (_startingTestingBoard == null)
            {
                _startingTestingBoard = new Board();
            }
            _startingTestingBoard.CopyStateFrom(_actualBoard);
            if (_startingTestingBoard.PlaceStone(move) == false)
            {
                return(-1);
            }
            UInt64 sim  = 0;
            int    wins = 0;

            while (sim < GameParameters.RandomSimulations)
            {
                int winner = PlaySimulation();
                if (winner != 0)
                {
                    sim++;
                    if (winner == _actualBoard.ActivePlayer)
                    {
                        wins++;
                    }
                }
            }
            return(sim > 0 ? (double)wins / sim : -1);
        }
예제 #4
0
        public int PlaySimulation()
        {
            if (_testingBoard == null)
            {
                _testingBoard = new Board();
            }
            _testingBoard.CopyStateFrom(_startingTestingBoard);
            int turnsSimulated = 0;

            while (turnsSimulated < GameParameters.GameDepth && _testingBoard.IsGameOver() == false)
            {
                turnsSimulated++;
                int  moveCount = GetAvailableMoves(_testingBoard);
                Move pass      = new Move(-1, -1); //добавить в список возможных ходов пас
                _availableMoves[moveCount++] = pass;
                _availableMoves.Shuffle(moveCount);
                for (int i = 0; i < moveCount; i++)
                {
                    if (_testingBoard.PlaceStone(_availableMoves[i]) == true)
                    {
                        break;
                    }
                }
            }
            int winner = _testingBoard.DetermineWinner();

            return(winner);
        }
예제 #5
0
        private int PlayRandomGame(UCTNode node)
        {
            _boardClone.CopyStateFrom(node.BoardState);
            int turnsSimulated = 0;

            while (turnsSimulated < GameParameters.GameDepth && _boardClone.IsGameOver() == false)
            {
                turnsSimulated++;
                Move m = new Move(-5, -5);
                do
                {
                    m.row    = RandomGen.Next(-1, GameParameters.BoardSize);
                    m.column = RandomGen.Next(-1, GameParameters.BoardSize);
                } while (_boardClone.PlaceStone(m) == false);
            }
            int winner = _boardClone.DetermineWinner();

            return(winner);
        }
예제 #6
0
 public void CreateChildren()
 {
     lock (this)
     {
         int   size = Board.Size;
         Board b    = BoardState;
         if (Children != null)
         {
             return;
         }
         if (Parent == null || Parent.Children == null)
         {
             Children = new List <UCTNode>(size * size);
         }
         else
         {
             Children = new List <UCTNode>(Parent.Children.Count);
         }
         for (int i = 0; i < size; i++)
         {
             for (int j = 0; j < size; j++)
             {
                 //is on empty space on the board
                 if (b[i, j] == 0 && b.IsEye(i, j) != b.ActivePlayer)
                 {
                     Board anotherCloneBoard = b.Clone();
                     Move  m = new Move(i, j);
                     if (anotherCloneBoard.PlaceStone(m) == true)
                     {
                         Children.Add(new UCTNode(this, m, anotherCloneBoard));
                     }
                 }
             }
         }
         Children.Shuffle();
         HasChildren = true;
     }
 }
예제 #7
0
        public int PlaySimulation()
        {
            if (_testingBoard == null)
            {
                _testingBoard = new Board();
            }
            _testingBoard.CopyStateFrom(_startingTestingBoard);
            int turnsSimulated = 0;

            while (turnsSimulated < GameParameters.GameDepth && _testingBoard.IsGameOver() == false)
            {
                turnsSimulated++;
                Move m = new Move(-1, -1);
                do
                {
                    m.row    = RandomGen.Next(-1, GameParameters.BoardSize);
                    m.column = RandomGen.Next(-1, GameParameters.BoardSize);
                } while (_testingBoard.PlaceStone(m) == false);
            }
            int winner = _testingBoard.DetermineWinner();

            return(winner);
        }
예제 #8
0
        public static void PlayGame(IPlayer blackPlayer, IPlayer whitePlayer)
        {
            gameRecord.AppendLine("(;FF[4]GM[1]SZ[9]AP[dotNetGo]");

            gameRecord.AppendLine(String.Format("PB[{0}]", blackPlayer.Name));
            gameRecord.AppendLine("HA[0]");
            gameRecord.AppendLine(String.Format("PW[{0}]", whitePlayer.Name));
            gameRecord.AppendLine("KM[6.5]");
            gameRecord.AppendLine("RU[Chinese]");
            gameRecord.AppendLine("");
            gameRecord.AppendLine("");
            Board board = new Board();

            while (board.IsGameOver() == false)
            {
                Move move;
                switch (board.ActivePlayer)
                {
                case 1:
                    move = blackPlayer.GetMove();
                    break;

                default:     //case 2:
                    move = whitePlayer.GetMove();
                    break;
                }
                if (blackPlayer.ReceiveTurn(move) == false)
                {
                    throw new ImpossibleException("somehow invalid turn made it through", "PlayGame");
                }
                if (whitePlayer.ReceiveTurn(move) == false)
                {
                    throw new ImpossibleException("somehow invalid turn made it through", "PlayGame");
                }
                if (move.row >= 0 && move.column >= 0)
                {
                    gameRecord.AppendFormat(";{0}[{1}{2}]", board.ActivePlayer == 1? "B": "W", alphabet[move.column], alphabet[move.row]);
                }
                if (board.PlaceStone(move) == false)
                {
                    throw new ImpossibleException("somehow invalid turn made it through", "PlayGame");
                }
                Console.WriteLine(board);
                //Console.ReadLine();
            }
            switch (board.State)
            {
            case Board.GameState.BlackSurrendered:
                Console.WriteLine("White won by resignation, last position:");
                break;

            case Board.GameState.WhiteSurrendered:
                Console.WriteLine("Black won by resignation, last position:");
                break;

            case Board.GameState.DoublePass:
                double blackScore, whiteScore;
                board.DetermineWinner(out blackScore, out whiteScore);
                gameRecord.AppendFormat(";RE[{0}+{1}]", blackScore > whiteScore?"B":"W", Math.Abs(blackScore - whiteScore));
                Console.WriteLine(board);
                Console.WriteLine("Turn: {0}", board.TurnNumber);
                Console.WriteLine("Black score: {0}; White score: {1}", blackScore, whiteScore);
                Console.WriteLine("last position:");
                break;
            }
            Console.WriteLine(board);
            gameRecord.Append(")");
            DateTime dt       = DateTime.Now;
            string   filename = String.Format("{0}-{1}-{2}-{3}-{4}-{5}.sgf",
                                              dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);

            File.WriteAllText(filename, gameRecord.ToString(), Encoding.UTF8);
        }
예제 #9
0
 public bool ReceiveTurn(Move m)
 {
     return(b.PlaceStone(m));
 }
예제 #10
0
        public static void PlayGame(IPlayer blackPlayer, IPlayer whitePlayer)
        {
            gameRecord.AppendLine("(;FF[4]GM[1]SZ[9]AP[dotNetGo]");

            gameRecord.AppendLine(String.Format("PB[{0}]", blackPlayer.Name));
            gameRecord.AppendLine("HA[0]");
            gameRecord.AppendLine(String.Format("PW[{0}]", whitePlayer.Name));
            gameRecord.AppendLine("KM[6.5]");
            gameRecord.AppendLine("RU[Chinese]");
            gameRecord.AppendLine("");
            gameRecord.AppendLine("");
            Board board = new Board();
            while (board.IsGameOver() == false)
            {
                Move move;
                switch (board.ActivePlayer)
                {
                    case 1:
                        move = blackPlayer.GetMove();
                        break;
                    default: //case 2:
                        move = whitePlayer.GetMove();
                        break;
                }
                if (blackPlayer.ReceiveTurn(move) == false)
                    throw new ImpossibleException("somehow invalid turn made it through", "PlayGame");
                if (whitePlayer.ReceiveTurn(move) == false)
                    throw new ImpossibleException("somehow invalid turn made it through", "PlayGame");
                if (move.row >= 0 && move.column >= 0)
                    gameRecord.AppendFormat(";{0}[{1}{2}]", board.ActivePlayer == 1? "B": "W", alphabet[move.column], alphabet[move.row]);
                if (board.PlaceStone(move) == false)
                    throw new ImpossibleException("somehow invalid turn made it through", "PlayGame");
                Console.WriteLine(board);
                //Console.ReadLine();
            }
            switch (board.State)
            {
                case Board.GameState.BlackSurrendered:
                    Console.WriteLine("White won by resignation, last position:");
                    break;
                case Board.GameState.WhiteSurrendered:
                    Console.WriteLine("Black won by resignation, last position:");
                    break;
                case Board.GameState.DoublePass:
                    double blackScore, whiteScore;
                    board.DetermineWinner(out blackScore, out whiteScore);
                    gameRecord.AppendFormat(";RE[{0}+{1}]", blackScore > whiteScore?"B":"W", Math.Abs(blackScore-whiteScore));
                    Console.WriteLine(board);
                    Console.WriteLine("Turn: {0}", board.TurnNumber);
                    Console.WriteLine("Black score: {0}; White score: {1}", blackScore, whiteScore);
                    Console.WriteLine("last position:");
                    break;
            }
            Console.WriteLine(board);
            gameRecord.Append(")");
            DateTime dt = DateTime.Now;
            string filename = String.Format("{0}-{1}-{2}-{3}-{4}-{5}.sgf",
                dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);
            File.WriteAllText(filename, gameRecord.ToString(), Encoding.UTF8);
        }
예제 #11
0
 public bool ReceiveTurn(Move m)
 {
     return(_actualBoard.PlaceStone(m));
 }