Exemple #1
0
        internal int AddMove(string player, int column)
        {
            VerifyColumnExists(column);
            var row  = GetRowForMove(column);
            var move = new BoardMove(player, column, row);

            return(AddMove(move));
        }
Exemple #2
0
        private bool FourInARow_Row(BoardMove move)
        {
            int matchingPlayCount = 0;
            var startingColumn    = move.Column - (WinningMatches - 1) < 0 ? 0 : move.Column - (WinningMatches - 1);
            int endingColumn      = move.Column + (WinningMatches - 1) >= _columns ? _columns - 1 : move.Column + (WinningMatches - 1);

            for (var i = startingColumn; i <= endingColumn && matchingPlayCount < WinningMatches; i++)
            {
                if (IsPlayerInSpot(move.Player, i, move.Row))
                {
                    matchingPlayCount++;
                }
                else
                {
                    matchingPlayCount = 0;
                }
            }

            return(matchingPlayCount >= WinningMatches);
        }
Exemple #3
0
        private bool FourInARow_Column(BoardMove move)
        {
            int matchingPlayCount = 0;
            var startingRow       = move.Row - (WinningMatches - 1) < 0 ? 0 : move.Row - (WinningMatches - 1);
            int endingRow         = move.Row;

            for (var i = startingRow; i <= endingRow && matchingPlayCount < WinningMatches; i++)
            {
                if (IsPlayerInSpot(move.Player, move.Column, i))
                {
                    matchingPlayCount++;
                }
                else
                {
                    matchingPlayCount = 0;
                }
            }

            return(matchingPlayCount >= WinningMatches);
        }
Exemple #4
0
        private bool FourInARow_DescreasingDiag(BoardMove move)
        {
            int matchingPlayCount = 1;
            var currentRow        = move.Row + 1;
            var currentCol        = move.Column - 1;

            while (currentRow < _rows && currentCol >= 0 && matchingPlayCount < WinningMatches)
            {
                if (IsPlayerInSpot(move.Player, currentCol, currentRow))
                {
                    matchingPlayCount++;
                }
                else
                {
                    break;
                }
                currentRow++;
                currentCol--;
            }

            currentRow = move.Row - 1;
            currentCol = move.Column + 1;

            while (currentRow >= 0 && currentCol < _columns && matchingPlayCount < WinningMatches)
            {
                if (IsPlayerInSpot(move.Player, currentCol, currentRow))
                {
                    matchingPlayCount++;
                }
                else
                {
                    break;
                }
                currentRow--;
                currentCol++;
            }
            return(matchingPlayCount >= WinningMatches);
        }
Exemple #5
0
        private bool IsGameWon(BoardMove move)
        {
            if (FourInARow_Row(move))
            {
                return(true);
            }

            if (FourInARow_Column(move))
            {
                return(true);
            }

            if (FourInARow_IncreasingDiag(move))
            {
                return(true);
            }

            if (FourInARow_DescreasingDiag(move))
            {
                return(true);
            }

            return(false);
        }