Click here to Skip to main content
14,699,570 members
Articles » General Programming » Algorithms & Recipes » String Matching
Tip/Trick
Posted 21 Jul 2016

Tagged as

Stats

10.1K views
33 downloads
1 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
21 Jul 2016CPOL
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:

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)

Share

About the Author

OriginalGriff
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

 
PraiseWhat a birlliant Article Pin
HobbyProggy21-Jul-16 2:20
professionalHobbyProggy21-Jul-16 2:20 
GeneralRe: What a birlliant Article Pin
OriginalGriff21-Jul-16 2:42
mveOriginalGriff21-Jul-16 2:42 
GeneralRe: What a birlliant Article Pin
HobbyProggy21-Jul-16 2:52
professionalHobbyProggy21-Jul-16 2:52 
PraiseJust... Pin
Kornfeld Eliyahu Peter21-Jul-16 1:47
mveKornfeld Eliyahu Peter21-Jul-16 1:47 
GeneralRe: Just... Pin
OriginalGriff21-Jul-16 2:10
mveOriginalGriff21-Jul-16 2:10 
GeneralRe: Just... Pin
Kornfeld Eliyahu Peter21-Jul-16 2:13
mveKornfeld Eliyahu Peter21-Jul-16 2:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.