//Remove and two items public static void Swap(this Sequence first,Sequence second, int index,int item) { int temp; if (first.digits.Length<index) temp = first.digits[0]; else temp = first.digits[index]; first.Merge(item, index); second.Merge(temp, index); }
public static int FindPosition(Sequence first) { int position = 0; if (first.bulls==null) { return 0; } else { for (int i = 0; i < first.cows.Length;i++ ) { if (first.bulls[i] == 0 && first.cows[i] == 0) return i; } } return position; }
public static void FindSequence(Sequence first,Sequence second,int bulls,int cows,int index,int temp,int item) { //B- C Same if (bulls < second.countOfbulls && second.countOfCows == cows) { //Swap first.Swap(second, index, item); first.Remove(item); second.bulls[index] = temp; bulls = bulls + 1; } //B+ C Same else if (bulls > second.countOfbulls && second.countOfCows == cows) { first.Remove(item); first.Remove(temp); second.bulls[index] = item; } //B same C+ else if (second.countOfbulls == bulls && cows > second.countOfCows) { second.cows[index] = item; } //B same C- else if (second.countOfbulls == bulls && cows < second.countOfCows) { first.Swap(second, index,item); first.Remove(item); second.cows[index] = temp; cows = cows + 1; } second.countOfCows = cows; second.countOfbulls = bulls; }
public static void FindNextSequence() { Console.WriteLine("1,2,3,4"); Console.WriteLine("Enter the bulls"); int bulls = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the cows"); int cows = Convert.ToInt32(Console.ReadLine()); int totalForFirst = bulls + cows; Sequence first = new Sequence(new int[] { 1, 2, 3, 4, 9 }, bulls, cows); Console.WriteLine("5,6,7,8"); Console.WriteLine("Enter the bulls"); bulls = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the cows"); cows = Convert.ToInt32(Console.ReadLine()); int totalForSecond = bulls + cows; Sequence second = new Sequence(new int[] { 5, 6, 7, 8 ,9}, bulls, cows); if (totalForFirst + totalForSecond == 4) { first.Remove(9); second.Remove(9); } if (totalForFirst > totalForSecond) { first.Remove(9); GenerateNextSequence(second, first); } else { second.Remove(9); GenerateNextSequence(first, second); } }
public static void GenerateNextSequence(Sequence first,Sequence second) { foreach(int item in first.digits) { //Swap int index = FindPosition(second); int temp = second.digits[index]; second.Swap(first, index, item); Console.WriteLine(string.Join(",", second.digits)); Console.WriteLine("Enter the bulls"); int bulls = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter the cows"); int cows = Convert.ToInt32(Console.ReadLine()); FindSequence(first, second, bulls, cows, index, temp, item); } }