public static string ApplyMistake(string function, Mistake mistake) { string products = function.Substring(4); string[] parts = products.Split(new string[] { " v " }, StringSplitOptions.None); string mistakenFunction = "Y = "; if (mistake.IndexOfX == null) // Apply Porduct Mistake { for (int i = 0; i < parts.Length; i++) { if (parts[i] == mistake.MistakenProduct) { parts[i] = mistake.MistakenValue + ""; break; } } mistakenFunction += string.Join(" v ", parts); return mistakenFunction; } for (int i = 0; i < parts.Length; i++) // Apply X Mistakes { int index = Math.Abs((int)mistake.IndexOfX - 3); StringBuilder sb = new StringBuilder(parts[i]); if (mistake.MistakenProduct == "X" && parts[i][index].ToString() == mistake.MistakenProduct) { sb[index] = mistake.MistakenValue.ToString()[0]; parts[i] = sb.ToString(); } else if (mistake.MistakenProduct == "x" && parts[i][index].ToString() == "x") { sb[index] = mistake.MistakenValue.ToString()[0]; parts[i] = sb.ToString(); } else if (mistake.MistakenProduct == "x" && parts[i][index].ToString() == "X") { if (mistake.MistakenValue == 1) { sb[index] = '0'; parts[i] = sb.ToString(); } else { sb[index] = '1'; parts[i] = sb.ToString(); } } } string imploded = string.Join(" v ", parts); mistakenFunction += imploded; return mistakenFunction; }
public static Mistake[] GenerateMistakes(Function function) { Mistake[] mistakes = new Mistake[4]; Mistake[] signMistakes = GenerateSignMistakes(function); Mistake[] productMistakes = GenerateProductMistake(function); signMistakes.CopyTo(mistakes, 0); productMistakes.CopyTo(mistakes, signMistakes.Length); return mistakes; }
private static Mistake[] GenerateSignMistakes(Function function) { string products = function.Function1.Substring(4); string[] parts = products.Split(new string[] { " v " }, StringSplitOptions.None); Mistake[] mistakes = new Mistake[2]; bool notFound = true; while (notFound) { int multiplierIndex = rnd.Next(0, 3); for (int i = 0; i < parts.Length; i++) { if (parts[i][multiplierIndex] == 'x') { int mistake = rnd.Next(0, 2); mistakes[0] = new Mistake("x", mistake, multiplierIndex + 1, GetDisplayX("x" + (multiplierIndex +1))); notFound = false; break; } } } notFound = true; while (notFound) { int multiplierIndex = rnd.Next(0, 3); for (int i = 0; i < parts.Length; i++) { if (parts[i][multiplierIndex] == 'X') { int mistake = rnd.Next(0, 2); mistakes[1] = new Mistake("X", mistake, multiplierIndex + 1, GetDisplayX("X" + (multiplierIndex + 1))); notFound = false; break; } } } return mistakes; }
private static Mistake[] GenerateProductMistake(Function function) { Mistake[] mistakes = new Mistake[2]; string products = function.Function1.Substring(4); string[] parts = products.Split(new string[] { " v " }, StringSplitOptions.None); if (function.Function2 == null) { int mistakeIndex1 = rnd.Next(0, 6); int mistakeIndex2 = rnd.Next(0, 6); while (mistakeIndex1 == mistakeIndex2) { mistakeIndex2 = rnd.Next(0, 6); } int mistakeValue1 = rnd.Next(0, 2); int mistakeValue2 = rnd.Next(0, 2); mistakes[0] = new Mistake(parts[mistakeIndex1], mistakeValue1, null, GetDisplayProduct(parts[mistakeIndex1])); mistakes[1] = new Mistake(parts[mistakeIndex2], mistakeValue2, null, GetDisplayProduct(parts[mistakeIndex2])); return mistakes; } int mistakeValue = rnd.Next(0, 2); int mistakeIndex = rnd.Next(0, 3); mistakes[0] = new Mistake(parts[mistakeIndex], mistakeValue, null, GetDisplayProduct(parts[mistakeIndex])); mistakeValue = rnd.Next(0, 2); mistakeIndex = rnd.Next(0, 3); products = function.Function2.Substring(4); parts = products.Split(new string[] { " v " }, StringSplitOptions.None); mistakes[1] = new Mistake(parts[mistakeIndex], mistakeValue, null, GetDisplayProduct(parts[mistakeIndex])); return mistakes; }
public static int[,] SolveFunction(string function, int[,] table, Mistake[] mistakes) { string products = function.Substring(4); string[] parts = products.Split(new string[] { " v " }, StringSplitOptions.None); for (int i = 0; i < 8; i++) // solve none mistaked functions { string[] functions = PrepareFunction(function, table, i); int result = 0; for (int j = 0; j < functions.Length; j++) { result = result | SolveProduct(functions[j]); } table[i,3] = result; } string[] mistakenFunctions = new string[4]; for (int i = 0; i < mistakes.Length; i++) { mistakenFunctions[i] = FunctionParser.ApplyMistake(function, mistakes[i]); } for (int i = 0; i < mistakes.Length; i++) { for (int j = 0; j < 8; j++) { string[] functions = PrepareFunction(mistakenFunctions[i], table, j); mistakenReadyFunctiongs[i] = string.Join(" v ", functions); int result = 0; for (int k = 0; k < functions.Length; k++) { result = result | SolveProduct(functions[k]); } table[j, i + 4] = result; } } return table; }
public static Solution Solve(Function function, Mistake[] mistakes) { Solution solution = new Solution(); int[,] solutionTable = GenerateTable(); solution.FirstTable = SolveFunction(function.Function1, solutionTable, mistakes); ; if (function.Function2 != null) { solutionTable = GenerateTable(); solution.SecondTable = SolveFunction(function.Function2, solutionTable, mistakes); } return solution; }