public BullsnCows.Score CalcPerm(string number, string guess) { if (number.Length != length || !game.Perms.Contains(guess)) { return(new BullsnCows.Score(length, -1, -1)); } BullsnCows.Score score = new BullsnCows.Score(); score.Length = length; for (int i = 0; i < length; i++) { char curGuess = guess[i]; if (curGuess == number[i]) { score.Strike++; } else if (number.Contains(curGuess)) { score.Ball++; } } return(score); }
public void CalcScore(BullsnCows.Score score, string guess) { if (score.IsError) { game.Symbol.Emotion = Symbol.Emotions.Abashed; game.Message.Text = "Not a valid answer."; return; } if (score.Strike == length) { game.Symbol.Emotion = Symbol.Emotions.Optimistic; game.Message.Text = "Hooray! I win!" + Environment.NewLine + "My number is " + game.ComNumber + "!"; game.GameTurn = BullsnCows.Turn.End; game.Winner = BullsnCows.Turn.Computer; return; } game.Symbol.Emotion = Symbol.RandomEmotion(); game.Message.Text = "I see."; if (game.Answers.Count > 0) { for (int idx = game.Answers.Count - 1; idx >= 0; idx--) { string answer = game.Answers[idx]; int strike = 0, ball = 0; for (int i = 0; i < length; i++) { if (answer[i] == guess[i]) { strike++; } else if (answer.Contains(guess[i])) { ball++; } } if ((strike != score.Strike) || (ball != score.Ball)) { game.Answers.RemoveAt(idx); } } } }
public BullsnCows.Score PlayerTurn(string guess) { if (game.GameTurn != BullsnCows.Turn.Player || !game.Perms.Contains(guess)) { return(new BullsnCows.Score(length, -1, -1)); } BullsnCows.Score score = CalcPerm(game.ComNumber, guess); if (score.Strike == length) { game.Symbol.Emotion = Symbol.Emotions.Abashed; game.Message.Text = "Oh My Goodness......." + Environment.NewLine + "What is your number?"; game.IsCheck = true; } else { game.Symbol.Emotion = Symbol.RandomEmotion(); } return(score); }