public void AddDisc(DiscPosition discPosition, Board board, out bool isLegalPlay)
        {
            isLegalPlay = true;

            //Outside the board
            if (discPosition.X > board.BoundaryX ||
                discPosition.Y > board.BoundaryY ||
                discPosition.X < 1 ||
                discPosition.Y < 1)
            {
                isLegalPlay = false;
            }

            if (board.DiscsOnBoard == null)
            {
                board.DiscsOnBoard = new List <DiscPosition>();
            }

            //On occupied position
            if (board.DiscsOnBoard.Exists(p =>
                                          p.X.Equals(discPosition.X) && p.Y.Equals(discPosition.Y)
                                          ))
            {
                isLegalPlay = false;
            }

            board.DiscsOnBoard.Add(discPosition);
        }
Exemple #2
0
        public DiscPosition Play(Board board)
        {
            var possiblePosition = GetPossibleDiscPosition(board.DiscsOnBoard, board.BoundaryX, board.BoundaryY);

            var myMove = new DiscPosition
            {
                X = possiblePosition.X,
                Y = possiblePosition.Y
            };

            return(myMove);
        }
        public void ShouldStateIllegalPlayIfOutsideBoard()
        {
            var disc = new DiscPosition {
                PlayerInitialLetter = 'T', X = 0, Y = 0
            };
            var board = new Board {
                BoundaryX = 20, BoundaryY = 20
            };
            bool isLegalPlay = true;

            _gameManager.AddDisc(disc, board, out isLegalPlay);

            Assert.IsFalse(isLegalPlay);
        }
        public void ShouldNotThrowExceptionIfDiscsOnBoardIsNull()
        {
            var disc = new DiscPosition {
                PlayerInitialLetter = 'T', X = 15, Y = 10
            };
            var board = new Board
            {
                BoundaryX = 20,
                BoundaryY = 20
            };

            bool isLegalPlay = true;

            Assert.DoesNotThrow(() => _gameManager.AddDisc(disc, board, out isLegalPlay));
        }
Exemple #5
0
        private DiscPosition GetPossibleDiscPosition(IList <DiscPosition> occupiedPositions, int boundaryX, int boundaryY)
        {
            var suggestion       = new DiscPosition();
            var positionComparer = new PositionComparer();
            var maxIterations    = boundaryX * boundaryY;

            do
            {
                suggestion = new DiscPosition
                {
                    X = Randomizer.Next(1, boundaryX + 1),
                    Y = Randomizer.Next(1, boundaryY + 1)
                };

                maxIterations--;
            }while (occupiedPositions.Contains(suggestion, positionComparer) &&
                    maxIterations >= 0);

            return(suggestion);
        }
        public void ShouldStateIllegalPlayIfAddingOnOccupiedPosition()
        {
            var disc = new DiscPosition {
                PlayerInitialLetter = 'T', X = 15, Y = 10
            };
            var board = new Board
            {
                BoundaryX    = 20,
                BoundaryY    = 20,
                DiscsOnBoard = new List <DiscPosition>
                {
                    new DiscPosition {
                        PlayerInitialLetter = 'T', X = 15, Y = 10
                    }
                }
            };
            bool isLegalPlay = true;

            _gameManager.AddDisc(disc, board, out isLegalPlay);

            Assert.IsFalse(isLegalPlay);
        }
        public bool IsWinner(List <DiscPosition> playerDiscs, out List <DiscPosition> winningCombination)
        {
            winningCombination = new List <DiscPosition>();

            if (playerDiscs.Count < 5)
            {
                return(false);
            }

            foreach (var currentDisc in playerDiscs)
            {
                winningCombination.Clear();
                winningCombination.Add(currentDisc);

                //Hitta 5 följande discar
                for (int j = 1; j <= 5; j++)
                {
                    DiscPosition disc     = currentDisc;
                    var          nextDisc = playerDiscs.FirstOrDefault(d => d.X.Equals(disc.X + j) && d.Y.Equals(disc.Y + j));
                    if (nextDisc != null)
                    {
                        winningCombination.Add(nextDisc);
                    }
                    else
                    {
                        break;
                    }
                }

                if (winningCombination.Count == 5)
                {
                    break;
                }
            }

            return(winningCombination.Count == 5);
        }