Exemplo n.º 1
0
        /// <summary>
        /// Sets a random position on the board for the given player and returns the position it was set at.
        /// </summary>
        /// <param name="playerComputer"></param>
        /// <param name="board"></param>
        /// <returns></returns>
        public int SetPosition(PlayerEnum.Player playerComputer, Dictionary <int, string> board, IEnumerable <IComputerMove> fallback)
        {
            for (int i = 1; i <= 9; i++)
            {
                var random = new Random().Next(i, 9);
                if (board.TryGetValue(random, out string s))
                {
                    if (s.Equals(string.Empty))
                    {
                        board[random] = playerComputer.ToString();
                        _logger.LogTrace($"Square={random}");
                        return(random);
                    }
                }
            }

            if (board.Select(x => x.Value == string.Empty).Where(x => x == true).Any())
            {
                throw new Exception("Board SetPosition failed.");
            }
            else
            {
                return((int)GameStatus.GameDraw);
            }
        }
Exemplo n.º 2
0
        public int SetPosition(PlayerEnum.Player playerComputer, Dictionary <int, string> board, IEnumerable <IComputerMove> fallback)
        {
            /* BOARD SQUARES
             * 1, 2, 3
             * 4, 5, 6
             * 7, 8, 9
             */

            // high value board squares that allow x number of rows through/from them to win
            // 5       - it allows for 4 rows
            // 1,3,7,9 - they allow for 3 rows
            // 2,4,6,8 - that remain only allow for 2 rows

            var highValueSquareLists = new List <List <int> >()
            {
                new List <int>()
                {
                    5
                },
                new List <int>()
                {
                    1, 3, 7, 9
                },
                new List <int>()
                {
                    2, 4, 6, 8
                }
            };

            foreach (var highValueSquares in highValueSquareLists)
            {
                var randomHighValueSquares = highValueSquares.OrderBy(a => new Random().Next()).ToList();
                foreach (var square in randomHighValueSquares)
                {
                    if (board.TryGetValue(square, out string squareValue))
                    {
                        if (squareValue.Equals(string.Empty))
                        {
                            board[square] = playerComputer.ToString();
                            _logger.LogTrace($"Square={square}");
                            return(square);
                        }
                    }
                }
            }

            // no potential winning rows, fallback to easy
            return(fallback
                   .First(rule => rule
                          .IsMatch(ComputerLevel.Easy))
                   .SetPosition(playerComputer, board, fallback));
        }