Ejemplo n.º 1
0
        public Peg[] GetPegInput()
        {
            Peg[] pegs = new Peg[PegWidth];

            string input = Console.ReadLine().Trim();

            string[] segments = input.Split(' ');

            if (segments.Length < PegWidth)
            {
                Console.WriteLine("Not enough segments");
                GetNewAttempt();
                return(null);
            }

            for (int i = 0; i < PegWidth; i++)
            {
                if (int.TryParse(segments[i], out int k))
                {
                    pegs[i] = (Peg)k;
                }
                else
                {
                    Console.WriteLine($"Invalid Input \'{segments[i]}\'");
                    GetNewAttempt();
                    return(null);
                }
            }

            return(pegs);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets/Sets a <see cref="Peg"/> at the specified index.
        /// </summary>
        /// <param name="index">The index to access.</param>
        /// <returns>A <see cref="Peg"/> at the specified index.</returns>
        public     Peg this[int index]
        {
            get
            {
                switch (index)
                {
                case 0: return(A);

                case 1: return(B);

                case 2: return(C);

                case 3: return(D);

                default: throw new IndexOutOfRangeException("Invalid Code Index!");
                }
            }
            set
            {
                switch (index)
                {
                case 0: A = value; break;

                case 1: B = value; break;

                case 2: C = value; break;

                case 3: D = value; break;

                default: throw new IndexOutOfRangeException("Invalid Code Index!");
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Chnages the <see cref="Console.ForegroundColor"/> to the specified <see cref="Peg.Colour"/>.
        /// </summary>
        /// <param name="peg">The peg to change the <see cref="Console.ForegroundColor"/>.</param>
        private void ChangeConsoleColour(Peg peg)
        {
            switch (peg.Colour)
            {
            case PegColour.None: Console.ForegroundColor = ConsoleColor.DarkGray; break;

            case PegColour.Red: Console.ForegroundColor = ConsoleColor.Red; break;

            case PegColour.Green: Console.ForegroundColor = ConsoleColor.Green; break;

            case PegColour.Blue: Console.ForegroundColor = ConsoleColor.Blue; break;

            case PegColour.Yellow: Console.ForegroundColor = ConsoleColor.Yellow; break;

            case PegColour.Cyan: Console.ForegroundColor = ConsoleColor.Cyan; break;

            case PegColour.Purple: Console.ForegroundColor = ConsoleColor.Magenta; break;

            case PegColour.Black: Console.ForegroundColor = ConsoleColor.Gray; break;

            case PegColour.White: Console.ForegroundColor = ConsoleColor.White; break;

            default: Console.ForegroundColor = ConsoleColor.DarkGray; break;
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Constructs a <see cref="Code"/> that contains four code <see cref="Peg"/> instances.
 /// </summary>
 /// <param name="a"></param>
 /// <param name="b"></param>
 /// <param name="c"></param>
 /// <param name="d"></param>
 public Code(Peg a, Peg b, Peg c, Peg d)
 {
     A = a;
     B = b;
     C = c;
     D = d;
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Returns a <see cref="Peg"/> array with their <see cref="PegColour"/> set to
        /// <see cref="PegColour.Black"/> or <see cref="PegColour.White"/>
        /// depening on the number of <see cref="Blacks"/> and <see cref="Whites"/>.
        /// </summary>
        /// <returns>An array of <see cref="Peg"/> with their <see cref="Peg.Colour"/> set to
        /// <see cref="PegColour.Black"/> or <see cref="PegColour.White"/>.</returns>
        public     Peg[] ToPegArray()
        {
            Peg[] blacks = PopulateBlacks();
            Peg[] whites = PopulateWhites();
            Peg[] result = new Peg[4];

            if (Blacks != 0)
            {
                if (Whites != 0)
                {
                    Array.Copy(blacks, result, blacks.Length);
                    Array.Copy(whites, 0, result, blacks.Length, whites.Length);
                }
                else
                {
                    Array.Copy(blacks, result, blacks.Length);
                }
            }
            else
            {
                Array.Copy(whites, result, whites.Length);
            }

            return(result);
        }
Ejemplo n.º 6
0
 private static int CountOccurrencesOfPeg(Peg peg, Code code)
 {
     return(
         (peg == code.Pegs[0] ? 1 : 0) +
         (peg == code.Pegs[1] ? 1 : 0) +
         (peg == code.Pegs[2] ? 1 : 0) +
         (peg == code.Pegs[3] ? 1 : 0)
         );
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Returns an array of <see cref="Peg"/> with their <see cref="PegColour"/> set to <see cref="PegColour.Black"/>.
        /// </summary>
        /// <returns>An array of <see cref="Peg"/> with their <see cref="PegColour"/> set to <see cref="PegColour.Black"/>.</returns>
        private    Peg[] PopulateBlacks()
        {
            Peg[] blacks = new Peg[Blacks];

            for (int i = 0; i < Blacks; i++)
            {
                blacks[i] = new Peg(PegColour.Black);
            }

            return(blacks);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Returns an array of <see cref="Peg"/> with their <see cref="PegColour"/> set to <see cref="PegColour.White"/>.
        /// </summary>
        /// <returns>An array of <see cref="Peg"/> with their <see cref="PegColour"/> set to <see cref="PegColour.White"/>.</returns>
        private    Peg[] PopulateWhites()
        {
            Peg[] whites = new Peg[Whites];

            for (int i = 0; i < Whites; i++)
            {
                whites[i] = new Peg(PegColour.White);
            }

            return(whites);
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            bool   win        = false;
            string pegLetters = "abcdefgh";
            string answer     = "";
            Random random     = new Random();

            for (int i = 0; i < 4; i++)
            {
                int index = random.Next(0, 8);
                answer += pegLetters[index];
            }
            Console.WriteLine(answer);
            Game game = new Game(answer);

            for (int turns = 0; turns < 10; turns++)
            {
                Console.Write("Choose four letters: ");
                string letters = Console.ReadLine();
                Peg[]  pegs    = new Peg[4];
                for (int i = 0; i < pegs.Length; i++)
                {
                    pegs[i] = new Peg(letters[i]);
                }
                Row row1 = new Row(pegs);
                game.AddRow(row1);
                foreach (string row in game.Rows)
                {
                    if (row != null)
                    {
                        Console.WriteLine(row);
                    }
                    else
                    {
                        break;
                    }
                }
                if (game.Score(row1)[0] == 4)
                {
                    Console.WriteLine("You win!");
                    win = true;
                    break;
                }
            }
            if (!win)
            {
                Console.WriteLine("No more turns");
            }
        }
Ejemplo n.º 10
0
        private void ResetAnswer()
        {
            answerPegs.Clear();
            AnswerPanel.Children.Clear();

            for (int i = 0; i < ColumnSlider.Value; i++)
            {
                Peg peg = new Peg();
                peg.Color = (Peg.Colors)random.Next(1, (int)Peg.Colors.Num_Colors);
                answerPegs.Add(peg);
            }

            PegButtonContainerControl pegContainer = new PegButtonContainerControl(answerPegs);

            AnswerPanel.Children.Add(pegContainer);
        }
Ejemplo n.º 11
0
 public Code(Peg p0, Peg p1, Peg p2, Peg p3)
 {
     Pegs = ImmutableList.Create(p0, p1, p2, p3);
 }