Ejemplo n.º 1
0
 public Guess MakeGuess(PegCombination p_combination)
 {
     _guess[_currentguess] = new Guess(p_combination);
     _guess[_currentguess].Compare(_secret);
     _currentguess++;
     return(_guess[_currentguess - 1]);
 }
Ejemplo n.º 2
0
        public      PegCompareResult[] CompareTo(PegCombination p_otherCombination)
        {
            PegCombination otherCombination = new PegCombination(p_otherCombination);

            PegCompareResult[] result = new PegCompareResult[_numberofpegs];

            for (int i = 0; i < _numberofpegs; i++)
            {
                if (this._color[i] == otherCombination._color[i])
                {
                    result[i] = PegCompareResult.MatchColorAndPosition;
                    otherCombination._color[i] = PegColors.Unassigned;
                }
                else
                {
                    result[i] = PegCompareResult.NoMatch;
                }
            }

            for (int i = 0; i < _numberofpegs; i++)
            {
                if (otherCombination.ContainsColor(this._color[i]))
                {
                    result[i] = PegCompareResult.MatchColor;
                    otherCombination.RemoveFirstOfColor(this._color[i]);
                }
            }

            return(result);
        }
Ejemplo n.º 3
0
 public PegCombination(PegCombination p_combination)
 {
     _numberofpegs = p_combination._numberofpegs;
     _color        = new PegColors[_numberofpegs];
     for (int i = 0; i < _numberofpegs; i++)
     {
         _color[i] = p_combination._color[i];
     }
 }
Ejemplo n.º 4
0
 public Guess(PegCombination p_combination)
 {
     _combination = new PegCombination(p_combination._color.Length);
     for (int i = 0; i < _combination._color.Length; i++)
     {
         _combination._color[i] = p_combination._color[i];
     }
     _colorMatches    = 0;
     _positionMatches = 0;
 }
Ejemplo n.º 5
0
 public Game(int p_numberOfPegs, int p_numberOfGuesses, bool p_allowMultiple)
 {
     _numberofpegs    = p_numberOfPegs;
     _numberofguesses = p_numberOfGuesses;
     _allowMultiple   = p_allowMultiple;
     _currentguess    = 0;
     _secret          = new PegCombination(_numberofpegs);
     _secret.GenerateRandom(_allowMultiple);
     _guess = new Guess[_numberofguesses];
 }
