} /*makeGuess*/ public ColorSequence giveHint() { ColorSequence guess = GameState.FeedbackHistory.Count == 0 ? makeGuess() : getNextGuess(GameState.FeedbackHistory.Last()); return guess; } /*giveHint*/
} /*giveControlsproperties*/ public void drawFrm() { Main.toolStripSave.Enabled = true; NrOfPins = Main.Mastermind.GameState.NrOfPins; NrOfTries = Main.Mastermind.GameState.NrOfTries; NrOfAvailableColors = Main.Mastermind.GameState.NrOfAvailableColors; showPlayerLabels(); showFbLabels(); showSecretLabels(); showAvailableLabels(); resizeFrmMain(); Main.CenterThisScreen(); highlightAccesiblePlayerLabels(); if (Main.Mastermind.GameState.ComputerIsPlaying && Main.Mastermind.GameState.TurnHistory.Count == 0) { ColorSequence firstGuess = Main.Mastermind.MSolver.makeGuess(); for (byte i = 0; i < NrOfPins; i++) LblArrayPlayer[0, i].BackColor = Color.FromName(firstGuess.getColorOnPlace(i).ToString()); } btnShow(); } /*drawFrm*/
} /*btnHint_Click*/ public void btnGiveFeedBack_Click(object sender, EventArgs e) { if (!Mastermind.gameIsOver()) { try { Button btn = sender as Button; byte correcPlaceAndCol = 0; byte correctCol = 0; for (int i = 0; i < Mastermind.GameState.NrOfPins; i++) { if (CtrlController.LblArrayFeedBackPins[Mastermind.GameState.TurnNumber - 1, i].BackColor == Color.Black) { correcPlaceAndCol++; } if (CtrlController.LblArrayFeedBackPins[Mastermind.GameState.TurnNumber - 1, i].BackColor == Color.White) { correctCol++; } } Feedback fb = new Feedback(correcPlaceAndCol, correctCol); ColorSequence newGuess = Mastermind.MSolver.makeGuess(fb); if (Mastermind.gameIsOver()) { MessageBox.Show(Mastermind.GameState.Won ? "Won" : "Lost"); } else { for (byte i = 0; i < newGuess.Length; i++) { CtrlController.LblArrayPlayer[Mastermind.GameState.TurnNumber - 1, i].BackColor = Color.FromName(newGuess.getColorOnPlace(i).ToString()); } if (btn != null) btn.Top += 50; pnlPlace.ScrollControlIntoView(CtrlController.LblArrayPlayer[Mastermind.GameState.TurnNumber - 1, 0]); } } catch (ArgumentOutOfRangeException) { MessageBox.Show(Mastermind.GameState.Won ? "Won" : "You must have made a mistake, I can't do anything anymore"); } } } /*btnGiveFeedBack_Click*/
} /*checkAnswerAndGiveFeedbackFor*/ private bool answerIsAllowed(ColorSequence guess) { for (byte i = 0; i < guess.Length; i++) for (byte j = 0; j < guess.Length; j++) if (i != j && guess.getColorOnPlace(i) == guess.getColorOnPlace(j)) return false; return true; } /*answerAllowed*/
} /*Randomizer*/ #endregion Properties #region Constructors public GameState(byte tries, byte pins, byte colors, bool computerIsPlaying, ColorSequence secretCombo = null) { NrOfTries = tries; NrOfPins = pins; NrOfAvailableColors = colors; ComputerIsPlaying = computerIsPlaying; TurnHistory = new List <ColorSequence>(); FeedbackHistory = new List <Feedback>(); TurnNumber = 1; SecretCombo = secretCombo ?? createSecretCombo(); } /*GameState*/
} /*Mastermind*/ #endregion constructors #region behavior internal Feedback checkAnswerAndGiveFeedbackFor(ColorSequence comboToCheck) { if (!answerIsAllowed(comboToCheck)) throw new ArgumentException(); Feedback fb = getSecretCombo().compareToColorsequence(comboToCheck); GameState.TurnHistory.Add(comboToCheck); GameState.FeedbackHistory.Add(fb); GameState.updateTurn(); return fb; } /*checkAnswerAndGiveFeedbackFor*/
} /*btnGuess_Click*/ public void btnHint_Click(object sender, EventArgs e) { if (!Mastermind.gameIsOver()) { try { ColorSequence guess = Mastermind.MSolver.giveHint(); for (byte i = 0; i < guess.Length; i++) { CtrlController.LblArrayPlayer[Mastermind.GameState.TurnNumber - 1, i].BackColor = Color.FromName(guess.getColorOnPlace(i).ToString()); } passGuessAndGetFeedback(guess); } catch (IndexOutOfRangeException) { MessageBox.Show( string.Format("I can't seem to resolve the puzzel"), string.Format("Error")); } } } /*btnHint_Click*/
public void btnGuess_Click(object sender, EventArgs e) { if (!Mastermind.gameIsOver()) { try { ColorSequence guess = new ColorSequence(); for (int i = 0; i < Mastermind.GameState.NrOfPins; i++) guess.addLast( ClrPicker.parseEnum <Colors>( CtrlController.LblArrayPlayer[Mastermind.GameState.TurnNumber - 1, i].BackColor.ToKnownColor().ToString())); passGuessAndGetFeedback(guess); } catch (ArgumentException) { MessageBox.Show( string.Format("Every pin must have a color and a color can't be used twice..."), string.Format("Error")); } } } /*btnGuess_Click*/
} /*setColorOnPlace*/ public Feedback compareToColorsequence(ColorSequence toCompare) { byte correctCol = 0; byte correctPosAndCol = 0; for (byte i = 0; i < toCompare.ColorCombo.Count; i++) { if (toCompare.ColorCombo[i] == ColorCombo[i]) correctPosAndCol++; for (byte j = 0; j < ColorCombo.Count; j++) if (toCompare.ColorCombo[i] == ColorCombo[j]) correctCol++; } correctCol -= correctPosAndCol; Feedback fb = new Feedback(correctPosAndCol, correctCol); return fb; } /*compareToColorsequence*/
} /*updateTurn*/ private ColorSequence createRandomSequence(byte length) { ColorSequence randomCombo = new ColorSequence(); bool[] used = new bool[NrOfAvailableColors]; for (byte i = 0; i < length; i++) { int tmp; do { tmp = Randomizer.Next(NrOfAvailableColors); } while (used[tmp]); randomCombo.addLast((Colors)tmp); used[tmp] = true; } return randomCombo; } /*createRandomSequence*/
} /*GetCombinations*/ private List <ColorSequence> getAllPossibleCombos(byte nrOfPins, byte nrOfColors) { List <ColorSequence> combinations = new List <ColorSequence>(); Colors[] bufferAvailable = new Colors[nrOfColors]; for (int i = 0; i < bufferAvailable.Length; i++) bufferAvailable[i] = (Colors)i; IEnumerable <IEnumerable <Colors>> result = GetCombinations(bufferAvailable, nrOfPins); foreach (IEnumerable <Colors> res in result) { List <Colors> tmp = res.ToList(); ColorSequence clsq = new ColorSequence(tmp); combinations.Add(clsq); } return combinations; } /*getAllPossibleCombos*/
} /*removeBadCombosFromGoodCombos*/ public ColorSequence makeGuess(Feedback fb = null) { ColorSequence guess; if (fb == null) { Colors[] buffer = new Colors[GameState.NrOfPins]; for (byte i = 0; i < buffer.Length; i++) buffer[i] = (Colors)i; guess = new ColorSequence(buffer); } else { GameState.FeedbackHistory.Add(fb); guess = getNextGuess(fb); GameState.updateTurn(); } GameState.TurnHistory.Add(guess); return guess; } /*makeGuess*/
} /*showOptions*/ private void passGuessAndGetFeedback(ColorSequence guess) { Feedback feedback = Mastermind.checkAnswerAndGiveFeedbackFor(guess); int tmp = 0; for (byte j = 0; j < feedback.CorrectPositionAndColor; j++) { CtrlController.LblArrayFeedBackPins[Mastermind.GameState.TurnNumber - 2, tmp].BackColor = Color.Black; tmp++; } for (byte j = 0; j < feedback.CorrectColor; j++) { CtrlController.LblArrayFeedBackPins[Mastermind.GameState.TurnNumber - 2, tmp].BackColor = Color.White; tmp++; } if (Mastermind.gameIsOver()) { MessageBox.Show(Mastermind.GameState.Won ? "Won" : "Lost"); CtrlController.unvealSecret(); } else { if (CtrlController.BtnGuess != null) CtrlController.BtnGuess.Top += 50; pnlPlace.ScrollControlIntoView(CtrlController.LblArrayPlayer[Mastermind.GameState.TurnNumber - 1, 0]); CtrlController.highlightAccesiblePlayerLabels(); } } /*passGuessAndGetFeedback*/
} /*getNextGuess*/ private void removeBadCombosFromGoodCombos(Feedback fb, ColorSequence turn) { GoodCombos.Remove(GoodCombos.Find(combo => combo.Equals(turn))); GoodCombos.RemoveAll(combo => !combo.compareToColorsequence(turn).Equals(fb)); } /*removeBadCombosFromGoodCombos*/
} /*MSolver*/ #endregion Properties #region constructors public Mastermind(byte tries, byte pins, byte colors, bool computerIsPlaying, ColorSequence secretCombo) : this(new GameState(tries, pins, colors, computerIsPlaying, secretCombo)) { }