Bulls and Cows
A challenge made by the forum "FXP", to build a quick game of "bulls and cows" in a short time. Entry creator: CC_CCunknown
csharp
3 years ago
3.8 kB
5
Indexable
using System; using System.Collections.Generic; using System.Linq; // namespace: bullsandcows // // summary: A challenge made by the forum "FXP", // to build a quick game of "bulls and cows" // in a short time. // Entry creator: CC_CC namespace bullsandcows { /// <summary> A static class to provide assistant functions over generic type. </summary> public static class Applier { /// <summary> Applies a function over argument. </summary> /// /// <typeparam name="TIn"> Type of the argument for application. </typeparam> /// <typeparam name="TOut"> Type of the applied argument result. </typeparam> /// <param name="x"> The applied argument. </param> /// <param name="f"> The function applied over the argument. </param> /// /// <returns> The applied argument result. </returns> public static TOut Apply<TIn, TOut>(this TIn x, Func<TIn, TOut> f) => f(x); } /// <summary> The program main class. </summary> class Program { /// <summary> Returns how many values are similar /// in the same position in guess and gen, /// and in different position in guess and gen. </summary> /// /// <exception cref="ArgumentException"> Thrown when guess or gen /// aren't the same length. </exception> /// /// <typeparam name="T"> Type of argument to compare guess and gen. </typeparam> /// <param name="guess"> The guessed input. </param> /// <param name="gen"> The generated value. </param> /// /// <returns> First result number of similar values in the same position, /// Second result number of similar values in different position. </returns> private static (int, int) Score<T>(IEnumerable<T> guess, IEnumerable<T> gen) where T : IEquatable<T> { var (bulls, nonbulls) = guess.Zip(gen) .ToLookup(x => { if (x.First == null || x.Second == null) throw new System.ArgumentException("guess and gen aren't the same length"); return x.First.Equals(x.Second); }) .Apply(x => (x[true], x[false])); var cows = nonbulls .Apply(x => (x.Select(xs => xs.First), x.Select(xs => xs.Second))) .Apply(x => x.Item1.Intersect(x.Item2)); return (bulls.Count(), cows.Count()); } /// <summary> Main entry-point for this application. </summary> public static void Main() { var nTry = 0; var bulls = 0; var cows = 0; var rand = new Random(Guid.NewGuid().GetHashCode()); Console.WriteLine("Enter the length of the number you intending to guess"); var length = int.Parse(Console.ReadLine()); var gen = new char[length]; for (int i = 0; i < gen.Length; i++) gen[i] = (char)rand.Next('0', '9'); while (bulls != length) { Console.Write(nTry + ": "); var guess = Console.ReadLine().ToCharArray(); if (guess.Length == length) { (bulls, cows) = Score(guess, gen); Console.WriteLine("Bulls={0}, Cows={1}", bulls, cows); nTry++; } else Console.WriteLine("Wrong length"); } Console.WriteLine("you Win! tries: {0}", nTry); } } }
Editor is loading...