コード例 #1
0
        /// <summary>
        /// Checks how many equal colors, withous repetitions, are in common with both rows
        /// </summary>
        /// <param name="row">The other row to compare</param>
        /// <returns>Number of similar colors, without repetitions</returns>
        internal int EqualColors(ColoredPegRow row)
        {
            if (this.NumberPegs != row.NumberPegs)
            {
                throw new MastermindColoredPegRowException("To compare objects, the number of pegs must be equal");
            }

            List <PegColor> foundColors = new List <PegColor>();

            for (int i = 0; i < this.NumberPegs; i++)
            {
                if (!foundColors.Contains(this.Pegs[i]))
                {
                    bool     newColor = false;
                    PegColor current  = this.Pegs[i];

                    foreach (PegColor color in row.Pegs)
                    {
                        if (current == color)
                        {
                            newColor = true;
                            break;
                        }
                    }

                    if (newColor)
                    {
                        foundColors.Add(current);
                    }
                }
            }

            return(foundColors.Count);
        }
コード例 #2
0
        /// <summary>
        /// Creates the combination to be broke by human player
        /// </summary>
        /// <param name="combination">An array containing the combination</param>
        /// <remarks>Once set, the combination cannot be changed</remarks>
        public void setup(ColoredPegRow combination)
        {
            if (this.Status != GameStatus.Setup)
            {
                throw new MastermindGameException("Setup can only be done during setup game status");
            }

            this.theBoard.setup(combination);
            this.Status = GameStatus.Ready;
        }
コード例 #3
0
        /// <summary>
        /// Checks how many pegs are in right positions (same color and same position)
        /// </summary>
        /// <param name="row">The other row to compare</param>
        /// <returns>Number of right pegs</returns>
        internal int EqualColorsAndPositions(ColoredPegRow row)
        {
            if (this.NumberPegs != row.NumberPegs)
            {
                throw new MastermindColoredPegRowException("To compare objects, the number of pegs must be equal");
            }

            int total = 0;

            for (int i = 0; i < this.NumberPegs; i++)
            {
                if (this.Pegs[i] == row.Pegs[i])
                {
                    total++;
                }
            }

            return(total);
        }
コード例 #4
0
        /// <summary>
        /// Try to guess a row of the board
        /// </summary>
        /// <param name="row">Row with pegs to break the combination</param>
        /// <returns>Result stats of the move</returns>
        public MoveResult doMove(ColoredPegRow row)
        {
            if (this.Status != GameStatus.Running)
            {
                throw new MastermindGameException("The game is not running");
            }

            MoveResult result = this.theBoard.doMove(row);

            // check for victory
            if (result.TotalRightColorAndPosition == this.theBoard.NumberPegs)
            {
                this.theWinner = this.Players[0];  // human player
                this.Status    = GameStatus.Ended;
            }
            else if (result.NoMoreMoves)
            {
                this.theWinner = this.Players[1];  // computer
                this.Status    = GameStatus.Ended;
            }

            return(result);
        }
コード例 #5
0
        /// <summary>
        /// Checks if two ColoredPegRow are equal
        /// </summary>
        /// <param name="obj">The other object to compare</param>
        /// <returns>True, if the rows are equal (peg colors and positions); false otherwise</returns>
        /// <remarks>This method always returns false if the other object is not a ColoredPegRow</remarks>
        public override bool Equals(object obj)
        {
            if (!(obj is ColoredPegRow))
            {
                return(false);
            }

            ColoredPegRow other = (ColoredPegRow)obj;

            if (other.NumberPegs != this.NumberPegs)
            {
                return(false);
            }

            for (int i = 0; i < this.NumberPegs; i++)
            {
                if (this.Pegs[i] != other.Pegs[i])
                {
                    return(false);
                }
            }

            return(true);
        }