Beispiel #1
0
        private void SkControl_DoubleClick(object sender, MouseEventArgs e)
        {
            bool AddStone(Point point)
            {
                if (board.AddStone(point, Current))
                {
                    Current = Next;
                    return(true);
                }
                return(false);
            }

            if (!AddStone(WorldToIndex(ScreenToWorld(e.X + (stoneSize / 2), e.Y + (stoneSize / 2)))))
            {
                return;
            }

            AddStone(ai.GetMove(board));

            if (board.DidTeamWin())
            {
                if (board.DidTeamWin(Team.Ai))
                {
                    blackPoints.Text = $"Black player points: {++bPoints}";
                }
                else
                {
                    pinkPoints.Text = $"Pink player points: {++pPoints}";
                }

                board = new Board();
            }
        }
Beispiel #2
0
        private Point RandomWeightedMove(Board board)
        {
            int SimulateBoard(Point point)
            {
                const int roundLimit = 10;

                var random      = new Random();
                var clonedBoard = new Board(board.Stones.ToDictionary(s => s.Key, s => s.Value));
                var currentTeam = Team.Ai;
                var round       = 0;

                clonedBoard.AddStone(point, Team.Ai);

                while (!clonedBoard.DidTeamWin() && round < roundLimit)
                {
                    var possibleMoves = clonedBoard.PossibleMoves();
                    int randomIndex   = random.Next(possibleMoves.Count);
                    clonedBoard.AddStone(possibleMoves[randomIndex], currentTeam);

                    currentTeam = currentTeam == Team.Ai ? Team.Player : Team.Ai;
                    round++;
                }

                if (round >= roundLimit)
                {
                    return(1000);
                }

                return(clonedBoard.DidTeamWin(Team.Ai) ? round : round *round);
            }

            var moves = new List <(int Weight, Point Point)>();

            foreach (var point in board.PossibleMoves())
            {
                var weight = 0;
                for (var i = 0; i < 10; i++)
                {
                    weight += SimulateBoard(point);
                }

                moves.Add((weight, point));
            }

            return(moves
                   .MinBy(m => m.Weight)
                   .Shuffle()
                   .First().Point);
        }