Example #1
0
        /// <summary>
        /// Calculates how many lines can the player using the given mark win according to the simulated chessboard state.
        /// </summary>
        /// <param name="filledChessboard">An instance of <see cref="Chessboard" /> with no blank cells.</param>
        /// <param name="mark">The chess piece mark of the player.</param>
        /// <returns>The number of lines for the player to win.</returns>
        /// <remarks>
        /// The "player" here doesn't mean the human player of this game, it only means either AI or the player of this game.
        /// </remarks>
        private static int CalculateScore(Chessboard filledChessboard, ChessPiece mark)
        {
            // Initialize the score to 0, increase it every time a possible line is found.
            int score = 0;

            // A boolean mark indicating whether the player wins.
            bool add;

            // Score of rows.
            for (int i = 0; i < Chessboard.Size; i++)
            {
                add = true;
                for (int j = 0; j < Chessboard.Size; j++)
                {
                    if (filledChessboard[i, j] != mark)
                    {
                        add = false;
                        break;
                    }
                }

                if (add)
                {
                    score++;
                }
            }

            // Score of columns.
            for (int j = 0; j < Chessboard.Size; j++)
            {
                add = true;
                for (int i = 0; i < Chessboard.Size; i++)
                {
                    if (filledChessboard[i, j] != mark)
                    {
                        add = false;
                        break;
                    }
                }

                if (add)
                {
                    score++;
                }
            }

            // Main diagonal.
            add = true;
            for (int i = 0; i < Chessboard.Size; i++)
            {
                if (filledChessboard[i, i] != mark)
                {
                    add = false;
                    break;
                }
            }
            if (add)
            {
                score++;
            }

            // Vice diagonal.
            add = true;
            for (int i = 0; i < Chessboard.Size; i++)
            {
                if (filledChessboard[i, Chessboard.Size - 1 - i] != mark)
                {
                    add = false;
                    break;
                }
            }
            if (add)
            {
                score++;
            }

            return(score);
        }
Example #2
0
        /// <summary>
        /// Calculates how many lines can the player win according to the simulated chessboard state.
        /// </summary>
        /// <param name="state">The simulated chessboard state.</param>
        /// <param name="playerMark">Chess piece mark of the player to move.</param>
        /// <returns>The number of lines for the player to win.</returns>
        /// <remarks>
        /// Although this method has only 2 lines, this is implemented for the sake of readability.
        /// </remarks>
        private static int PlayerScore(Chessboard state, ChessPiece playerMark)
        {
            Chessboard filledChessboard = FillChessboard(state, playerMark);

            return(CalculateScore(filledChessboard, playerMark));
        }
Example #3
0
        /// <summary>
        /// Calculates how many lines can the opponent win according to the simulated chessboard state.
        /// </summary>
        /// <param name="state">The simulated chessboard state.</param>
        /// <param name="opponentMark">Chess piece mark of the opponent to move.</param>
        /// <returns>The number of lines for the opponent to win.</returns>
        /// <remarks>
        /// Although this method has only 2 lines, this is implemented for the sake of readability.
        /// </remarks>
        private static int OpponentScore(Chessboard state, ChessPiece opponentMark)
        {
            Chessboard filledChessboard = FillChessboard(state, opponentMark);

            return(CalculateScore(filledChessboard, opponentMark));
        }