Ejemplo n.º 6
0
        public bool IsPlausibleFor(PegCombination p_combination)
        {
            int colorMatchesLeft    = this._colorMatches;
            int positionMatchesLeft = this._positionMatches;

            for (int i = 0; i < _result.Length; i++)
            {
                if (_combination.ContainsColor(p_combination._color[i]))
                {
                    if (_combination._color[i] == p_combination._color[i])
                    {
                        if (positionMatchesLeft > 0)
                        {
                            positionMatchesLeft--;
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        if (colorMatchesLeft > 0)
                        {
                            colorMatchesLeft--;
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }
            }
            if (colorMatchesLeft > 0)
            {
                return(false);
            }
            if (positionMatchesLeft > 0)
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 7
0
        public void Compare(PegCombination p_combination)
        {
            _result = _combination.CompareTo(p_combination);

            _colorMatches    = 0;
            _positionMatches = 0;

            for (int i = 0; i < _result.Length; i++)
            {
                switch (_result[i])
                {
                case PegCompareResult.MatchColor:
                    _colorMatches++;
                    break;

                case PegCompareResult.MatchColorAndPosition:
                    _positionMatches++;
                    break;
                }
            }
        }
Ejemplo n.º 8
0
 public bool IsEqual(PegCombination othercombination)
 {
     return(this == othercombination);
 }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            int            currentpeg;
            PegCombination _editorCombination;
            Game           _mygame;
            bool           abort = false;

            const int topOffset = 5;


            void DrawCurrentGuessPeg()
            {
                Draw.drawrect(PegCombination.GetColor(_editorCombination._color[currentpeg]), 4 + (currentpeg * 3), topOffset + 24 - _mygame._currentguess * 2, 2, 1);
            }

            void DrawCombination(PegCombination p_combination, int x, int y)
            {
                // Console.SetCursorPosition(19 + (0 * 2), 24 - _mygame._currentguess * 2);
                Console.BackgroundColor = Color.Gray;
                Console.SetCursorPosition(x, y);
                for (int i = 0; i < p_combination._color.Length; i++)
                {
                    if (p_combination._color[i] == PegColors.Unassigned)
                    {
                        Console.ForegroundColor = Color.Red;
                        Console.Write("XX ");
                    }
                    else
                    {
                        Console.ForegroundColor = PegCombination.GetColor(p_combination._color[i]);
                        Console.Write(string.Format("{0}{0} ", Draw.Get8BitChar(219)), PegCombination.GetColor(p_combination._color[i]));
                    }
                }
            }

            void DrawGuessResult(Guess p_guess, int x, int y)
            {
                //Console.SetCursorPosition(19 + (0 * 2), 24 - _mygame._currentguess * 2);
                Console.BackgroundColor = Color.Gray;
                Console.SetCursorPosition(x, y);
                for (int i = 0; i < p_guess._positionMatches; i++)
                {
                    Console.ForegroundColor = Color.White;
                    Console.Write(Draw.Get8BitChar(220) + " ");
                }

                for (int i = 0; i < p_guess._colorMatches; i++)
                {
                    Console.ForegroundColor = Color.Black;
                    Console.Write(Draw.Get8BitChar(220) + " ");
                }
            }

            void CheckPlausibilities()
            {
                if (_editorCombination.IsValid)
                {
                    for (int i = 0; i < _mygame._currentguess; i++)
                    {
                        Console.SetCursorPosition(37, topOffset + 24 - i * 2);
                        Console.BackgroundColor = Color.Gray;

                        if (_mygame._guess[i].IsPlausibleFor(_editorCombination))
                        {
                            Console.ForegroundColor = Color.Green;
                            Console.Write("OK  ");
                        }
                        else
                        {
                            Console.ForegroundColor = Color.Red;
                            Console.Write("FAIL");
                        }
                    }
                }
            }

            void RemovePegIndicator()
            {
                Console.BackgroundColor = Color.Gray;
                Console.ForegroundColor = Color.Black;

                Draw.drawrect(Color.Gray, 3, topOffset + 24 - (_mygame._currentguess * 2) + 1, 13, 1);
            }

            void IndicatePeg()
            {
                RemovePegIndicator();

                Console.BackgroundColor = Color.Gray;
                Console.ForegroundColor = Color.Black;

                Console.SetCursorPosition(4 + currentpeg * 3, topOffset + 24 - (_mygame._currentguess * 2) + 1);
                Console.Write("^");
            }

            void IndicateGuess()
            {
                Draw.drawrect(Color.Black, 0, topOffset + 0, 3, 26);

                Console.SetCursorPosition(0, topOffset + 24 - (_mygame._currentguess * 2));
                Console.BackgroundColor = Color.Black;
                Console.ForegroundColor = Color.White;
                Console.Write(">");
            }

            void NextPeg()
            {
                if (currentpeg == (numberofpegs - 1))
                {
                    currentpeg = 0;
                }
                else
                {
                    currentpeg++;
                }
                IndicatePeg();
            }

            void PrevPeg()
            {
                if (currentpeg == 0)
                {
                    currentpeg = (numberofpegs - 1);
                }
                else
                {
                    currentpeg--;
                }
                IndicatePeg();
            }

            void ClearPlausibility()
            {
                Draw.drawrect(Color.Gray, 34, topOffset + 4, 10, 22);
            }

            void HideSecret()
            {
                Draw.drawrect(Color.Gray, 3, topOffset + 0, 16, 3);
            }

            void RevealSecret()
            {
                DrawCombination(_mygame._secret, 4, topOffset + 1);
            }

            void Init()
            {
                Console.Title = "MasterMind Trainer 1.0";

                Console.SetWindowSize(78, 40);
                Console.SetBufferSize(78, 40);
                Console.BackgroundColor = Color.Black;
                Console.Clear();

                Console.SetCursorPosition(0, 0);
                Console.WriteAscii("MASTERMIND", Color.Red);



                // Secret bar
                Draw.drawrect(Color.Gray, 3, topOffset + 0, 16, 3);

                // Guess indicator lane
                Draw.drawrect(Color.Black, 0, topOffset + 4, 3, 26);

                // Guess lane
                Draw.drawrect(Color.Gray, 3, topOffset + 4, 16, 22);

                // Answer lane
                Draw.drawrect(Color.Gray, 21, topOffset + 4, 11, 22);

                // Hint lane
                Draw.drawrect(Color.Gray, 34, topOffset + 4, 10, 22);

                // Help lane
                DrawHelp(46, topOffset + 4);

                _editorCombination = new PegCombination(numberofpegs);
                _mygame            = new Game(numberofpegs, numberofguesses, false);

                currentpeg = 0;

                _editorCombination.Reset();

                // DrawCombination(_mygame._secret, 4, 1);
                DrawCombination(_editorCombination, 4, topOffset + 24 - _mygame._currentguess * 2);

                IndicateGuess();
                IndicatePeg();
            }

            Init();


            while (!abort)
            {
                ConsoleKeyInfo key = Console.ReadKey(true);

                switch (key.Key)
                {
                case ConsoleKey.Escape:
                    return;

                case ConsoleKey.F1:
                    RevealSecret();
                    break;

                case ConsoleKey.F2:
                    HideSecret();
                    break;

                case ConsoleKey.F4:
                    numberofpegs = 4;
                    Init();
                    break;

                case ConsoleKey.F5:
                    numberofpegs = 5;
                    Init();
                    break;

                case ConsoleKey.LeftArrow:
                    PrevPeg();
                    break;

                case ConsoleKey.RightArrow:
                    NextPeg();
                    break;

                case ConsoleKey.D1:
                    _editorCombination.SetPeg(currentpeg, PegColors.Black);
                    DrawCurrentGuessPeg();
                    CheckPlausibilities();
                    NextPeg();
                    break;

                case ConsoleKey.D2:
                    _editorCombination.SetPeg(currentpeg, PegColors.White);
                    DrawCurrentGuessPeg();
                    CheckPlausibilities();
                    NextPeg();
                    break;

                case ConsoleKey.D3:
                    _editorCombination.SetPeg(currentpeg, PegColors.Red);
                    DrawCurrentGuessPeg();
                    CheckPlausibilities();
                    NextPeg();
                    break;

                case ConsoleKey.D4:
                    _editorCombination.SetPeg(currentpeg, PegColors.Green);
                    DrawCurrentGuessPeg();
                    CheckPlausibilities();
                    NextPeg();
                    break;

                case ConsoleKey.D5:
                    _editorCombination.SetPeg(currentpeg, PegColors.Blue);
                    DrawCurrentGuessPeg();
                    CheckPlausibilities();
                    NextPeg();
                    break;

                case ConsoleKey.D6:
                    _editorCombination.SetPeg(currentpeg, PegColors.Yellow);
                    DrawCurrentGuessPeg();
                    CheckPlausibilities();
                    NextPeg();
                    break;

                case ConsoleKey.D7:
                    _editorCombination.SetPeg(currentpeg, PegColors.Orange);
                    DrawCurrentGuessPeg();
                    CheckPlausibilities();
                    NextPeg();
                    break;

                case ConsoleKey.D8:
                    _editorCombination.SetPeg(currentpeg, PegColors.Brown);
                    DrawCurrentGuessPeg();
                    CheckPlausibilities();
                    NextPeg();
                    break;

                case ConsoleKey.Enter:
                    if (_mygame._secret == _editorCombination)
                    {
                        RevealSecret();

                        Console.SetCursorPosition(0, topOffset + 27);
                        Console.ForegroundColor = Color.Red;
                        Console.BackgroundColor = Color.Black;
                        Console.WriteLine(" You're a winner!");

                        if (nameofplayer == "")
                        {
                            Console.Write(" Whats your name? ");
                            string name = Console.ReadLine();
                            if (name.Trim() != "")
                            {
                                nameofplayer = name.Trim();
                            }
                        }

                        Console.WriteLine("Press <Enter> to play again.");
                        Console.ReadLine();
                        Init();
                    }
                    else
                    {
                        if (_editorCombination.IsValid)
                        {
                            RemovePegIndicator();
                            DrawGuessResult(_mygame.MakeGuess(_editorCombination), 22, topOffset + 24 - (_mygame._currentguess - 1) * 2);

                            ClearPlausibility();

                            if (_mygame._currentguess < _mygame._numberofguesses)
                            {
                                currentpeg = 0;
                                IndicateGuess();
                                IndicatePeg();

                                _editorCombination.Reset();
                                DrawCombination(_editorCombination, 4, topOffset + 24 - _mygame._currentguess * 2);
                            }
                            else
                            {
                                RevealSecret();
                                Console.SetCursorPosition(0, topOffset + 27);
                                Console.ForegroundColor = Color.Red;
                                Console.BackgroundColor = Color.Black;
                                Console.Write(" You're a loser.... Press <Enter> to try again.");
                                Console.ReadLine();
                                Init();
                            }
                        }
                    }
                    break;
                }

                if ((key.Key == ConsoleKey.C) && (key.Modifiers == ConsoleModifiers.Control))
                {
                    abort = true;
                }
            }

            Console.ReadLine();
        }