Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Need help with understanding a code - C#

$
0
0

Hello there. 

I am currently working on a project, mostly to learn and get more comfortable with the C# language as I am fairly new to the programming world. The project is mainly a Mastermind Game created in Windows Forms.

After a lot of work and success I got stuck with a problem. To explain it more detailed - assuming that you know the rules of Mastermind, after each time the player makes a guess where he put up what he thinks might be the correct colour order, he will get to know how many of his pins which were right colour on the right place, and right colour but on the wrong place.

However, no matter how many different codes and methods I wrote, it still didnt work as I intended, so I asked a mate who is far more experienced with programming for some help, and he gave me this...

/*RightCRightP is the variable for how many pins which are right colour and on right place.

RightCWrongP is the variable for how many pins which are right colour but on wrong place.

guessList is a list including the players guesses.

answerList is a list including the right answer ( right order ).

*/

//His code

var RightCRightP = guessList .Zip(answerList, (g, s) => g == s) .Count(z => z); var RightCWrongP = guessList .Intersect(answerList) .Sum(c => System.Math.Min( answerList.Count(x => x == c), guessList.Count(x => x == c))) - RightCRightP;

The thing is, I want to learn what each thing in this codepiece do so that I can learn from it and not just copy his thing with no sence to it. Sadly I cant ask him personally because he is currently on vacation so I thought that anyone here could help me out instead.

Thanks in advance, and please be as gentle with the explanations as possible. Im still a novice!



Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>