public Arithmetic3x3 GenerateArithmetic3x3() { Random rand = new Random(); int num = rand.Next(1, 9); List <ArithmeticExpression3> arithmeticsRow1 = GetArithmetics3(num); ArithmeticExpression3 row1 = arithmeticsRow1[rand.Next(0, arithmeticsRow1.Count - 1)]; List <ArithmeticExpression3> arithmeticsCol1 = GetArithmetics3(row1.val1); ArithmeticExpression3 col1 = arithmeticsCol1[rand.Next(0, arithmeticsCol1.Count - 1)]; List <ArithmeticExpression3> arithmeticsRow2 = GetArithmetics3(col1.val2); List <ArithmeticExpression3> arithmeticsRow3 = GetArithmetics3((int)col1.GetResult()); List <ArithmeticExpression3> arithmeticsCol2 = GetArithmetics3(row1.val2); List <ArithmeticExpression3> arithmeticsCol3 = GetArithmetics3((int)row1.GetResult()); List <Arithmetic3x3> temp3x3 = new List <Arithmetic3x3>(); foreach (ArithmeticExpression3 row2 in arithmeticsRow2) { foreach (ArithmeticExpression3 row3 in arithmeticsRow3) { foreach (ArithmeticExpression3 col2 in arithmeticsCol2) { foreach (ArithmeticExpression3 col3 in arithmeticsCol3) { if (row2.val2 == col2.val2 && row2.GetResult() == col3.val2 && row3.val2 == col2.GetResult() && row3.GetResult() == col3.GetResult()) { Arithmetic3x3 newArithmetic3x3 = new Arithmetic3x3(); newArithmetic3x3.rows = new ArithmeticExpression3[3]; newArithmetic3x3.cols = new ArithmeticExpression3[3]; newArithmetic3x3.rows[0] = row1; newArithmetic3x3.rows[1] = row2; newArithmetic3x3.rows[2] = row3; newArithmetic3x3.cols[0] = col1; newArithmetic3x3.cols[1] = col2; newArithmetic3x3.cols[2] = col3; temp3x3.Add(newArithmetic3x3); } } } } } return(temp3x3[rand.Next(0, temp3x3.Count - 1)]); }
private List <ArithmeticExpression3> GetArithmetics3(int val1) { List <ArithmeticExpression3> arithmetics = new List <ArithmeticExpression3>(); foreach (TOperation operation in Enum.GetValues(typeof(TOperation))) { ArithmeticExpression3 arithmetic = new ArithmeticExpression3(); arithmetic.val1 = val1; for (int i = 1; i < 10; i++) { arithmetic.val2 = i; arithmetic.op = operation; float result = arithmetic.GetResult(); if (result <= 9 && result >= 0 && (result - Math.Round(result)) == 0) { arithmetics.Add(arithmetic); } } } return(arithmetics); }