Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#
Tip/Trick

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 13.1K   33   1   6
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)


Written By
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 1:20
professionalHobbyProggy21-Jul-16 1:20 
GeneralRe: What a birlliant Article Pin
OriginalGriff21-Jul-16 1:42
mveOriginalGriff21-Jul-16 1:42 
GeneralRe: What a birlliant Article Pin
HobbyProggy21-Jul-16 1:52
professionalHobbyProggy21-Jul-16 1:52 
PraiseJust... Pin
Kornfeld Eliyahu Peter21-Jul-16 0:47
professionalKornfeld Eliyahu Peter21-Jul-16 0:47 
You should write it as a standalone web page using JavaScript Laugh | :laugh:
Skipper: We'll fix it.
Alex: Fix it? How you gonna fix this?
Skipper: Grit, spit and a whole lotta duct tape.

GeneralRe: Just... Pin
OriginalGriff21-Jul-16 1:10
mveOriginalGriff21-Jul-16 1:10 
GeneralRe: Just... Pin
Kornfeld Eliyahu Peter21-Jul-16 1:13
professionalKornfeld Eliyahu Peter21-Jul-16 1: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.