Exemplo n.º 1
0
        /// <summary>
        /// Counts the number of pieces capturable by specified piece.
        /// </summary>
        /// <param name="piece">The piece which can capture another ones.</param>
        /// <returns>The number of capturable pieces.</returns>
        public int CountCapturableByPiece(CheckersPiece piece)
        {
            ExceptionHelper.ThrowIfNull("piece", piece);

            var capturableCount = 0;
            var square          = piece.CurrentSquare;
            var newRowIndex     = square.RowIndex + 2 * GetIndexModifier(piece.Player);

            if (IsValidIndex(newRowIndex))
            {
                var columnIndex           = square.ColumnIndex;
                var newColumnToLeftIndex  = columnIndex - 2;
                var newColumnToRightIndex = columnIndex + 2;

                if (IsValidIndex(newColumnToLeftIndex))
                {
                    capturableCount += GetMoveKind(new CheckersMove(piece, GetSquare(newColumnToLeftIndex, newRowIndex))) == CheckersMoveKind.Capture ? 1 : 0;
                }

                if (IsValidIndex(newColumnToRightIndex))
                {
                    capturableCount += GetMoveKind(new CheckersMove(piece, GetSquare(newColumnToRightIndex, newRowIndex))) == CheckersMoveKind.Capture ? 1 : 0;
                }
            }

            return(capturableCount);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reset the board to initial state (player one and two with pieces in start positions).
        /// </summary>
        public void Reset()
        {
            PlayerOnePieces = new List <CheckersPiece> ();
            PlayerTwoPieces = new List <CheckersPiece> ();

            for (int c = 0; c < Size; c++)
            {
                for (int r = 0; r < Size; r++)
                {
                    var square = new CheckersSquare(c, r);

                    if (square.State == CheckersSquareState.Free)
                    {
                        if (r < 3)
                        {
                            var piece = new CheckersPiece(CheckersPlayer.PlayerOne);
                            PlayerOnePieces.Add(piece);
                            square.PutPiece(piece);
                        }
                        else if (r >= Size - 3)
                        {
                            var piece = new CheckersPiece(CheckersPlayer.PlayerTwo);
                            PlayerTwoPieces.Add(piece);
                            square.PutPiece(piece);
                        }
                    }

                    m_squares[c, r] = square;
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Counts the number of chances the specified piece to be captured.
        /// </summary>
        /// <param name="piece">The piece which can be captured.</param>
        /// <returns>The number of changes of piece be captured.</returns>
        public int CountPieceChancesToBeCaptured(CheckersPiece piece)
        {
            ExceptionHelper.ThrowIfNull("piece", piece);

            var capturedCount = 0;
            var square        = piece.CurrentSquare;

            var indexModifier         = GetIndexModifier(piece.Player);
            var enemyPieceRowIndex    = square.RowIndex + indexModifier;
            var enemyToSquareRowIndex = square.RowIndex - indexModifier;

            if (IsValidIndex(enemyPieceRowIndex) && IsValidIndex(enemyToSquareRowIndex))
            {
                var columnIndex           = square.ColumnIndex;
                var enemyLeftColumnIndex  = columnIndex - 1;
                var enemyRightColumnIndex = columnIndex + 1;

                if (IsValidIndex(enemyLeftColumnIndex) && IsValidIndex(enemyRightColumnIndex))
                {
                    capturedCount += CountIfEnemyPiece(enemyPieceRowIndex, enemyToSquareRowIndex, enemyLeftColumnIndex, enemyRightColumnIndex);
                    capturedCount += CountIfEnemyPiece(enemyPieceRowIndex, enemyToSquareRowIndex, enemyRightColumnIndex, enemyLeftColumnIndex);
                }
            }

            return(capturedCount);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GeneticSharp.Extensions.Checkers.CheckersMove"/> class.
        /// </summary>
        /// <param name="piece">The piece which will be moved.</param>
        /// <param name="toSquare">The target square.</param>
        public CheckersMove(CheckersPiece piece, CheckersSquare toSquare)
        {
            ExceptionHelper.ThrowIfNull("piece", piece);

            if (piece.CurrentSquare == null)
            {
                throw new ArgumentException("A piece for a move should have a current square defined.");
            }

            ExceptionHelper.ThrowIfNull("toSquare", toSquare);

            Piece    = piece;
            ToSquare = toSquare;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GeneticSharp.Extensions.Checkers.CheckersMove"/> class.
        /// </summary>
        /// <param name="piece">The piece which will be moved.</param>
        /// <param name="toSquare">The target square.</param>
        public CheckersMove(CheckersPiece piece, CheckersSquare toSquare)
        {
            ExceptionHelper.ThrowIfNull("piece", piece);

            if (piece.CurrentSquare == null)
            {
                throw new ArgumentException("A piece for a move should have a current square defined.");
            }

            ExceptionHelper.ThrowIfNull("toSquare", toSquare);

            Piece = piece;
            ToSquare = toSquare;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Put the specified piece above this square.
        /// </summary>
        /// <param name="piece">The piece.</param>
        /// <returns>True if square was free and could receive the piece, otherwise false.</returns>
        public bool PutPiece(CheckersPiece piece)
        {
            if (State == CheckersSquareState.Free)
            {
                CurrentPiece        = piece;
                State               = piece.Player == CheckersPlayer.PlayerOne ? CheckersSquareState.OccupiedByPlayerOne : CheckersSquareState.OccupiedByPlayerTwo;
                piece.CurrentSquare = this;
                return(true);
            }
            else if (State == CheckersSquareState.NotPlayable)
            {
                throw new ArgumentException("Attempt to put a piece in a not playable square.");
            }

            return(false);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Remove the current piece.
        /// </summary>
        /// <returns>True if has a piece to be removed, otherwise false.</returns>
        public bool RemovePiece()
        {
            if (CurrentPiece != null)
            {
                if (CurrentPiece.CurrentSquare == this)
                {
                    CurrentPiece.CurrentSquare = null;
                }

                CurrentPiece = null;
                State        = CheckersSquareState.Free;

                return(true);
            }

            return(false);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Evaluates the move.
        /// </summary>
        /// <returns>The move.</returns>
        /// <param name="move">Move.</param>
        public double EvaluateMove(CheckersMove move)
        {
            double moveFitness = 0;

            // Evals the move kind.
            var moveKind = Board.GetMoveKind(move);

            switch (moveKind)
            {
            case CheckersMoveKind.Forward:
                moveFitness = 0.5;
                break;

            case CheckersMoveKind.Capture:
                moveFitness = 1;
                break;

            case CheckersMoveKind.Invalid:
                moveFitness = 0;
                break;
            }

            if (moveFitness > 0)
            {
                var futurePiece = new CheckersPiece(move.Piece.Player)
                {
                    CurrentSquare = move.ToSquare
                };

                // Evals the possibilities to capture anothers pieces.
                moveFitness += Board.CountCapturableByPiece(move.Piece);

                // Evals the possibilities to be captured by another pieces.
                moveFitness -= Board.CountPieceChancesToBeCaptured(futurePiece);
            }


            return(moveFitness);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Reset the board to initial state (player one and two with pieces in start positions).
        /// </summary>
        public void Reset()
        {
            // Creates the two lists of pieces por player one and two.ß
            PlayerOnePieces = new List <CheckersPiece>();
            PlayerTwoPieces = new List <CheckersPiece>();

            for (int c = 0; c < Size; c++)
            {
                for (int r = 0; r < Size; r++)
                {
                    // For each combinatino of collumn and row of the board
                    // Is create a new CheckersSquare.
                    var square = new CheckersSquare(c, r);

                    // If the sqaure is free.
                    if (square.State == CheckersSquareState.Free)
                    {
                        // If the actual line index is lower than 3,
                        // then is a square for player one.
                        if (r < 3)
                        {
                            var piece = new CheckersPiece(CheckersPlayer.PlayerOne);
                            PlayerOnePieces.Add(piece);
                            square.PutPiece(piece);
                        }
                        /// fi the actual line index is bigger than max lines index -3,
                        /// then it is a square for player two.
                        else if (r >= Size - 3)
                        {
                            var piece = new CheckersPiece(CheckersPlayer.PlayerTwo);
                            PlayerTwoPieces.Add(piece);
                            square.PutPiece(piece);
                        }
                    }

                    m_squares[c, r] = square;
                }
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Counts the number of chances the specified piece to be captured.
        /// </summary>
        /// <param name="piece">The piece which can be captured.</param>
        /// <returns>The number of changes of piece be captured.</returns>
        public int CountPieceChancesToBeCaptured(CheckersPiece piece)
        {
            ExceptionHelper.ThrowIfNull("piece", piece);

            var capturedCount = 0;
            var square        = piece.CurrentSquare;

            var indexModifier         = GetIndexModifier(piece.Player);
            var enemyPieceRowIndex    = square.RowIndex + 1 * indexModifier;
            var enemyToSquareRowIndex = square.RowIndex - 1 * indexModifier;

            if (IsValidIndex(enemyPieceRowIndex) && IsValidIndex(enemyToSquareRowIndex))
            {
                var columnIndex           = square.ColumnIndex;
                var enemyLeftColumnIndex  = columnIndex - 1;
                var enemyRightColumnIndex = columnIndex + 1;

                if (IsValidIndex(enemyLeftColumnIndex) && IsValidIndex(enemyRightColumnIndex))
                {
                    var enemyPiece = GetSquare(enemyLeftColumnIndex, enemyPieceRowIndex).CurrentPiece;

                    if (enemyPiece != null)
                    {
                        capturedCount += GetMoveKind(new CheckersMove(enemyPiece, GetSquare(enemyRightColumnIndex, enemyToSquareRowIndex))) == CheckersMoveKind.Capture ? 1 : 0;
                    }

                    enemyPiece = GetSquare(enemyRightColumnIndex, enemyPieceRowIndex).CurrentPiece;

                    if (enemyPiece != null)
                    {
                        capturedCount += GetMoveKind(new CheckersMove(enemyPiece, GetSquare(enemyLeftColumnIndex, enemyToSquareRowIndex))) == CheckersMoveKind.Capture ? 1 : 0;
                    }
                }
            }

            return(capturedCount);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Put the specified piece above this square.
        /// </summary>
        /// <param name="piece">The piece.</param>
        /// <returns>True if square was free and could receive the piece, otherwise false.</returns>
        public bool PutPiece(CheckersPiece piece)
        {
            if (State == CheckersSquareState.Free)
            {
                CurrentPiece = piece;
                State = piece.Player == CheckersPlayer.PlayerOne ? CheckersSquareState.OccupiedByPlayerOne : CheckersSquareState.OccupiedByPlayerTwo;
                piece.CurrentSquare = this;
                return true;
            }
            else if (State == CheckersSquareState.NotPlayable)
            {
                throw new ArgumentException("Attempt to put a piece in a not playable square.");
            }

            return false;
        }
Exemplo n.º 12
0
        /// <summary>
        /// Reset the board to initial state (player one and two with pieces in start positions).
        /// </summary>
        public void Reset()
        {
            PlayerOnePieces = new List<CheckersPiece>();
            PlayerTwoPieces = new List<CheckersPiece>();

            for (int c = 0; c < Size; c++)
            {
                for (int r = 0; r < Size; r++)
                {
                    var square = new CheckersSquare(c, r);

                    if (square.State == CheckersSquareState.Free)
                    {
                        if (r < 3)
                        {
                            var piece = new CheckersPiece(CheckersPlayer.PlayerOne);
                            PlayerOnePieces.Add(piece);
                            square.PutPiece(piece);
                        }
                        else if (r >= Size - 3)
                        {
                            var piece = new CheckersPiece(CheckersPlayer.PlayerTwo);
                            PlayerTwoPieces.Add(piece);
                            square.PutPiece(piece);
                        }
                    }

                    m_squares[c, r] = square;
                }
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Counts the number of chances the specified piece to be captured.
        /// </summary>
        /// <param name="piece">The piece which can be captured.</param>
        /// <returns>The number of changes of piece be captured.</returns>
        public int CountPieceChancesToBeCaptured(CheckersPiece piece)
        {
            ExceptionHelper.ThrowIfNull("piece", piece);

            var capturedCount = 0;
            var square = piece.CurrentSquare;

            var indexModifier = GetIndexModifier(piece.Player);
            var enemyPieceRowIndex = square.RowIndex + indexModifier;
            var enemyToSquareRowIndex = square.RowIndex - indexModifier;

            if (IsValidIndex(enemyPieceRowIndex) && IsValidIndex(enemyToSquareRowIndex))
            {
                var columnIndex = square.ColumnIndex;
                var enemyLeftColumnIndex = columnIndex - 1;
                var enemyRightColumnIndex = columnIndex + 1;

                if (IsValidIndex(enemyLeftColumnIndex) && IsValidIndex(enemyRightColumnIndex))
                {
                    var enemyPiece = GetSquare(enemyLeftColumnIndex, enemyPieceRowIndex).CurrentPiece;

                    if (enemyPiece != null)
                    {
                        capturedCount += GetMoveKind(new CheckersMove(enemyPiece, GetSquare(enemyRightColumnIndex, enemyToSquareRowIndex))) == CheckersMoveKind.Capture ? 1 : 0;
                    }

                    enemyPiece = GetSquare(enemyRightColumnIndex, enemyPieceRowIndex).CurrentPiece;

                    if (enemyPiece != null)
                    {
                        capturedCount += GetMoveKind(new CheckersMove(enemyPiece, GetSquare(enemyLeftColumnIndex, enemyToSquareRowIndex))) == CheckersMoveKind.Capture ? 1 : 0;
                    }
                }
            }

            return capturedCount;
        }
Exemplo n.º 14
0
        /// <summary>
        /// Counts the number of pieces catchable by specified piece.
        /// </summary>
        /// <param name="piece">The piece which can capture another ones.</param>
        /// <returns>The number of catchable pieces.</returns>
        public int CountCatchableByPiece(CheckersPiece piece)
        {
            ExceptionHelper.ThrowIfNull("piece", piece);

            var capturableCount = 0;
            var square = piece.CurrentSquare;
            var newRowIndex = square.RowIndex + (2 * GetIndexModifier(piece.Player));

            if (IsValidIndex(newRowIndex))
            {
                var columnIndex = square.ColumnIndex;
                var newColumnToLeftIndex = columnIndex - 2;
                var newColumnToRightIndex = columnIndex + 2;

                if (IsValidIndex(newColumnToLeftIndex))
                {
                    capturableCount += GetMoveKind(new CheckersMove(piece, GetSquare(newColumnToLeftIndex, newRowIndex))) == CheckersMoveKind.Capture ? 1 : 0;
                }

                if (IsValidIndex(newColumnToRightIndex))
                {
                    capturableCount += GetMoveKind(new CheckersMove(piece, GetSquare(newColumnToRightIndex, newRowIndex))) == CheckersMoveKind.Capture ? 1 : 0;
                }
            }

            return capturableCount;
        }
Exemplo n.º 15
0
        /// <summary>
        /// Evaluates the move.
        /// </summary>
        /// <returns>The move.</returns>
        /// <param name="move">Move.</param>		
        public double EvaluateMove(CheckersMove move)
        {
            double moveFitness = 0;

            // Evals the move kind.
            var moveKind = Board.GetMoveKind (move);

            switch (moveKind) {
                case CheckersMoveKind.Forward:
                    moveFitness = 0.5;
                    break;

                case CheckersMoveKind.Capture:
                    moveFitness = 1;
                    break;

                case CheckersMoveKind.Invalid:
                    moveFitness = 0;
                    break;
            }

            if (moveFitness > 0)
            {
                var futurePiece = new CheckersPiece(move.Piece.Player) { CurrentSquare = move.ToSquare };

                // Evals the possibilities to capture anothers pieces.
                moveFitness += Board.CountCapturableByPiece(move.Piece);

                // Evals the possibilities to be captured by another pieces.
                moveFitness -= Board.CountPieceChancesToBeCaptured(futurePiece);
            }

            return moveFitness;
        }
Exemplo n.º 16
0
        /// <summary>
        /// Remove the current piece.
        /// </summary>
        /// <returns>True if has a piece to be removed, otherwise false.</returns>
        public bool RemovePiece()
        {
            if (CurrentPiece != null)
            {
                if (CurrentPiece.CurrentSquare == this)
                {
                    CurrentPiece.CurrentSquare = null;
                }

                CurrentPiece = null;
                State = CheckersSquareState.Free;

                return true;
            }

            return false;
        }