/// <summary>
        /// The AI's first move.
        /// </summary>
        private void AIFirstMove()
        {
            if (this.gameControl.ActivePlayer.AttemptsUsed < 1)
            {
                IntToSymbol its           = new IntToSymbol();
                Random      rand          = new Random();
                List <int>  randomNumbers = Enumerable.Range(0, this.gameControl.CodeDepth).OrderBy(x => rand.Next()).Take(this.gameControl.CodeDepth).ToList();

                string codeToBuildOn = string.Empty;

                for (int i = 0; i < this.gameControl.CodeLength; i++)
                {
                    int tempChar = randomNumbers[rand.Next(0, randomNumbers.Count() - 1)];
                    codeToBuildOn = codeToBuildOn + its.convert(tempChar);
                    randomNumbers.Remove(tempChar);
                }

                this.AIenterGuess(codeToBuildOn);
            }
            else
            {
                this.AIsolver();
            }

            this.ScoreTheGuess(this.gameControl.ActivePlayer.AttemptsUsed);
        }
        /// <summary>
        /// Draws the box of Pickers.
        /// </summary>
        private void DrawPickerBox()
        {
            IntToSymbol its = new IntToSymbol();

            this.Pickers = new List <Picker>();
            int pickerXpos = 20;

            for (int i = 0; i < this.gameControl.CodeDepth; i++)
            {
                Picker picker = new Picker(this, this.gameControl, i);
                picker.Name = "Picker" + i;

                picker.Text = its.convert(i);

                picker.Width  = 35;
                picker.Height = 35;

                picker.Location = new Point(pickerXpos, 0);
                pickerXpos      = pickerXpos + 35;
                this.Controls.Add(picker);
                this.Pickers.Add(picker);
            }

            this.gameControl.ActivePlayer.SelectedPicker = this.GetPickerByID(0);
        }
        /// <summary>
        /// Generates a Secret Code instead of manually picking it.
        /// </summary>
        private void GenerateSecretCode()
        {
            IntToSymbol its           = new IntToSymbol();
            Random      rand          = new Random();
            List <int>  randomNumbers = Enumerable.Range(0, this.gameControl.CodeDepth).OrderBy(x => rand.Next()).Take(this.gameControl.CodeDepth).ToList();

            for (int i = 0; i < this.gameControl.CodeLength; i++)
            {
                int tempChar = randomNumbers[rand.Next(0, randomNumbers.Count() - 1)];

                string symbol = its.convert(tempChar);

                if (this.gameControl.Versus == true)
                {
                    this.gameControl.ActivePlayer.SecretCode = this.gameControl.ActivePlayer.SecretCode + symbol;
                }
                else
                {
                    this.gameControl.ActivePlayer.SecretCode = this.gameControl.ActivePlayer.SecretCode + "0"; // You have to give the player some code, even though it's not used.
                    this.gameControl.Opponent.SecretCode     = this.gameControl.Opponent.SecretCode + symbol;
                }

                randomNumbers.Remove(tempChar);
            }

            buttonSubmit.Enabled = true;

            if (this.gameControl.Versus == false)
            {
                buttonSubmit.Focus();
                SendKeys.Send("{ENTER}");
            }
        }
        /// <summary>
        /// Generates all permutations for a given code length and depth.
        /// NOTE: This works with integers, not special unicode symbols. Output from this
        /// method must be later converted.
        /// </summary>
        /// <param name="size">How long is the code?</param>
        /// <param name="depth">How many unique symbols in the code?</param>
        /// <returns>A collection of strings.</returns>
        public IEnumerable <string> Permutations(int size, int depth)
        {
            IntToSymbol its = new IntToSymbol();

            if (size > 0)
            {
                foreach (string s in Permutations(size - 1, depth))
                {
                    string allChars = string.Empty;

                    for (int i = 0; i < depth; i++)
                    {
                        allChars += its.convert(i);
                    }

                    foreach (char c in allChars)
                    {
                        if (!s.Contains(c))
                        {
                            yield return(s + c);
                        }
                    }
                }
            }
            else
            {
                yield return(string.Empty);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Initializes a new instance of the GuessSpot class.
        /// </summary>
        /// <param name="gameControl">A passed-in reference to the GameControl form.</param>
        /// <param name="gameBo">A passed-in reference to the GameBoard form.</param>
        public GuessSpot(GameController gameControl, GameBoard gameBo)
        {
            this.gameController = gameControl;
            this.gameBoard      = gameBo;
            this.BorderStyle    = BorderStyle.FixedSingle;
            this.TextAlign      = HorizontalAlignment.Center;
            this.MaxLength      = 1;
            this.Text           = "[ ]";
            string lastText = "[ ]";

            this.Font = new Font(this.Font.FontFamily, this.gameBoard.SquareSize * 5 / 8, FontStyle.Regular);

            this.GotFocus += (r, y) =>
            {
                if (lastText != "[ ]")
                {
                    this.Text = string.Empty;
                }
            };

            this.Click += (s, z) =>
            {
                if (this.row == gameControl.ActivePlayer.AttemptsUsed)
                {
                    this.Text = gameControl.ActivePlayer.SelectedPicker.Text;
                    soundConfirm.Play();
                }
            };

            this.TextChanged += (s, z) =>
            {
                this.Invalidate();
                this.Update();

                // Only increment spotsUsed if this is the first time this text has changed.
                if (lastText == "[ ]")
                {
                    insuranceOn = false;
                    gameBoard.SpotsUsed++;
                }

                int tempInt;

                bool result = int.TryParse(this.Text.ToString(), out tempInt);
                if (result == true)
                {
                    soundConfirm.Play();
                    lastText = result.ToString(); // Make sure Last Text keeps this from triggering again after conversion.

                    // Convert those ints to symbols.
                    IntToSymbol its = new IntToSymbol();
                    this.Text = its.convert(tempInt);

                    foreach (Picker p in gameBoard.Pickers)
                    {
                        if (p.Id == tempInt)
                        {
                            gameControl.ActivePlayer.SelectedPicker = p;
                            p.BackColor = Color.Cyan;
                        }
                        else
                        {
                            p.BackColor = p.InitialColor;
                        }
                    }

                    if (gameControl.ActivePlayer.IsHuman == true && gameBo.SpotsUsed > 0)
                    {
                        SendKeys.Send("{TAB}");
                    }
                }

                lastText = this.Text;

                if (gameBoard.SpotsUsed == gameControl.CodeLength && gameControl.ActivePlayer.IsHuman == true && insuranceOn == false && gameBoard.GameOver == false)
                {
                    gameBoard.ScoreTheGuess(this.Row);
                    insuranceOn = true;
                    return;
                }
                else if (gameBoard.SpotsUsed == gameControl.CodeLength && gameControl.ActivePlayer.IsHuman == false)
                {
                    return;
                }
            };
        }
        /// <summary>
        /// Draws the entry boxes.
        /// </summary>
        private void DrawEntryBoxes()
        {
            IntToSymbol its  = new IntToSymbol();
            int         xPos = 20;

            for (int i = 0; i < this.gameControl.CodeLength; i++)
            {
                TextBox textboxEntry = new TextBox();
                textboxEntry.Location    = new Point(xPos, 150);
                textboxEntry.Font        = new Font("Century Gothic", 28, FontStyle.Bold);
                textboxEntry.Width       = this.specialSquareSize;
                textboxEntry.Height      = this.specialSquareSize;
                textboxEntry.Text        = "[ ]";
                textboxEntry.BorderStyle = BorderStyle.FixedSingle;
                textboxEntry.TextAlign   = HorizontalAlignment.Center;
                textboxEntry.MaxLength   = 1;

                textboxEntry.Click += (s, z) =>
                {
                    textboxEntry.Text = gameControl.ActivePlayer.SelectedPicker.Text;
                    soundConfirm.Play();
                };
                textboxEntry.TextChanged += (s, z) =>
                {
                    int tempInt;

                    bool result = int.TryParse(textboxEntry.Text.ToString(), out tempInt);
                    if (result == true)
                    {
                        soundConfirm.Play();

                        textboxEntry.Text = its.convert(tempInt);

                        foreach (Picker p in Pickers)
                        {
                            if (p.Id == tempInt)
                            {
                                gameControl.ActivePlayer.SelectedPicker = p;
                                p.BackColor = Color.Cyan;
                            }
                            else
                            {
                                p.BackColor = p.InitialColor;
                            }
                        }
                    }

                    buttonSubmit.Focus();
                };

                xPos = xPos + this.specialSquareSize;

                this.Controls.Add(textboxEntry);
                textboxEntry.Name = textboxEntry.Name + i;
                this.codeEntryList.Add(textboxEntry);

                this.buttonSubmit.Location    = new Point((this.specialSquareSize * this.gameControl.CodeLength) + 20, 150);
                this.buttonSubmit.Width       = this.specialSquareSize * 2;
                this.buttonGenerateCode.Width = (this.specialSquareSize * this.gameControl.CodeLength) + (this.specialSquareSize * 2);
                this.Width = (this.specialSquareSize * this.gameControl.CodeLength) + (this.specialSquareSize * 3);
            }
        }