Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Scoring the FOSW of the Day for People Who Find It Difficult.

5.00/5 (3 votes)
21 Jul 2016CPOL 14.3K   33  
Scoring the FOSW of the day for people who find it difficult

Introduction

All this does is let you enter two strings, and get the Black / White counts according to the Mastermind rules:

Using the Code

It's pretty simple - all it needs is two lines of Linq:

C#
char[] solution = tbAnswer.Text.ToArray();
char[] guess = tbGuess.Text.ToArray();
int black = guess.Zip(solution, (g, s) => g == s).Count(b => b);
int white = guess.Intersect(solution).Sum
(c => System.Math.Min(solution.Count(x => x == c), guess.Count(x => x == c))) - black;
tbScore.Text = string.Format("{0}B, {1}W", black, white);

History

  • 2016-07-21: Original version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)